我有疑问。我用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

最佳答案

您需要另一个选择来计算不同月份:

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

关于sql - 提取月份,计算并放入列中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19274530/

10-15 22:23