减少广泛的查询

时间:2014-03-05 00:31:04

标签: sql date

我正在从我们的数据库编译工作票的历史数据。我使用过去12个月的最后一天。在那一天,我想看看有多少票是开放的(日期是在月的最后一天之前或之前创建的,而日期是在最后一天之后关闭或为空)。截至本月最后一天,有多少门票已开放超过30天,以及当月关闭了多少张门票。

我的查询返回前12个月中每个月的4个值。我开发了一个查询,我一次收集数据一个月,然后使用Union来获取下个月等等。

我现在正在尝试开发一个查询,每个月收集的数据都不需要联盟。这是一个样本月份。

Select Convert(Varchar,
               DATEADD(day,
                       -1,
                       DATEADD(mm, DATEDIFF(m, 0, GETDATE()) - 12, 0)),
               101) as [ DateRun ],
       t1. [ data1 ],
       sum(t2. [ data2 ] + t3. [ data3 ]) as [ Total Open ],
       (t1. [ data1 ] + 0.0) /
       sum((t2. [ data2 ] + 0.0) + (t3. [ data3 ] + 0.0)) as [ Percent Aged Over 30 Days ]
  From (Select count(*) as [ data1 ]
          From (Select s.ticketnumber, s.datecreated, s.dateclosed
                  from mydb2 i
                 inner join mydb1 s
                    on i.ticketNumber = s.ticketNumber
                 Where datediff(dd,
                                s.dateCreated,
                                DATEADD(day,
                                        -1,
                                        DATEADD(mm,
                                                DATEDIFF(m, 0, GETDATE()) - 12,
                                                0))) > '30'
                   AND (s.dateclosed is null OR
                        s.dateclosed >=
                        DATEADD(day,
                                -1,
                                DATEADD(mm, DATEDIFF(m, 0, GETDATE()) - 12, 0)))
                 Group by s.ticketNumber, s.datecreated, s.dateclosed) as tb1) as t1,
       (Select count(*) as [ data2 ]
          From (Select s.ticketnumber, s.datecreated, s.dateclosed
                  from mydb2 i
                 inner join mydb1 s
                    on i.ticketNumber = s.ticketNumber
                 Where s.datecreated <
                       DATEADD(day,
                               -1,
                               DATEADD(mm, DATEDIFF(m, 0, GETDATE()) - 12, 0))
                   AND (s.dateclosed is null OR
                       s.dateclosed >=
                       DATEADD(day,
                                -1,
                                DATEADD(mm, DATEDIFF(m, 0, GETDATE()) - 12, 0)))
                 Group by s.ticketNumber, s.datecreated, s.dateclosed) as tb2) as t2,
       (Select count(*) as [ data3 ]
          From (Select s.ticketnumber, s.datecreated, s.dateclosed
                  from mydb2 i
                 inner join mydb1 s
                    on i.ticketNumber = s.ticketNumber
                 Where s.dateclosed >
                       DATEADD(day,
                               -1,
                               DATEADD(mm, DATEDIFF(m, 0, GETDATE()) - 13, 1))
                   AND s.dateclosed <
                       DATEADD(day,
                               -1,
                               DATEADD(mm, DATEDIFF(m, 0, GETDATE()) - 12, 0))
                 Group by s.ticketNumber, s.datecreated, s.dateclosed) as tb3) as t3
 Group by t1. [ data1 ]

通过研究,我能够使用以下代码在过去12个月内获得以下代码,但我无法更新上述代码,因此我不需要12个联盟。

Select dateadd(month, 1 + datediff(month, 0, s.datecreated), -1) as [ Date Run ]
  from mydb1 s
 Where s.datecreated >
       DATEADD(day, -1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) - 12, 0))
   and s.datecreated <
       DATEADD(day, -1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0))
 Group by dateadd(month, 1 + datediff(month, 0, s.datecreated), -1)
 Order by 1

1 个答案:

答案 0 :(得分:1)

您可以尝试使用CTE生成月份,将其用作查询的基础,然后将聚合作为子查询运行。换句话说,就像这样:

with Months( ClosingDate, n ) as
(
    select DATEADD(dd, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) as ClosingDate, 1 as n
    union all
    select DATEADD(dd, -1, DATEADD(mm, -1, DATEADD(dd, 1, ClosingDate))), n + 1
    from Months
    where n < 12
) 
select DATENAME(mm, m.ClosingDate) MonthLabel, DATEADD(dd, 1 - DATEPART(dd, m.ClosingDate), m.ClosingDate) OpeningDate, m.ClosingDate,
ISNULL((select COUNT(*)
    from mydb1 s
    where s.dateCreated < DATEADD(dd, -29, m.ClosingDate)
      and (s.dateClosed is null or s.dateClosed >= DATEADD(dd, 1, m.ClosingDate))
), 0) [Total Open 30+ Days]
from Months m

为清楚起见,我只包含了一个聚合,并省略了您的连接和分组。