sql group by with count来了解状态

时间:2018-01-08 08:35:18

标签: sql-server tsql sql-server-2012 sql-server-2014 sql-server-2016

大家好我有两个像这样的表subscripiton下面有这样的列..

Id  SubscriptionKey  
 1   subkey1
 2   subkey2
 3   subKey3

另一个表 guardkey 包含如下所示的列

 id     key    SubscriptionKey   IsConverted
  1     key1     subkey1           True
  2     key2     subkey1           true
  3     key3     subkey2           true
  4     Key4     subkey2           false
  5     key5     subkey2           true
  6     key6     subKey1           false
  7     key7     subkey3           true
  8     key8     subkey3           true

我们如何确定是否转换了与特定子键相关的所有键。

我将此数据绑定到gridview,如果转换了该特定子项的所有键,我需要禁用特定行

任何人都可以帮忙解决这个问题,看看是否所有密钥都转换为特定密钥..

非常感谢...

我正在使用sql server ..

预期输出就像这样

 subscriptionkey 
      subkey1
      subkey2

3 个答案:

答案 0 :(得分:3)

您可以使用以下查询:

SELECT SubscriptionKey
FROM guardkey
GROUP BY SubscriptionKey
HAVING COUNT(*) <> COUNT(CASE WHEN IsConverted = 'True' THEN 1 END)

HAVING子句使用COUNT函数两次:

  • 第一次计算群组的所有记录(COUNT(*)
  • 第二次有条件地计算该组的“已转换”记录。

如果这两个计数不同,则不转换子密钥,并且查询返回其值。

Demo here

编辑:要从subscription表中获取其余字段,您可以将上述查询用作派生表:

SELECT s.*
FROM subscription AS s
JOIN (
    SELECT SubscriptionKey
    FROM guardkey
    GROUP BY SubscriptionKey
    HAVING COUNT(*) <> COUNT(CASE WHEN IsConverted = 'True' THEN 1 END)
) AS t ON s.SubscriptionKey = t.SubscriptionKey 

答案 1 :(得分:1)

这将是归档上述

的简单方法
DECLARE @T1 TABLE
(
    Id INT,
    SubscriptionKey VARCHAR(50)
)

DECLARE @T2 TABLE
(
    Id INT,
    [key] VARCHAR(50),
    SubscriptionKey VARCHAR(50),
    IsConverted BIT
)

INSERT INTO @t1
VALUES(1,'SubKey1'),
(2,'SubKey2'),
(3,'SubKey3')

INSERT INTO @T2
VALUES
(1,'key1','subkey1',1),
(2,'key2','subkey1',1),
(3,'key3','subkey2',1),
(4,'Key4','subkey2',0),
(5,'key5','subkey2',1),
(6,'key6','subKey1',0)

SELECT
    *
    FROM @T1 t1
        WHERE EXISTS(
            SELECT 1 FROM @T2 T2 WHERE T2.SubscriptionKey = t1.SubscriptionKey AND IsConverted = 1
        )
        AND NOT EXISTS(
            SELECT 1 FROM @T2 T2 WHERE T2.SubscriptionKey = t1.SubscriptionKey AND IsConverted = 0
        )

答案 2 :(得分:1)

请试试这个 -

- 未转换的SubscriptionKeys

SELECT [id], [key], [SubscriptionKey], [IsConverted]
FROM 
(
    SELECT * , COUNT(CASE WHEN IsConverted = 'True' THEN 1 END) OVER (PARTITION BY SubscriptionKey) a
             , COUNT(*) OVER (PARTITION BY SubscriptionKey) b     
    FROM guardkey
)x WHERE a = b

id          key  SubscriptionKey IsConverted
----------- ---- --------------- -----------
7           key7 subkey3         true
8           key8 subkey3         true
(2 rows affected)

- CONVERTED SubscriptionKeys

SELECT [id], [key], [SubscriptionKey], [IsConverted]
FROM 
(
    SELECT * , COUNT(CASE WHEN IsConverted = 'True' THEN 1 END) OVER (PARTITION BY SubscriptionKey) a
             , COUNT(*) OVER (PARTITION BY SubscriptionKey) b     
    FROM guardkey
)x WHERE a < b

id          key  SubscriptionKey IsConverted
----------- ---- --------------- -----------
1           key1 subkey1         True
2           key2 subkey1         true
6           key6 subKey1         false
3           key3 subkey2         true
4           Key4 subkey2         false
5           key5 subkey2         true


(6 rows affected)