结合今天和同一列的今天之前的总数导致一个SQL查询

时间:2012-10-12 06:17:26

标签: sql sql-server-2008

declare @temp table (ddate datetime)

insert @temp
select DATEDIFF(d,0,CONVERT(smalldatetime,'09/30/2012')  -Number)
from master..spt_values
where type='p' and number < DatePart(d,'09/30/2012')
order by 1

DECLARE @DeptCode int =1 

- 显示特定日期数据

select   ComplaintMedia_Abbri,
ddate,COUNT(ComplaintMedia)  as c
from    Complaint 
INNER JOIN @temp 
ON convert(datetime,convert(varchar(10),ComplaintDate,101),101)=convert(datetime,convert(varchar(10),ddate,101),101)        
WHERE isnull(Receivedby_Dept,Relatesto_Dept)=1     
group by ComplaintMedia_Abbri,ddate
order by ddate,ComplaintMedia_Abbri

- 显示日期特定日期数据总计

select   ComplaintMedia_Abbri,ddate,
    COUNT(ComplaintMedia_Abbri)  as c
    from    Complaint 

    INNER JOIN @temp 
    ON convert(datetime,convert(varchar(10),ComplaintDate,101),101)<=convert(datetime,convert(varchar(10),ddate,101),101)       

    WHERE isnull(Receivedby_Dept,Relatesto_Dept)=1     
    group by ComplaintMedia_Abbri,ddate

我想在一个SQL查询结果中显示运行总计直到日期和今天(那天)记录.....

在加入时有两个不同的条件。

预期结果应如下所示

expected result for above query

修改: 我可以通过使用这些查询并加入它们来实现此结果,但我想在一个查询中执行此任务而不是两个查询

我目前的方式可以在这里查看。抱歉这么长的问题,但我认为有必要理解问题..

declare @temp table (ddate datetime)

insert @temp
select DATEDIFF(d,0,CONVERT(smalldatetime,'09/30/2012')  -Number)
from master..spt_values
where type='p' and number < DatePart(d,'09/30/2012')
order by 1

--select * from @temp

SELECT * FROM 

        (select ddate,ISNULL(L,0) AS Letter,
          ISNULL(P,0) AS Phone,
          ISNULL(E,0) AS Email,
          ISNULL(W,0) AS WEB 
          FROM 
          (
            select   ComplaintMedia_Abbri,
          ddate,COUNT(ComplaintMedia)  as c
            from    Complaint 
            INNER JOIN @temp 
            ON convert(datetime,convert(varchar(10),ComplaintDate,101),101)=convert(datetime,convert(varchar(10),ddate,101),101)        
            WHERE isnull(Receivedby_Dept,Relatesto_Dept)=1     
            group by ComplaintMedia_Abbri,ddate
    ) p 

    pivot (SUM(c) FOR ComplaintMedia_Abbri IN (E,W,L,P)) AS pvt 

    ) AS [A]

    INNER JOIN 

    (
    select ddate,ISNULL(L,0) AS LetterTot,
      ISNULL(P,0) AS PhoneTot,
      ISNULL(E,0) AS EmailTot,
      ISNULL(W,0) AS WEBTot
      FROM 
      (

    select   ComplaintMedia_Abbri,ddate,
    COUNT(ComplaintMedia_Abbri)  as c
    from    Complaint 

    INNER JOIN @temp 
    ON convert(datetime,convert(varchar(10),ComplaintDate,101),101)<=convert(datetime,convert(varchar(10),ddate,101),101)       

    WHERE isnull(Receivedby_Dept,Relatesto_Dept)=1     
    group by ComplaintMedia_Abbri,ddate
    ) p 

   pivot (SUM(c) FOR ComplaintMedia_Abbri IN (E,W,L,P)) AS pvt



    ) AS [B]

   ON A.ddate=B.ddate

   order by A.ddate

1 个答案:

答案 0 :(得分:2)

我已经修改了注释中给出的SQL Fiddle,它会为您提供所需的输出,前提是您已经拥有每日总数:

http://www.sqlfiddle.com/#!6/09168/2

DECLARE @startDate datetime
DECLARE @endDate datetime

SELECT @startDate = '2012-10-08'
SELECT @endDate = '2012-10-12'

SELECT
    DT1.ddate,
    DT1.phone,
    DT1.letter,
    DT1.email,
    DT1.web,
    SUM(DT2.phone) phoneTotal,
    SUM(DT2.letter) letterTotal,
    SUM(DT2.email) emailTotal,
    SUM(DT2.web) webTotal 
  FROM
    DailyTotals DT1
    LEFT JOIN DailyTotals DT2 ON DT1.ddate >= DT2.ddate AND DT2.ddate >= @startDate
  WHERE
    DT1.ddate <= @endDate
  GROUP BY
  DT1.ddate,
  DT1.phone,
  DT1.letter,
  DT1.email,
  DT1.web

如果要将其作为一个语句,则需要将DailyTotals替换为可提供每日总计的子查询。但是,我建议制作一个名为DailyTotals的视图并使用它。

编辑:

您可以使用CTE生成日期范围而不是临时表。我已经修改了你认为可以与CTE而不是@temp一起工作的完整查询。我无法测试它。如果这不起作用,请使用您的架构创建一个SLQ小提琴,我会再试一次。

WITH Dates AS
(
    SELECT CONVERT(date, MIN(ComplaintTime)) AS ddate,
    MAX(ComplaintTime) as EndDate
    FROM
      Complaints
    UNION ALL
    SELECT DATEADD(DAY, 1, ddate), EndDate
    FROM Dates
    WHERE DATEADD(DAY, 1, ddate) <= EndDate
    )

SELECT * FROM 

        (select ddate,ISNULL(L,0) AS Letter,
          ISNULL(P,0) AS Phone,
          ISNULL(E,0) AS Email,
          ISNULL(W,0) AS WEB 
          FROM 
          (
            select   ComplaintMedia_Abbri,
          ddate,COUNT(ComplaintMedia)  as c
            from    Complaint 
            INNER JOIN Dates
            ON convert(date,ComplaintDate)=ddate OPTION (MAXRECURSION 500)
            WHERE isnull(Receivedby_Dept,Relatesto_Dept)=1     
            group by ComplaintMedia_Abbri,ddate
    ) p 

    pivot (SUM(c) FOR ComplaintMedia_Abbri IN (E,W,L,P)) AS pvt 

    ) AS [A]

    INNER JOIN 

    (
    select ddate,ISNULL(L,0) AS LetterTot,
      ISNULL(P,0) AS PhoneTot,
      ISNULL(E,0) AS EmailTot,
      ISNULL(W,0) AS WEBTot
      FROM 
      (

    select   ComplaintMedia_Abbri,ddate,
    COUNT(ComplaintMedia_Abbri)  as c
    from    Complaint 

    INNER JOIN Dates OPTION (MAXRECURSION 0)
    ON CONVERT(date,ComplaintDate) <= ddate OPTION (MAXRECURSION 0)

    WHERE isnull(Receivedby_Dept,Relatesto_Dept)=1     
    group by ComplaintMedia_Abbri,ddate
    ) p 

   pivot (SUM(c) FOR ComplaintMedia_Abbri IN (E,W,L,P)) AS pvt



    ) AS [B]

   ON A.ddate=B.ddate

   order by A.ddate