在GROUP BY子句中使用EXISTS

时间:2019-05-16 08:11:37

标签: sql sql-server tsql select

是否可以执行以下操作:
我有一个看起来像这样的表:

declare @tran_TABLE TABLE(
EOMONTH DATE,
AccountNumber INT,
CLASSIFICATION_NAME VARCHAR(50),
Value Float

)

INSERT INTO @tran_TABLE VALUES('2018-11-30','123','cat1',10)
INSERT INTO @tran_TABLE VALUES('2018-11-30','123','cat1',15)
INSERT INTO @tran_TABLE VALUES('2018-11-30','123','cat1',5 )
INSERT INTO @tran_TABLE VALUES('2018-11-30','123','cat2',10)
INSERT INTO @tran_TABLE VALUES('2018-11-30','123','cat3',12)
INSERT INTO @tran_TABLE VALUES('2019-01-31','123','cat1',5 )
INSERT INTO @tran_TABLE VALUES('2019-01-31','123','cat2',10)
INSERT INTO @tran_TABLE VALUES('2019-01-31','123','cat2',15)
INSERT INTO @tran_TABLE VALUES('2019-01-31','123','cat3',5 )
INSERT INTO @tran_TABLE VALUES('2019-01-31','123','cat3',2 )
INSERT INTO @tran_TABLE VALUES('2019-03-31','123','cat1',15)


EOMONTH     AccountNumber   CLASSIFICATION_NAME     Value
2018-11-30  123                     cat1                10
2018-11-30  123                     cat1                15
2018-11-30  123                     cat1                5
2018-11-30  123                     cat2                10
2018-11-30  123                     cat3                12
2019-01-31  123                     cat1                5
2019-01-31  123                     cat2                10
2019-01-31  123                     cat2                15
2019-01-31  123                     cat3                5
2019-01-31  123                     cat3                2
2019-03-31  123                     cat1                15

我想产生一个结果,它会检查每个月是否每个AccountNumber(在这种情况下只有一个)是否存在CLASSIFICATION_NAME cat1,cat2,cat3。
如果该月所有3个都存在,则返回1,但是如果缺少则返回0。

结果应如下所示:

EOMONTH     AccountNumber   CLASSIFICATION_NAME
2018-11-30    123                   1                               
2019-01-31    123                   1                       
2019-03-31    123                   0   

但是我想尽可能紧凑地做到这一点,而不必首先创建一个按CLASSIFICATION_NAME,EOMONTH和AccountNumber将所有内容分组的表,然后从该表中进行选择。
例如,在下面的伪代码中,是否可以使用EXISTS语句进行分组依据?

SELECT 
    EOMONTH
    ,AccountNumber
    ,CASE WHEN EXISTS (CLASSIFICATION_NAME = 'cat1' AND 'cat2' AND 'cat3') THEN 1 ELSE 0 end 
    ,SUM(Value) AS totalSpend
FROM @tran_TABLE
GROUP BY 
    EOMONTH
    ,AccountNumber

4 个答案:

答案 0 :(得分:7)

您可以通过对满足此条件的不同分类(每组)进行计数来模拟这种行为:

SELECT 
    EOMONTH
    ,AccountNumber
    ,CASE COUNT(DISTINCT CASE WHEN classification_name IN ('cat1', 'cat2', 'cat3') THEN classification_name END) 
          WHEN 3 THEN 1 
          ELSE 0 
     END
    ,SUM(Value) AS totalSpend
FROM @tran_TABLE
GROUP BY 
    EOMONTH
    ,AccountNumber

答案 1 :(得分:1)

试试这个-

SELECT EOMONTH, 
AccountNumber,
CASE 
    WHEN COUNT(DISTINCT CLASSIFICATION_NAME) = 3 THEN 1
    ELSE 0
END CLASSIFICATION_NAME
FROM @tran_TABLE
GROUP BY EOMONTH,AccountNumber

输出为-

2018-11-30  123 1
2019-01-31  123 1
2019-03-31  123 0

答案 2 :(得分:1)

这样查询。您可以计算不同的值。 当您计算唯一值时,请在“ Three_Unique_Cat”栏中输入。当您精确地计算“ cat1”,“ cat2”,“ cat3”时,则列“ Three_Cat1_Cat2_Cat3”

SELECT 
    EOMONTH, AccountNumber
    ,CASE WHEN
       COUNT(DISTINCT CLASSIFICATION_NAME)=3 THEN 1 
       ELSE 0 
    END AS 'Three_Unique_Cat'
    ,CASE WHEN 
       COUNT(DISTINCT CASE WHEN CLASSIFICATION_NAME IN ('cat1','cat2','cat3') 
        THEN CLASSIFICATION_NAME ELSE NULL END)=3 THEN 1 
       ELSE 0 
     END AS 'Three_Cat1_Cat2_Cat3'
    ,SUM(Value) AS totalSpend
FROM @tran_TABLE
GROUP BY EOMONTH, AccountNumber 

Output:
EOMONTH   AccountNumber Three_Unique_Cat    Three_Cat1_Cat2_Cat3    totalSpend
2018-11-30  123 1   1   52
2019-01-31  123 1   1   37
2019-03-31  123 0   0   15

答案 3 :(得分:0)

很容易,如下所示:

select
    EOMONTH,
    AccountNumber,
    case when count(distinct CLASSIFICATION_NAME) = 3 then 1 else 0 end as CLASSIFICATION_NAME
from
    tran_TABLE
group by
    EOMONTH,
    AccountNumber