transaction_date采用日期格式 我实际上要输出的是按季度计算的唯一ID的DUNTINCT(即,给定四分之一的Unique_Id出现了多少次)。
SELECT transaction_date ,
UNIQUE_ID,
FROM panel
WHERE (some criteria = 'x')
GROUP BY UNIQUE_ID
答案 0 :(得分:2)
试试这个:
SELECT datepart(quarter,transaction_date),
count(distinct UNIQUE_ID) as cnt
FROM panel
WHERE (some criteria = 'x')
GROUP BY datepart(quarter,p.transaction_date)
但是计数(不同)将进行排序,因此需要花费大量时间。所以你可以先在表格中区分它然后进行计数
SELECT datepart(quarter,p.transaction_date),
count(p.UNIQUE_ID) as cnt
FROM (select distinct transaction_date as transaction_date, UNIQUE_ID
from panel) as p
WHERE (some criteria = 'x')
GROUP BY datepart(quarter,p.transaction_date)
答案 1 :(得分:2)
我会使用date_trunc
:
select
date_trunc ('quarter', transaction_date), count (distinct unique_id)
from panel
where criteria = 'x'
group by 1
这预示着当你说“按季度”时,2015年第1季度与2014年第1季度不同。
答案 2 :(得分:0)
SELECT DATEPART(QUARTER, transaction_date) ,
COUNT(DISTINCT UNIQUE_ID),
FROM panel
GROUP BY transaction_date