2015-10-15 102 views
1

假設我們有一個包含日期的ID列表。我們想知道第一次和第二次ID出現的時間。大約第一次,我創建了一個查詢,這是第一次和第二次在PostgreSQL中出現行ID

SELECT year, mon, COUNT(id) AS sum_first_id 
FROM (
    SELECT DISTINCT 
     ON (id) DATE, id 
    FROM TABLE 
    GROUP BY 2, 1 
    ) AS foo 
GROUP BY 2, 1 
ORDER BY 1, 2; 

我認爲這是有效的。但是我怎樣才能找到IDs第二次出現?

回答

2

比方說,你有表table_x

select * 
from table_x 
order by 1, 2 

id | date  
----+------------ 
    1 | 2015-06-04 
    1 | 2015-06-05 
    1 | 2015-06-14 
    2 | 2015-06-05 
    2 | 2015-06-08 
    2 | 2015-06-10 
    2 | 2015-06-17 
    2 | 2015-06-22 
(8 rows) 

要選擇N個第一元素組使用row_number()功能:

select id, date 
from (
    select id, date, row_number() over (partition by id order by date) rn 
    from table_x 
    order by 1, 2 
    ) sub 
where rn <= 2 

id | date  
----+------------ 
    1 | 2015-06-04 
    1 | 2015-06-05 
    2 | 2015-06-05 
    2 | 2015-06-08 
(4 rows) 

它不會出現你的查詢是正確的。

SELECT year, mon, COUNT(id) AS sum_first_id -- what is year, mon? 
FROM (
    SELECT DISTINCT 
     ON (id) DATE, id 
    FROM TABLE 
    GROUP BY 2, 1       -- should be order by 2, 1 
    ) AS foo 
GROUP BY 2, 1 
ORDER BY 1, 2; 
+0

年,mon是提取年月日,我沒有定義,對不起!你放的是什麼條款,你能解釋我嗎? –

+0

'rn'是'row_number()'的別名。在主查詢中嘗試'選擇id,date,rn'來查看它的值。 – klin

相關問題