SQL Sum计数不止一次

时间:2011-11-22 10:27:30

标签: sql sql-server sql-server-2008 tsql

我遇到这个困难。此查询在计算总和时工作正常,直到我将第一个内部联接放入。在表tbl_companies中,每个公司有多个条目,例如表格可能如下所示:

priority      company     externalip
1             bla         9.9.9.9
1             bla         3.3.3.3
1             company2    3.56.6.6

在下面的查询中,总和(当公司有多个条目时计算为TotalWithoutNew和TotalAllId加倍,如果有三个条目则计算三倍等。我想要它做的只是从中恢复优先级表tbl_companies

SELECT b.company, 
       b.priority, 
       i.concom, 
       Coalesce (SUM(CASE 
                       WHEN c.category_id = '30' THEN 0 
                       ELSE t.logmins 
                     END), 0)       AS totalwithoutnew, 
       Coalesce (SUM(t.logmins), 0) AS totalallid 
FROM   helpdesk3.dbo.inquiry AS i 
       INNER JOIN [Check].[dbo].[tbl_companies] AS b 
         ON i.concom = b.company COLLATE sql_latin1_general_cp1_ci_as 
       INNER JOIN timelog AS t 
         ON t.inquiry_id = i.inquiry_id 
       INNER JOIN prod AS p 
         ON i.prod_id = p.prod_id 
       INNER JOIN category AS c 
         ON p.category_id = c.category_id 
WHERE  ( Datepart(yyyy, escdate) = 2011 ) 
GROUP  BY i.concom, 
          b.company, 
          b.priority 
ORDER  BY totalwithoutnew DESC, 
          b.priority DESC    

3 个答案:

答案 0 :(得分:2)

您应该拆分查询以避免来自tbl_companies的多个结果。

select distinct b.company, 
       b.priority, 
       x.concom, 
       x.totalwithoutnew, 
       x.totalallid 
FROM    (
            SELECT i.concom, 
                   Coalesce (SUM(CASE 
                                   WHEN c.category_id = '30' THEN 0 
                                   ELSE t.logmins 
                                 END), 0)       AS totalwithoutnew, 
                   Coalesce (SUM(t.logmins), 0) AS totalallid 
            FROM   helpdesk3.dbo.inquiry AS i 
                   INNER JOIN timelog AS t 
                     ON t.inquiry_id = i.inquiry_id 
                   INNER JOIN prod AS p 
                     ON i.prod_id = p.prod_id 
                   INNER JOIN category AS c 
                     ON p.category_id = c.category_id 
            WHERE  ( Datepart(yyyy, escdate) = 2011 ) 
            GROUP  BY i.concom 
            ) x
INNER JOIN [Check].[dbo].[tbl_companies] AS b 
          ON x.concom = b.company COLLATE sql_latin1_general_cp1_ci_as 

ORDER  BY x.totalwithoutnew DESC, 
          b.priority DESC  

答案 1 :(得分:0)

INQUIRY表到tbl_companies表的连接将生成一组三行(当有三家公司时),因此下一次加入TIMELOG表是每个TIMELOG.LOGMINS列也会有三个值 - 因此COALESCE (SUM(CASE WHEN C.CATEGORY_ID = '30' THEN 0 ELSE t.LOGMINS END), 0) AS TotalWithoutNew计算将增加三倍。

select 
...
FROM helpdesk3.dbo.INQUIRY AS i 
inner join [Check].[dbo].[tbl_companies] As B ON i.CONCOM = B.company COLLATE SQL_Latin1_General_CP1_CI_AS 
INNER JOIN TIMELOG AS t ON t.INQUIRY_ID = i.INQUIRY_ID 
...

如果您希望单个公司出现在选择中而不是乘以总和,则从where子句和group by子句中删除对tbl_companies的连接。沿着这些方向的东西应该有用(虽然我不知道数据的确切结构我不能确定):

select 
(select company from [tbl_companies] where company = i.concom) as company,
(select priority from [tbl_companies] where company = i.concom) as priority,
...
FROM helpdesk3.dbo.INQUIRY AS i 
INNER JOIN TIMELOG AS t ON t.INQUIRY_ID = i.INQUIRY_ID 
...

答案 2 :(得分:0)

有几种方法可以做到这一点。假设优先级,公司和externalip唯一识别tbl_companies记录,我建议:

SELECT b.company, 
       b.priority, 
       i.concom, 
       Coalesce (SUM(CASE 
                       WHEN c.category_id = '30' THEN 0 
                       ELSE t.logmins 
                     END), 0) / COUNT(DISTINCT b.externalip)  AS totalwithoutnew, 
       Coalesce (SUM(t.logmins), 0) / COUNT(DISTINCT b.externalip) AS totalallid 
FROM   helpdesk3.dbo.inquiry AS i 
       INNER JOIN [Check].[dbo].[tbl_companies] AS b 
         ON i.concom = b.company COLLATE sql_latin1_general_cp1_ci_as 
       INNER JOIN timelog AS t 
         ON t.inquiry_id = i.inquiry_id 
       INNER JOIN prod AS p 
         ON i.prod_id = p.prod_id 
       INNER JOIN category AS c 
         ON p.category_id = c.category_id 
WHERE  ( Datepart(yyyy, escdate) = 2011 ) 
GROUP  BY i.concom, 
          b.company, 
          b.priority 
ORDER  BY totalwithoutnew DESC, 
          b.priority DESC