如何汇总重复字段的不同值

时间:2014-02-27 08:20:09

标签: sql sum distinct

我刚刚来到这里,希望我能在这里找到一些帮助。

我遇到以下问题:Fiddle SQL Demo

CREATE TABLE TestData
    (
      [Acct] INT ,
      [Desc] VARCHAR(100) ,
      [OU01] VARCHAR(100) ,
      [OU02] VARCHAR(100) ,
      DEPTHS INT
    );

CREATE TABLE TestDepends
    (
      [Acct] INT ,
      [SecondKey] NUMERIC(9, 0) ,
      [Name] VARCHAR(100) ,
      [COST] NUMERIC(9, 0) ,
      [used] TINYINT ,
      [OU01] VARCHAR(100) ,
      [OU02] VARCHAR(100)
    );



INSERT  INTO TestData
VALUES  ( 1, 'Feld 1', 'OU01', '', 1 );
INSERT  INTO TestData
VALUES  ( 1, 'Feld 1', 'OU01', 'OU02', 2 );
INSERT  INTO TestData
VALUES  ( 1, 'Feld 1', 'OU01', 'OU03', 2 );
INSERT  INTO TestData
VALUES  ( 1, 'Feld 1', 'OU02', '', 1 );


INSERT  INTO TestDepends
VALUES  ( 1, 21, 'Name 1', 120, 0, 'OU01', 'OU02' );
INSERT  INTO TestDepends
VALUES  ( 1, 22, 'Name 2', 50, 0, 'OU01', '' );
INSERT  INTO TestDepends
VALUES  ( 1, 21, 'Name 1', 120, 1, 'OU01', 'OU02' );
INSERT  INTO TestDepends
VALUES  ( 1, 23, 'Name 3', 111, 0, 'OU01', 'OU03' );
INSERT  INTO TestDepends
VALUES  ( 1, 24, 'Name 4', 99, 0, 'OU02', '' );
INSERT  INTO TestDepends
VALUES  ( 1, 25, 'Name 5', 120, 0, 'OU01', 'OU02' );

SELECT  TD.Acct ,
        COUNT(DISTINCT TDS.SecondKey) AS Found ,
        COUNT(DISTINCT CASE WHEN TDS.Used = 1 THEN TDS.SecondKey
                       END) AS Used ,
        TD.OU01 ,
        TD.OU02
FROM    TestData TD
        INNER JOIN TestDepends TDS ON TDS.Acct = TD.Acct
                                      AND ( TD.OU01 = ''
                                            OR ISNULL(TD.OU01, '') = ISNULL(TDS.OU01, '')
                                          )
                                      AND ( TD.OU02 = ''
                                            OR ISNULL(TD.OU02, '') = ISNULL(TDS.OU02, '')
                                          )
WHERE   TD.depths = 2
GROUP BY TD.Acct ,
        TD.OU01 ,
        TD.OU02

正如你在第一个选择i中看到的那样,计算不同的数据(SecondKey),这非常有效。

现在我想总结所有不同数据的所有成本

  SELECT  TD.Acct ,
            SUM(TDS.Cost) AS Cost ,
            SUM(TDS.Cost) AS Cost ,
            TD.OU01 ,
            TD.OU02
    FROM    TestData TD
            INNER JOIN TestDepends TDS ON TDS.Acct = TD.Acct
                                          AND ( TD.OU01 = ''
                                                OR ISNULL(TD.OU01, '') = ISNULL(TDS.OU01, '')
                                              )
                                          AND ( TD.OU02 = ''
                                                OR ISNULL(TD.OU02, '') = ISNULL(TDS.OU02, '')
                                              )
    WHERE   TD.depths = 2
    GROUP BY TD.Acct ,
            TD.OU01 ,
            TD.OU02

正如你所看到的,它总结了“重复”(不是整行是重复的,但是SecondKeys)行。是否可以总结只有不同的SecondKeys的所有成本?

提前致谢。

2 个答案:

答案 0 :(得分:1)

如果我说得对,你需要加入TestDepends而不是加GROUP BY查询以避免重复:

....
FROM    TestData TD
        INNER JOIN 
           ( 
            SELECT Acct,
                   SecondKey,   
                   Name,
                   cost,
                   MAX(used) as used,
                   OU01,
                   OU02
             FROM
             TestDepends 
             GROUP BY Acct,SecondKey,Name,cost, OU01, OU02
           )  
         TDS ON ....

SQL Fiddle demo

答案 1 :(得分:0)

这是另一种方法: 我使用Distinct关键字删除了重复项,而没有选择派生表USED中的t1

  SELECT  TD.Acct ,
            SUM(t1.Cost) AS Cost ,           
            TD.OU01 ,
            TD.OU02
    FROM    TestData TD
            INNER JOIN
              (select distinct Acct, Secondkey, Name, Cost, ou01, ou02 
               from TestDepends) t1 ON t1.Acct = TD.Acct
                                          AND ( TD.OU01 = ''
                                                OR ISNULL(TD.OU01, '') = ISNULL(t1.OU01, '')
                                              )
                                          AND ( TD.OU02 = ''
                                                OR ISNULL(TD.OU02, '') = ISNULL(t1.OU02, '')
                                              )
    WHERE   TD.depths = 2
    GROUP BY TD.Acct ,
            TD.OU01 ,
            TD.OU02

演示:http://sqlfiddle.com/#!3/76470/23