SQL在不同的查询中组合计数和总和

时间:2015-03-04 16:06:47

标签: count sql-server-2012 sum distinct

我在创建查询时遇到一些问题,应该显示部门的各种统计数据 - 案例数量(新的,活动的,关闭的,中止的),预测的目标数和实际数(数量和价值数),不平衡案例数和金额。

下面给出了我需要的好照片的部分代码。 “u”表是一个CTE,我从四个表(案例,用户,备注和付款)中转储联接。

其中一些是求和,减法,乘法或它们的组合。

(1)有没有办法不重复相同的计数,所以我可以在下一栏做减法?

性能不是问题 - 每天都需要统计数据,因此我可以将其排队在午夜运行,但代码会在重复时快速膨胀,而且我不确定它是否真的与众不同,但我必须要有某种排序我可以开始将它与原始数据进行比较之前查询的最终版本。

此外,此时我很难过:

DECLARE @StartExDate datetime 
DECLARE @EndExDate datetime
declare @ThisWeekNow int = 0

SET @StartExDate =
     (SELECT(CONVERT(DATETIME, (SELECT DATEADD(DAY, 1, EOMONTH(GETDATE(),-1))))))
SET @EndExDate =
     (SELECT(CONVERT(DATETIME, (SELECT DATEADD(DAY, 1, EOMONTH(GETDATE()))))))
SET @ThisWeekNow = (SELECT DATEPART(wk, GETDATE()));
WITH u
as
(SELECT tt.team, tt.target, m.client, m.case, m.Created, m.AC_Manager, m.Casetype,
m.Supervisor, m.CAc_Balance, m.OAc_Balance, m.DAc_Balance,m.Notes, m.termsSent,
m.TermsRec, m.Complete, n.Note, clt.Costs, clt.VAT, clt.Type, us.UserCode, us.Manager
    from users us
    inner join tt
    on tt.team = us.Manager
    inner join m
    on us.Usercode = m.Ac_Manager
    inner join n
    ON m.client = n.client and m.Case = n.case
    inner join clt
    on m.Client = clt.Client_Code and m.case = clt.case_No
    where m.Created > '2014-03-01 00:00:000'
    )
Select distinct
     tt.Team
    ,tt.Target
    ,(Select count(*) from users us where Us.Manager = tt.team) as 'Team Target'
    ,(Select count(distinct u.Client+cast(u.case as varchar)) 
      from u where tt.team = u.Manager and
      Complete = 0 and u.Created > @StartExDate and u.Created < @EndExDate
     ) as 'New'
    ,(Select count(distinct u.Client+cast(u.Case as varchar))
      from u where tt.team = u.Manager and Complete = 0) as ' All Live'
    ,(Select count(distinct u.Client+cast(u.Case as varchar))
      from u where tt.team = u.Manager and Complete = 0
             and u.TC_Received is not null) as 'Unsigned'
    ,(Select a - n from
       (Select 
        (Select count(distinct u.Client+cast(u.Case as varchar))
         from u where tt.team = u.Manager and Complete = 0) as a
        ,(Select count(distinct u.Client+cast(u.Case as varchar))
          from u where tt.team = u.Manager and Complete = 0 
          and u.TC_Received is not null) as n
     ) x    ) as 'Worked'
    ,(Select tt.target - (Select a - n from 
       (Select 
        (Select count(distinct u.Client+cast(u.Case as varchar)) 
         from u where tt.team = u.Manager and Complete = 0) as a
       ,(Select count(distinct u.Client+cast(u.Case as varchar))
        from u where tt.team = u.Manager and Complete = 0 
                and u.TC_Rec is not null) as n
       ) x ) p  ) as 'Free capacity'
    ,(Select count(distinct u.Client+cast(u.Case as varchar))
      from u
      where tt.team = u.Manager and Complete = 0
      and patindex('%Signed%',u.note) > 0 ) as 'Signed'
    ,(Select (SUM(u.costs)+SUM(u.VAT)) 
      from u 
      where tt.team = u.Manager and u.Complete = 0
      and patindex('%Signed%',u.note) > 0  and u.TC_Received  is not null
     ) as 'Actual Income'
    ,(Select count(distinct u.Client+cast(u.Case as varchar)) 
      from u where tt.team = u.Manager and Complete = 0
       and patindex('%Aborted%',u.note) > 0 ) as 'Aborts'
    ,(Select count(distinct u.Client+cast(u.Case as varchar))
      from u where tt.team = u.Manager and Complete = 1 ) as 'Completions'
    ,(Select count(distinct u.Client+cast(u.Case as varchar))
      from u where tt.team = u.Manager and Complete = 1 and u.OAc_Balance > 0
     ) as 'Office Balances'
    ,(Select sum (distinct u.OAc_Balance)
      from u where tt.team = u.Manager and Complete = 1 ) as 'OB Amount'
    ,(Select count(distinct u.Client+cast(u.Case as varchar))
      from u where tt.team = u.Manager and Complete = 1 and u.CAc_Balance > 0
    ) as 'Client Balances'
    ,(Select sum (distinct u.CAc_Balance)
      from u where tt.team = u.Manager and Complete = 1) as 'CB Amount'
from tt
join u
on tt.Team = u.Manager
group by tt.Team, tt.Target, u.Usercode
order by tt.Team

这整个程序应该为我们提供公司正在进行的工作和其他统计数据的摘要。看起来像那样:

Team|Target|Team Members|New| All Live|Unsigned|Worked|Free Capacity|Signed|Actual Income|Aborts|Completions|Office Balances|OB Amount|Client Balances|CB Amount|
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
AAA |   360|           8|  2|      247|     223|    24|          336|    71|      85434.6|     4|        165|             22|  1340.84|             87|205777.58|
BBB |   420|           9|  3|      536|     437|    99|          321|   162|    185089.42|     4|        698|             74|   7293.2|            310|942835.87|
CCC |   420|           7|  0|      439|     355|    84|          336|   116|     178517.4|     6|        498|             64|   8604.2|            264|109842.76|
DDD |   420|           9|  0|      725|     290|   435|          -15|   189|      61415.2|     7|        591|             64|  7580.54|            256|569165.07|
EEE |   420|           6|  1|      423|     302|   121|          299|   100|    105229.38|     2|        606|             42|  7679.66|            382|126168.31|
FFF |   420|           8|  1|      498|     450|    48|          372|   123|    124665.68|     3|        367|             36|   3947.7|            257| 93294.52|
GGG |   420|           8|  2|      467|     406|    61|          359|   105|     119822.4|    97|        356|             43|  2143.68|            140|215389.32|
HHH |     1|           1|  0|      186|     150|    36|          -35|    65|        82878|     0|         81|             22|   2255.2|             84|  6863.74|
III |   360|           7|  1|      527|     454|    73|          287|   158|    165513.58|     6|        417|             42|  3242.08|            160|102729.61|
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Total:

第3列到第6列是计数,第7列是减法(目标无符号),可用容量再次减法:目标工作,实际收入是每种情况的值的总和(并且应该计算与列中的相同行数)我也需要插入百分数列,但这是问题1:如何重用数据(即:在当前算术运算中用作参数的前一列中计算)

excel中的快速计数显示每行的值都是大的。并且查询执行时间已经大约100秒。还不是问题,但想降低它。

所以问题(2)有没有办法将与前一列中计算的行相同的行与不同的字符串相加

如果SQL可以在类似逻辑的任何内容中解析'WHERE(x或y)和(x1或x2或x3)',那么程序中的大部分卷积都是不必要的。

我不知道发布U样本是否是一个好主意,因为它是四个表的连接......所以 - 大 - 样本至少要有几百行。消除数据是一种痛苦。

0 个答案:

没有答案