我有查询问题。我用postgresql! 我需要从日期中提取月份并得到月份的总和,然后将其放在一列
中table qwe
----------------------------
tipe | dateissued
----------------------------
a | 8/12/2013
b | 8/12/2013
c | 8/12/2013
d | 9/12/2013
我需要的结果是
----------------------------
tipe | month | totalMonth
----------------------------
a | 8 | 2
b | 8 | 2
c | 8 | 2
d | 9 | 2
“2”总月份我从8& 9
查询我到目前为止已经完成了
select tipe , extract(month from dateissued),
count( extract(month from dateissued)) over() as totalMonth
from qwe
group by tipe,dateissued
答案 0 :(得分:1)
您需要另一个选择来计算不同的月份:
SELECT tipe,
Extract(month FROM dateissued),
(SELECT Count(DISTINCT( Extract(month FROM dateissued) )) AS totalMonth
FROM qwe)
FROM qwe
GROUP BY tipe,
dateissued;
SQL小提琴:SQL Fiddle