将两列一起添加以从联合查询中获取总和

时间:2014-05-19 18:52:39

标签: sql sql-server tsql

我有以下查询:

 with abby as (SELECT u.Name as 'UserId1'
, count(distinct b.id) as 'Total Count'
, '' as 'ediCount'

FROM abprot.[FC10y].[dbo].[Batch] b with(nolock)

inner join abprot.[FC10y].[dbo].[Principal] u with(nolock) on u.Id = b.CreatorId

where b.CreationDate >= getdate() - 7 
and u.name <> 'abbyyservice'

group by u.Name)

,  edimon as (select userId
, '' as 'Total Count'
, count(*) as 'esubCount'

from ESubmitTrackingTBL

where DateCopied >= getdate() - 7
    and userid <> abbyyservice

group by UserId
)

select * from abby union all select * from edimon

我需要按用户将每个cte的总数加到另一个字段中。我试图包含另一个cte,但收到一条警告,即“UserID”和“Total Count”被指定不止一次 如果我只是将查询作为联合运行,结果如下所示: enter image description here

最终结果应如下所示:

enter image description here

如果我遗漏了任何可能有帮助的事情,请向我道歉 - 如果需要,请告诉我可以添加的内容以澄清这一点。

3 个答案:

答案 0 :(得分:1)

我省去了CTE,只使用派生表。

为了避免在查询或存储过程执行跨越日期边界时出现问题,我更喜欢使用T-SQL变量并为整个运行建立一致的 Now-ness 。越过午夜边界并且今天有#34;如果你不小心处理事情,那么中途变化会导致......微妙的问题。不要问我怎么知道这个。

因此...

declare @now         datetime = current_timestamp      -- current instant in time
declare @today       date     = @now                   -- today's date
declare @cutoff_date date     = dateadd(day,-7,@today) -- 1 week ago

您的cutoff_date值可能会有所不同,具体取决于您的规范是否需要查看之前的7 日历日或之前的168小时(7 * 24)相对于当前时刻)

所以我的查询看起来像这样:

declare @now         datetime = current_timestamp      -- current instant in time
declare @today       date     = @now                   -- today's date
declare @cutoff_date date     = dateadd(day,-7,@today) -- 1 week ago

select user_id        =      x.user_id       ,
       total_count    = sum( x.total_count ) ,
       edi_count      = sum( x.edi_count   ) ,
       grand_total    = sum( x.total_count )
                      + sum( x.edi_count   ) ,
       esub_count_pct = 100.0
                      * sum( x.edi_count )
                      / (   sum( x.total_count )
                          + sum( x.edi_count   )
                        )
from (   select user_id     = u.Name ,
                total_count = count( distinct b.id ) ,
                esub_count  = 0
         from abprot.FC10y.dbo.Batch     b
         join abprot.FC10y.dbo.Principal u on u.Id   =  b.Creator.Id
                                          and u.name <> 'abbyyservice'
         where b.CreationDate >= @cutoff_date
         group by u.Name
       UNION ALL
         select user_id     = t.userId ,
                total_count = 0        ,
                esub_count  = 1
         from dbo.ESubmitTrackingTBL t
         where t.DateCopied >= @cutoff_date
     ) x
group by x.user_id 

答案 1 :(得分:0)

您的查询确实看起来很糟糕。我假设您只想要上一次CTE中的内容。

计算非常简单。我有点惊讶的是,可以将这个SQL组合在一起的人不会想出最终的计算结果:

with abby as (
      SELECT u.Name as UserId, count(distinct b.id) as [Total Count], '' as ediCount
      FROM abprot.[FC10y].[dbo].[Batch] b with(nolock) inner join
           abprot.[FC10y].[dbo].[Principal] u with(nolock)
           on u.Id = b.CreatorId
      where b.CreationDate >= getdate() - 7 and u.name <> 'abbyyservice'
      group by u.Name
    ), 
    edimon as (
     select userId, '' as [Total Count], count(*) as esubCount
     from ESubmitTrackingTBL
     where DateCopied >= getdate() - 7 and userid <> abbyyservice
    )
select userId, [Total Count], esubcount,  ([Total Count] + esubcount) as Total,
       100.0 * esubcount / ([Total Count] + esubcount) as [percent edisubcount] 
from edimon;

建议:避免对标识符使用单引号。最好使用不需要转义的名称(例如userid)。如果你这样做,那就用方括号。

编辑:

你的问题可能比你想要的更容易。尝试使用group by with rollup

SELECT u.Name as UserId, count(distinct b.id) as [Total Count], '' as ediCount,
        ([Total Count] + esubcount) as Total,
       100.0 * esubcount / ([Total Count] + esubcount) as [percent edisubcount] 
FROM abprot.[FC10y].[dbo].[Batch] b with(nolock) inner join
     abprot.[FC10y].[dbo].[Principal] u with(nolock)
     on u.Id = b.CreatorId
where b.CreationDate >= getdate() - 7 and u.name <> 'abbyyservice'
group by u.Name with rollup;

答案 2 :(得分:0)

with abby as (
      SELECT u.Name as UserId, count(distinct b.id) as [Total Count], '' as ediCount
      FROM abprot.[FC10y].[dbo].[Batch] b with(nolock) inner join
           abprot.[FC10y].[dbo].[Principal] u with(nolock)
           on u.Id = b.CreatorId
      where b.CreationDate >= getdate() - 7 and u.name <> 'abbyyservice'
      group by u.Name
    ), 
    edimon as (
     select userId, '' as [Total Count], count(*) as esubCount
     from ESubmitTrackingTBL
     where DateCopied >= getdate() - 7 and userid <> 'abbyyservice'
    )

select UserId, 
  TotalCount, 
  esubCount, 
  convert(decimal, esubCount)/(convert(decimal, TotalCount) + convert(decimal, esubCount)) percentesubCount
from (select * from abby
      union
      select * from edimon) x