我在Redshift
上使用postgres
。我有一個查詢這是這樣的:基於來自內部查詢的組合的子集設置
SELECT EXTRACT(year from created_at) AS CustomYear,
client_ip,
member_id,
COUNT(*) AS Views
FROM ads.fbs_page_view_staging
WHERE member_id = 2
GROUP BY CustomYear,
client_ip,
member_id
HAVING COUNT(*) = 1
ORDER BY CustomYear
在這裏,我選擇的client_ip
和member_id
其中Views
是組合1.我現在想採取的client_ip
和member_id
這些組合和子集整個表ads.fbs_page_view_staging
只有這樣的組合。
如果只有一個專欄中,我想在子集,說client_ip
,我已經寫了下面的查詢,並得到了結果:
SELECT EXTRACT(year FROM created_at) AS CustomYear,
COUNT(*)
FROM ads.fbs_page_view_staging
WHERE member_id = 2
AND client_ip IN (SELECT client_ip
FROM ((SELECT EXTRACT(year from created_at) AS CustomYear,
client_ip,
member_id,
COUNT(*)
FROM ads.fbs_page_view_staging
WHERE member_id = 2
GROUP BY CustomYear,
client_ip,
member_id
HAVING COUNT(*) = 1
ORDER BY CustomYear)))
GROUP BY customyear
ORDER BY customyear
注意,在外部查詢,我子集化基礎的在client_ip
。但是,我如何在列的組合上對錶進行分組?
任何幫助將不勝感激。
我認爲這可能會有所幫助,如果您能準確描述您想要在最終結果中看到哪些行。我知道你想要一個基於子查詢的子集,但是你能更準確地描述它嗎? – Bampfer