我使用紅移的Postgres從單一插入多行選擇
我想要的輸出爲每天
輸出表monthly_user_activity
id maudate date userid
1 2016-10-10 2016-10-10 MjA1MDATG2TDUwOTE2MD
2 2016-10-11 2016-10-10 MjA1MDATG2TDUwOTE2MD
3 2016-10-12 2016-10-10 MjA1MDATG2TDUwOTE2MD
4 2016-10-13 2016-10-10 MjA1MDATG2TDUwOTE2MD
5 2016-10-14 2016-10-10 MjA1MDATG2TDUwOTE2MD
6 2016-10-15 2016-10-10 MjA1MDATG2TDUwOTE2MD
7 2016-10-16 2016-10-10 MjA1MDATG2TDUwOTE2MD
...
30 2016-11-09 2016-10-10 MjA1MDATG2TDUwOTE2MD
輸入表user_activity:
id date userid
1 2016-10-10 MjA1MDATG2TDUwOTE2MD
我的輸入只是一行代表一天中的用戶活動,對於e在此表下面我想在未來創造天30個副本ACH行,我這是怎麼會計算每日MAU
我想這樣的事情,下面我願意來運行此查詢30次,它似乎不是很乾。
INSERT INTO monthly_user_activity
(maudate, date, userid)
SELECT
original_date::date + interval '0 day',
original_date,
userid
FROM (
SELECT
date as original_date,
userid
FROM
user_activity
)
INSERT INTO monthly_user_activity
(maudate, date, userid)
SELECT
original_date::date + interval '1 day',
original_date,
userid
FROM (
SELECT
date as original_date,
userid
FROM
user_activity
)
INSERT INTO monthly_user_activity
(maudate, date, userid)
SELECT
original_date::date + interval '2 day',
original_date,
userid
FROM (
SELECT
date as original_date,
userid
FROM
user_activity
)
---...
INSERT INTO monthly_user_activity
(maudate, date, userid)
SELECT
original_date::date + interval '30 day',
original_date,
userid
FROM (
SELECT
date as original_date,
userid
FROM
user_activity
)
從評論 更新這是使用你發給我的鏈接的正確方法。
SELECT
id
d::date,
date as original_date,
userid
FROM user_activity,
(
select (dateadd(day, +row_number() over (order by true), date)) as d
from user_activity limit 30
)
Redshift不以有用的方式支持generate_series。有解決方法:http://stackoverflow.com/a/34167753/3019685。 – systemjack