使用SQL查询返回每周创建的票证数

时间:2011-03-08 18:19:11

标签: sql postgresql

我有一张票证表,common_ticket,有一个名为creation_date的列,它保存着创建日期。

我想计算过去几个月每周创建的门票数量。我在编写SQL查询以返回此类信息时遇到问题。只要每个单独的星期有一个不同的数字,它的返回方式就不是很重要。

有没有人对如何做到这一点有任何想法?

2 个答案:

答案 0 :(得分:13)

类似的东西:

SELECT extract(week from creation_date), 
       extract(year from creation_date),
       count(*)
FROM tickets
GROUP BY extract(week from creation_date), 
         extract(year from creation_date)

答案 1 :(得分:2)

这应该这样做:

SELECT [t1].[value] AS [Year], [t1].[value2] AS [Week], COUNT(*) AS [Count]
FROM (
    SELECT DATEPART(Year, [t0].[creation_date]) AS [value],
        DATEPART(Week, [t0].[creation_date]) AS [value2]
    FROM [common_ticket] AS [t0]
) AS [t1]
GROUP BY [t1].[value], [t1].[value2];