如何在 SQL 查询中对多列使用 distinct 和 count
How to use distinct and count for multiple columns in SQL query
我的表格:
+-----------+-----------+---------+
| Cust_Name | Contract | Status |
+-----------+-----------+---------+
| ABC | CISCO | Active |
| DEF | Microsoft | Expired |
| ABC | HP | Expired |
| DEF | DELL | Active |
| ABC | CISCO | Active |
+-----------+-----------+---------+
MS SQL 查询:
SELECT Cust_Name, Active_count,
Contract = substring(List, 1, len(List) - 1)
FROM (SELECT Cust_Name, COUNT(*) AS Active_count
FROM mytable
WHERE status = 'Active'
GROUP BY Cust_Name) AS a
CROSS APPLY (SELECT (SELECT Contract + ', '
FROM Mytable b
WHERE b.Cust_Name = a.Cust_Name
AND b.Status = 'Active'
FOR XML PATH(''), TYPE).value('.', 'nvarchar(MAX)')) AS b(List)
根据我的查询,我无法得到 below.How 我可以得到如下结果吗:
+-----------+--------------+--------------+
| Cust_Name | Active_count | Contracts |
+-----------+--------------+--------------+
| ABC | 2 | CISCO,Oracle |
| DEF | 1 | DELL |
+-----------+--------------+--------------+
我会在末尾使用 STUFF()
而不是 SUBSTRING()
编写查询。无论如何,我认为您只需要 SELECT DISTINCT
在子查询中:
SELECT a.Cust_Name, a.Active_count, Contract = b.List
FROM (SELECT Cust_Name, COUNT(DISTINCT Contract) AS Active_count
FROM mytable
WHERE STS_CODE = 'Active'
GROUP BY Cust_Name
) a CROSS APPLY
(SELECT STUFF((SELECT DISTINCT b.Contract + ', '
FROM Mytable b
WHERE b.Cust_Name = a.Cust_Name AND b.Status = 'Active'
FOR XML PATH(''), TYPE
).value('.', 'nvarchar(MAX)'), 1, 2, ''
)
) AS b(List);
请试试这个:
WITH CTE_Testtable AS (
SELECT
Cust_Name,
Contract,
Status
FROM
TESTTable
WHERE
Status = 'Active'
GROUP BY
Cust_Name, Contract, Status
)
SELECT
Cust_Name,
COUNT(Cust_Name) Active_Count,
STUFF((SELECT ', ' + Contract
FROM
TESTTable B
WHERE
B.Cust_Name = A.Cust_Name
AND Status = 'Active'
GROUP BY Contract
FOR XML PATH('')), 1, 2, '')
FROM
CTE_Testtable A
WHERE
Status = 'Active'
GROUP BY
Cust_Name
我的表格:
+-----------+-----------+---------+
| Cust_Name | Contract | Status |
+-----------+-----------+---------+
| ABC | CISCO | Active |
| DEF | Microsoft | Expired |
| ABC | HP | Expired |
| DEF | DELL | Active |
| ABC | CISCO | Active |
+-----------+-----------+---------+
MS SQL 查询:
SELECT Cust_Name, Active_count,
Contract = substring(List, 1, len(List) - 1)
FROM (SELECT Cust_Name, COUNT(*) AS Active_count
FROM mytable
WHERE status = 'Active'
GROUP BY Cust_Name) AS a
CROSS APPLY (SELECT (SELECT Contract + ', '
FROM Mytable b
WHERE b.Cust_Name = a.Cust_Name
AND b.Status = 'Active'
FOR XML PATH(''), TYPE).value('.', 'nvarchar(MAX)')) AS b(List)
根据我的查询,我无法得到 below.How 我可以得到如下结果吗:
+-----------+--------------+--------------+
| Cust_Name | Active_count | Contracts |
+-----------+--------------+--------------+
| ABC | 2 | CISCO,Oracle |
| DEF | 1 | DELL |
+-----------+--------------+--------------+
我会在末尾使用 STUFF()
而不是 SUBSTRING()
编写查询。无论如何,我认为您只需要 SELECT DISTINCT
在子查询中:
SELECT a.Cust_Name, a.Active_count, Contract = b.List
FROM (SELECT Cust_Name, COUNT(DISTINCT Contract) AS Active_count
FROM mytable
WHERE STS_CODE = 'Active'
GROUP BY Cust_Name
) a CROSS APPLY
(SELECT STUFF((SELECT DISTINCT b.Contract + ', '
FROM Mytable b
WHERE b.Cust_Name = a.Cust_Name AND b.Status = 'Active'
FOR XML PATH(''), TYPE
).value('.', 'nvarchar(MAX)'), 1, 2, ''
)
) AS b(List);
请试试这个:
WITH CTE_Testtable AS (
SELECT
Cust_Name,
Contract,
Status
FROM
TESTTable
WHERE
Status = 'Active'
GROUP BY
Cust_Name, Contract, Status
)
SELECT
Cust_Name,
COUNT(Cust_Name) Active_Count,
STUFF((SELECT ', ' + Contract
FROM
TESTTable B
WHERE
B.Cust_Name = A.Cust_Name
AND Status = 'Active'
GROUP BY Contract
FOR XML PATH('')), 1, 2, '')
FROM
CTE_Testtable A
WHERE
Status = 'Active'
GROUP BY
Cust_Name