2016-12-15 73 views
0

我正在比較兩個表(dbo.newdbo.old),如果前三列匹配且第四列不匹配,則必須選擇它。現在這顯示了很多值,我只想顯示column2的唯一值。這是代碼我現在有:如何僅在此現有SQL查詢中顯示唯一值

SELECT dbo.new.[column1], dbo.new.[column2], dbo.new.[column3], dbo.new.[column4] 
FROM dbo.new 
JOIN dbo.old ON dbo.new.[column1]=dbo.old.[column1] 
    AND dbo.new.[column2]=dbo.old.[column2] 
    AND dbo.new.[column3]=dbo.old.[column3] 
WHERE [dbo].[new].[column4] <> [dbo].[old].[column4] 

前兩個表我開始:

----------------- 
| 1 | 1 | 1 | 1 | 
----------------- 
| 2 | 1 | 2 | 2 | 
----------------- 
| 3 | 3 | 3 | 3 | 
----------------- 
| 4 | 1 | 4 | 4 | 
----------------- 

----------------- 
| 1 | 1 | 1 | 9 | 
----------------- 
| 2 | 1 | 2 | 9 | 
----------------- 
| 3 | 3 | 3 | 9 | 
----------------- 
| 4 | 1 | 4 | 9 | 
----------------- 

這是上面的查詢結果:

----------------- 
| 1 | 1 | 1 | 1 | 
----------------- 
| 2 | 1 | 2 | 2 | 
----------------- 
| 3 | 3 | 3 | 3 | 
----------------- 
| 4 | 1 | 4 | 4 | 
----------------- 
    ^delete those duplicates 

這就是我想要的結果如下:

----------------- 
| 1 | 1 | 1 | 1 | 
----------------- 
| 3 | 3 | 3 | 3 | 
----------------- 

我試過很多東西,如UNIQUEDISTINCT,但我找不到解決方案。它甚至需要顯示第一個值,只要它顯示唯一的一行。所以這也是正確的:

----------------- 
| 4 | 1 | 4 | 4 | 
----------------- 
| 3 | 3 | 3 | 3 | 
----------------- 
+1

編輯您的問題,並提供樣本數據以及所需的結果。 –

+0

@wouterdejong您希望所有列的記錄具有相同的值嗎?並且添加一些示例數據,以便理解這個問題。 –

+0

你說'如果前三列匹配,第四列不匹配,就必須選擇它。'對我而言,這意味着將選擇1 1 1 5。 1 1 1 1或1 1 5 2是不會被選中的例子 - 在第一個條件中第四個不匹配被驗證,第二個列1-3不匹配 - 你能否澄清? – Cato

回答

1

它看起來像你只列2興趣,所以讓我們開始只選擇那個。 然後在最後添加一個簡單的:GROUP BY,就完成了。

SELECT N.[column2] as myvalue 
FROM dbo.new N 
JOIN dbo.old O 
ON N.[column1]=O.[column1] 
    AND N.[column2]=O.[column2] 
    AND N.[column3]=O.[column3] 
WHERE N.[column4] <> O.[column4] 
GROUP BY N.[column2] 
+0

我在這裏得到與@Serg代碼相同的錯誤:'Additional信息:無法綁定多部分標識符「dbo.new.column2」。 –

+0

@wouterdejong我編輯了該組,試試吧。 –

+0

試過了,但是已經工作了 –

4

over()中選擇您需要的順序以獲得正確的行。

SELECT TOP(1) WITH TIES 
    dbo.new.[column1], dbo.new.[column2], dbo.new.[column3], dbo.new.[column4] 
FROM dbo.new 
JOIN dbo.old ON dbo.new.[column1]=dbo.old.[column1] 
AND dbo.new.[column2]=dbo.old.[column2] 
AND dbo.new.[column3]=dbo.old.[column3] 
where [dbo].[new].[column4] <> [dbo].[old].[column4] 
ORDER BY row_number() over(partition by dbo.new.[column2] order by dbo.new.[column1]) 

快速演示,運行正常的SQL Server 2014

create table dbo.new(
    column1 int, 
    column2 int, 
    column3 int, 
    column4 int); 
create table dbo.old(
    column1 int, 
    column2 int, 
    column3 int, 
    column4 int); 
insert dbo.new values 
(1 , 1 , 1 , 1), 
(2 , 1 , 2 , 2), 
(3 , 3 , 3 , 3), 
(4 , 1 , 4 , 4); 

insert dbo.old values 
(1 , 1 , 1 , 9), 
(2 , 1 , 2 , 9), 
(3 , 3 , 3 , 9), 
(4 , 1 , 4 , 9); 


SELECT TOP(1) WITH TIES 
    dbo.new.[column1], dbo.new.[column2], dbo.new.[column3], dbo.new.[column4] 
FROM dbo.new 
JOIN dbo.old ON dbo.new.[column1]=dbo.old.[column1] 
AND dbo.new.[column2]=dbo.old.[column2] 
AND dbo.new.[column3]=dbo.old.[column3] 
where [dbo].[new].[column4] <> [dbo].[old].[column4] 
ORDER BY row_number() over(partition by dbo.new.[column2] order by dbo.new.[column1]); 

結果是

column1 column2 column3 column4 
    1  1  1  1 
    3  3  3  3 
+0

不錯的一個! 我不知道「與領帶」 更經典的應該是「row_number()over(partition by dbo.new。[column2])number_column」 和「where number_column = 1」 –

+0

@serg謝謝,但由於某種原因,我得到的錯誤:''其他信息:多部分標識符「dbo.new.column2」無法綁定.'' –

+0

請提供表定義 – Serg