2016-10-17 75 views
-1

我想從表中的訂單,其中從表用戶列EMAIL_ID包含重複(行中的用戶與重複的郵件)列表的列表搜索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] 

有了相同的結果:

與也試過。

任何想法我做錯了什麼?

謝謝!

+1

接着說:SQL-server'標籤使用 –

+0

語法中的第二次嘗試看起來很有希望,只有'IN'關鍵字需要同柱,沒有多列比較。 – krtek

回答

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] 
) 
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 
相關問題