提取月份,计算并将其放在一列中

时间:2013-10-09 14:13:54

标签: sql postgresql

我有查询问题。我用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

http://sqlfiddle.com/#!12/fa8d4/6/0

1 个答案:

答案 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