显示没有选定列值的日期

时间:2015-12-02 09:35:02

标签: sql sql-server tsql sql-server-2012

我有以下查询,该查询计算过去10天内特定日期创建的项目数

SELECT 
    CONVERT (DATE, CreatedDate_6258638D_B885_AB3C_E316_D00782B8F688) AS 'Logged Date', 
    Count (*) AS 'Total' 
FROM   
    MTV_System$WorkItem$Incident 
WHERE  
    CreatedDate_6258638D_B885_AB3C_E316_D00782B8F688 >= DATEADD(DAY, DATEDIFF(DAY, 0, Getdate()) - 10, 0) 
GROUP BY 
    CONVERT(DATE, CreatedDate_6258638D_B885_AB3C_E316_D00782B8F688) 

如何显示没有值的日期(即获取过去10天的每个日期值,如果有数据则返回计数,如果没有,则返回0)。使用SQL Server 2012。

3 个答案:

答案 0 :(得分:3)

您可以编写一个递归cte,将过去10天的日期输入表格,如下所示:

WITH TableA (StartDate) AS (SELECT  DATEADD(DAY, DATEDIFF(DAY, 0, Getdate()) - 10, 0)),
q as (
    SELECT  StartDate
            , Number = 0
    FROM    TableA
    UNION ALL 
    SELECT  DATEADD(d,1,StartDate) 
            , Number = Number + 1
    FROM    q
    WHERE   10 > Number )

然后将q与原始查询连接,以获取每个日期的行。

    select q.StartDate, yourtable.Total from q
    left join (
        SELECT 
            CONVERT (DATE, CreatedDate_6258638D_B885_AB3C_E316_D00782B8F688) AS 'Logged Date', 
            Count (*) AS 'Total' 
        FROM   
            MTV_System$WorkItem$Incident 
        WHERE  
            CreatedDate_6258638D_B885_AB3C_E316_D00782B8F688 >= DATEADD(DAY, DATEDIFF(DAY, 0, Getdate()) - 10, 0) 
        GROUP BY 
            CONVERT(DATE, CreatedDate_6258638D_B885_AB3C_E316_D00782B8F688) 
            ) as yourtable on [Logged Date] = q.StartDate

答案 1 :(得分:2)

类似于BeanFrog的回答,但更短一点

-- sample data for testing
declare @MTV_System$WorkItemIncident table (
    [CreatedDate_6258638D_B885_AB3C_E316_D00782B8F688] DATE,
    [Total] INT
);
INSERT INTO @MTV_System$WorkItemIncident VALUES ('2015-11-23', 23);
INSERT INTO @MTV_System$WorkItemIncident VALUES ('2015-11-21', 21);
INSERT INTO @MTV_System$WorkItemIncident VALUES ('2015-11-30', 30);

-- now the query
WITH TableA (LoggedDate) AS (
    SELECT TOP 10 CONVERT (DATE, DATEADD(DAY, number * -1, GETDATE())) AS 'LoggedDate' 
    FROM master.dbo.spt_values 
    WHERE name IS NULL
)
SELECT  TableA.[LoggedDate],
        SUM(ISNULL(Data.Total, 0)) AS 'LoggedCount'
FROM TableA
    LEFT JOIN @MTV_System$WorkItemIncident AS Data ON CONVERT (DATE, CreatedDate_6258638D_B885_AB3C_E316_D00782B8F688) = TableA.[LoggedDate]
GROUP BY TableA.[LoggedDate]

答案 2 :(得分:0)

并非BeanFrog的答案有任何问题,但如果你不想使用递归cte,你可以这样做:

CREATE TABLE MTV_System$WorkItem$Incident (id int PRIMARY KEY, CreatedDate_6258638D_B885_AB3C_E316_D00782B8F688 datetime)
INSERT INTO MTV_System$WorkItem$Incident VALUES (1, '20151201')
INSERT INTO MTV_System$WorkItem$Incident VALUES (2, '20151126')
INSERT INTO MTV_System$WorkItem$Incident VALUES (3, '20151127')
INSERT INTO MTV_System$WorkItem$Incident VALUES (4, '20151127')

SELECT 
    ReportDate AS 'Logged Date', 
    Count (CreatedDate_6258638D_B885_AB3C_E316_D00782B8F688) AS 'Total'
FROM (
    SELECT DATEADD(DAY, DATEDIFF(DAY, 0, Getdate()) - 0, 0) AS ReportDate
    UNION SELECT DATEADD(DAY, DATEDIFF(DAY, 0, Getdate()) - 1, 0)
    UNION SELECT DATEADD(DAY, DATEDIFF(DAY, 0, Getdate()) - 2, 0)
    UNION SELECT DATEADD(DAY, DATEDIFF(DAY, 0, Getdate()) - 3, 0)
    UNION SELECT DATEADD(DAY, DATEDIFF(DAY, 0, Getdate()) - 4, 0)
    UNION SELECT DATEADD(DAY, DATEDIFF(DAY, 0, Getdate()) - 5, 0)
    UNION SELECT DATEADD(DAY, DATEDIFF(DAY, 0, Getdate()) - 6, 0)
    UNION SELECT DATEADD(DAY, DATEDIFF(DAY, 0, Getdate()) - 7, 0)
    UNION SELECT DATEADD(DAY, DATEDIFF(DAY, 0, Getdate()) - 8, 0)
    UNION SELECT DATEADD(DAY, DATEDIFF(DAY, 0, Getdate()) - 9, 0)
    UNION SELECT DATEADD(DAY, DATEDIFF(DAY, 0, Getdate()) - 10, 0)
) AS Dates
LEFT JOIN MTV_System$WorkItem$Incident ON ReportDate = CONVERT(DATE, CreatedDate_6258638D_B885_AB3C_E316_D00782B8F688)
GROUP BY ReportDate

BeanFrog的答案的优点是能够轻松改变天数。