联合和聚合函数

时间:2016-03-07 14:20:26

标签: sql sql-server-2014

我有两个与UNION操作结合的查询:

DECLARE @BeginningOfYear AS date

SET @BeginningOfYear = smalldatetimefromparts(YEAR(GETDATE()), 1, 1, 0, 0)

SELECT

  groupname,
  SUM(CASE WHEN ticketcreateddate > DATEADD(DAY, -7, GETDATE()) THEN 1 ELSE 0 END) AS 'LW',
  SUM(CASE WHEN ticketcreateddate > @BeginningOfYear THEN 1 ELSE 0 END) AS 'YTD',
  CAST(COUNT(*) / DATEDIFF(WEEK, @BeginningOfYear, GETDATE()) AS float) AS 'Average Per Week'

FROM zendeskticketexport

WHERE groupname IN ('DC Web Ops', 'DC Maintenance', 'DC QC1')

GROUP BY groupname

UNION

SELECT

  groupname,
  SUM(CASE WHEN ticketcreateddate > DATEADD(DAY, -7, GETDATE()) THEN 1 ELSE 0 END) AS 'LW',
  SUM(CASE WHEN ticketcreateddate > @BeginningOfYear THEN 1 ELSE 0 END) AS 'YTD',
  CAST(COUNT(*) / DATEDIFF(WEEK, @BeginningOfYear, GETDATE()) AS float) AS 'Average Per Week'

FROM zendeskunclosedticketexport

WHERE groupname IN ('DC Web Ops', 'DC Maintenance', 'DC QC1')

GROUP BY groupname

此查询的结果:

groupname       LW      YTD     Average Per Week
DC Maintenance  179     1831    184
DC QC1          85      647     64
DC Web Ops      69      400     40
DC Maintenance  68      86      8
DC QC1          18      20      2
DC Web Ops      53      80      8

我想在结果查询中总结每个组的数字,但我不知道如何在不创建临时表的情况下执行此操作...我想避免这样做,因为我觉得这对查询来说是不必要的复杂化。我正在考虑做一个相当完整的加入......这会更合适,还是一个临时表可以继续这个?

编辑:我应该提到两个表上都有一个唯一标识符 - 让我们称之为ID - 但是如果表上存在一个ID,那么它就不存在 - 这就是为什么我认为完全加入可能是合适的。

2 个答案:

答案 0 :(得分:1)

你可以使用像这样的派生表

select p.groupname,sum(p.LW) as LW,sum(p.YTD) as YTD,sum(p.[Average Per Week]) as Average Per Week from
  (SELECT

  groupname,
  SUM(CASE WHEN ticketcreateddate > DATEADD(DAY, -7, GETDATE()) THEN 1 ELSE 0 END) AS 'LW',
  SUM(CASE WHEN ticketcreateddate > @BeginningOfYear THEN 1 ELSE 0 END) AS 'YTD',
  CAST(COUNT(*) / DATEDIFF(WEEK, @BeginningOfYear, GETDATE()) AS float) AS 'Average Per Week'

FROM zendeskticketexport

WHERE groupname IN ('DC Web Ops', 'DC Maintenance', 'DC QC1')

GROUP BY groupname

UNION

SELECT

  groupname,
  SUM(CASE WHEN ticketcreateddate > DATEADD(DAY, -7, GETDATE()) THEN 1 ELSE 0 END) AS 'LW',
  SUM(CASE WHEN ticketcreateddate > @BeginningOfYear THEN 1 ELSE 0 END) AS 'YTD',
  CAST(COUNT(*) / DATEDIFF(WEEK, @BeginningOfYear, GETDATE()) AS float) AS 'Average Per Week'

FROM zendeskunclosedticketexport

WHERE groupname IN ('DC Web Ops', 'DC Maintenance', 'DC QC1')

GROUP BY groupname) as p
group by p.groupname

或者您可以使用 CTE

;with cte
(SELECT

      groupname,
      SUM(CASE WHEN ticketcreateddate > DATEADD(DAY, -7, GETDATE()) THEN 1 ELSE 0 END) AS 'LW',
      SUM(CASE WHEN ticketcreateddate > @BeginningOfYear THEN 1 ELSE 0 END) AS 'YTD',
      CAST(COUNT(*) / DATEDIFF(WEEK, @BeginningOfYear, GETDATE()) AS float) AS 'Average Per Week'

    FROM zendeskticketexport

    WHERE groupname IN ('DC Web Ops', 'DC Maintenance', 'DC QC1')

    GROUP BY groupname

    UNION

    SELECT

      groupname,
      SUM(CASE WHEN ticketcreateddate > DATEADD(DAY, -7, GETDATE()) THEN 1 ELSE 0 END) AS 'LW',
      SUM(CASE WHEN ticketcreateddate > @BeginningOfYear THEN 1 ELSE 0 END) AS 'YTD',
      CAST(COUNT(*) / DATEDIFF(WEEK, @BeginningOfYear, GETDATE()) AS float) AS 'Average Per Week'

    FROM zendeskunclosedticketexport

    WHERE groupname IN ('DC Web Ops', 'DC Maintenance', 'DC QC1')

    GROUP BY groupname) 
select p.groupname,sum(p.LW) as LW,sum(p.YTD) as YTD,sum(p.[Average Per Week]) as Average Per Week 
from from cte GROUP BY groupname

答案 1 :(得分:0)

我是否让你在这里,你想要两张桌子的总金额和数量?

SELECT
  groupname,
  SUM(CASE WHEN ticketcreateddate > DATEADD(DAY, -7, GETDATE()) THEN 1 ELSE 0 END) AS 'LW',
  SUM(CASE WHEN ticketcreateddate > @BeginningOfYear THEN 1 ELSE 0 END) AS 'YTD',
  CAST(COUNT(*) / DATEDIFF(WEEK, @BeginningOfYear, GETDATE()) AS float) AS 'Average Per Week'
(
  SELECT groupname, ticketcreateddate
  FROM zendeskunclosedticketexport
  WHERE groupname IN ('DC Web Ops', 'DC Maintenance', 'DC QC1')
  UNION ALL
  SELECT groupname, ticketcreateddate
  FROM zendeskunclosedticketexport
  WHERE groupname IN ('DC Web Ops', 'DC Maintenance', 'DC QC1')
)
GROUP BY groupname