我有一个包含这些样本数据的表(用户(
user location name
111 usa aaa
222 canada bbb
333 usa ccc
444 mexico ddd
555 japan eee
从这个表中,我想得到如下的最终输出
date user location name
2020-11-03 111 usa aaa
2020-11-03 222 canada bbb
2020-11-03 333 usa ccc
2020-11-03 444 mexico ddd
2020-11-03 555 japan eee
2020-11-02 111 usa aaa
2020-11-02 222 canada bbb
2020-11-02 333 usa ccc
2020-11-02 444 mexico ddd
2020-11-02 555 japan eee
2020-11-01 111 usa aaa
2020-11-01 222 canada bbb
2020-11-01 333 usa ccc
2020-11-01 444 mexico ddd
2020-11-01 555 japan eee
如果您看到上面的输出,对于每一天(过去3天(,我都会看到user,location,name
数据。
有人能为提出解决方案吗
您似乎想要cross join
:
select v.date, t.*
from t cross join
(values ('2020-11-03'), ('2020-11-02'), ('2020-11-01')) v(date);
你可以很容易地在最后三天调整:
select current_date - v.n * interval '1 day', t.*
from t cross join
(values (1), (2), (3)) v(n);