PlayerID | PlayerName |Country
---------+------------+---------
1001 | Virat | Ind
1001 | Virat | USA
1002 | Dave | Aus
1003 | Paul | Ireland
1005 | Kapil | Ind
1005 | Kapil | Aus
1006 | Manoj | NZ
1007 | Manish | SA
1008 | Paul | SriLanka
1008 | Paul | SriLanka
1008 | Paul | SriLanka
1004 | David | SA
1005 | David | SA
我想从给定的表中运行查询,找出代表多个国家/地区的PlayerIDs
或PlayerNames
。例如,PlayerID=1001
代表了多个国家/地区,即“IND
”和“USA
”。
此表中有一些重复的行,但请忽略它们。
答案 0 :(得分:4)
我认为你的意思是:
SELECT PlayerID, PlayerName
FROM yourTable
GROUP BY PlayerID, PlayerName
HAVING COUNT(DISTINCT Country) > 1;
答案 1 :(得分:1)
避免子查询,CTE,联接,交叉申请等 - 可以尝试像
这样的东西SELECT Y.PlayerId, Y.PlayerName,COUNT(DISTINCT Y.Country) countriesPlayedFor
FROM YourTable Y
GROUP BY Y.PlayerId, Y.PlayerName
HAVING COUNT(DISTINCT Y.Country) > 1
答案 2 :(得分:0)
这也将处理重复:
WITH CTE
AS (
SELECT DISTINCT PlayerID
,PlayerName
,Country
FROM dbo.PlayerCountries
)
SELECT PlayerName
,COUNT(Country)
FROM CTE
GROUP BY PlayerName
HAVING COUNT(Country) > 1