2017-03-23 118 views
1

我使用紅移的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 
) 
+0

Redshift不以有用的方式支持generate_series。有解決方法:http://stackoverflow.com/a/34167753/3019685。 – systemjack

回答

3

PostgreSQL 8.4後:

隨着generate_series(start, stop, step interval)可以填充日期爲您每月插入數據:

SELECT 
t.d::date, 
date as original_date, 
user_id 
FROM user_activity, 
generate_series(date,date+ interval '1 month - 1 day','1 day') t(d) 

看看這個鏈接獲取更多信息generate_series

PostgreSQL 8.4,然後才能創建你自己的功能:

create or replace function generate_series_date(begin_date integer,end_date integer) 
RETURNS integer AS $$ 

declare 

affected_rows integer :=0; 
integer_var integer:=0; 

begin 
FOR i IN begin_date..end_date LOOP 
    INSERT INTO monthly_user_activity 
    (mau_date, date, user_id) 
    select 
    date+ "interval"(''''||i||' day'''), 
    date, 
    user_id 
    FROM user_activity; 

    GET DIAGNOSTICS integer_var = ROW_COUNT; 
    affected_rows = affected_rows+integer_var; 

    END LOOP; 

    return affected_rows; 

    end; 
$$ LANGUAGE plpgsql 
+0

我越來越多的函數generate_series(時間戳沒有時區,時間戳沒有時區,「未知」)不存在; –

+0

什麼是PostgreSQL版本? –

+0

i686-pc-linux-gnu上的PostgreSQL 8.0.2,由GCC gcc編譯(GCC)3.4.2 20041017(Red Hat 3.4.2-6.fc3),Redshift 1.0.1231 –