我试图从表中获取一个列表订单中的列Email_Id用户包含重复项(具有重复电子邮件的用户中的行)。
SELECT
o.[Email], o.[Email_Id], d.intCount
FROM (
SELECT [Email], COUNT(*) as intCount
FROM [Server].[dbo].[Table]
GROUP BY [Email]
HAVING COUNT(*) > 1
) AS d
INNER JOIN [Server].[dbo].[Table] o ON o.[Email] = d.[Email]
所以我尝试了以下内容:
SELECT * FROM [Server].[dbo].[Orders]
WHERE [Email_Id] IN/
SELECT
o.[Email], o.[Email_Id], d.intCount
FROM (
SELECT [Email], COUNT(*) as intCount
FROM [Server].[dbo].[Users]
GROUP BY [Email]
HAVING COUNT(*) > 1
) AS d
INNER JOIN [Server].[dbo].[Users] o ON o.[Email] = d.[Email]
)
返回当未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式。
还试过:
SELECT [Order_Is], (
SELECT
o.[Email], o.[Email_Id], d.intCount
FROM (
SELECT [Email], COUNT(*) as intCount
FROM [Server].[dbo].[Users]
GROUP BY [Email]
HAVING COUNT(*) > 1
) AS d
INNER JOIN [Server].[dbo].[Users] o ON o.[Email] = d.[Email]
)
as [Email_Id]
FROM [Server].[dbo].[Orders]
结果相同。
知道我做错了什么?
谢谢!
答案 0 :(得分:2)
SELECT *
FROM [Server].[dbo].[Orders]
JOIN ( SELECT [Email], [Email_Id],
count(*) over (partition by [Email]) as cnt
FROM [Server].[dbo].[Users] ) tt
on tt.[Email_Id] = [Orders].[Email_Id]
and tt.[cnt] > 1
答案 1 :(得分:0)
在WHERE
语句子查询中,IN
运算符只能有一个字段或表达式,这就是错误所说的,请尝试这种方式:
SELECT * FROM [Server].[dbo].[Orders]
WHERE [Email_Id] IN (
SELECT
o.[Email_Id]
FROM (
SELECT [Email], COUNT(*) as intCount
FROM [Server].[dbo].[Users]
GROUP BY [Email]
HAVING COUNT(*) > 1
) AS d
INNER JOIN [Server].[dbo].[Users] o ON o.[Email] = d.[Email]
)