2012-11-14 49 views
0

任何人都可以幫我查詢嗎?SELECT列哪裏有一列是不同的?

我試過以下,但它有錯誤出現

SELECT Column1, Column2, Column3 FROM Table WHERE [Column1] NOT IN 
(SELECT [Column1] FROM Table GROUP BY [Column1] HAVING COUNT([Column]) > 1) 

無效MEMO,OLE或超鏈接對象的子查詢[列1]。

回答

4

使用Group ByHaving條款:

SELECT Column1, MIN(Column2)AS Column2, MIN(Column3)AS Column3 
FROM dbo.Table 
GROUP BY Column1 
HAVING (COUNT(Column1) = 1) 

應工作,因爲有每個 「組」 只有一排。

+1

這裏有一個例子:http://sqlfiddle.com/#!3/d99a8/1/0 –

+0

具有列在MIN休息()函數不給我是個例外。謝謝。 – Pantelis

0

據我得到的,你需要的所有數據,其中[列1]是唯一的(只顯示一次)

DECLARE @x TABLE (col1 INT, col2 INT, col3 INT) 

INSERT INTO @x 
     ([col1], [col2], [col3]) 
VALUES (1, 2, 3 ) 
     ,(1, 4, 5) 
     ,(2, 6, 7) 

SELECT * FROM @x 

SELECT col1, col2 , col3 FROM @x 
WHERE col1 NOT IN 
    (SELECT [col1] FROM @x GROUP BY [col1] HAVING COUNT(*) > 1) 
1

原始查詢應該工作,你剛剛列,而不是列1。

SELECT Column1, Column2, Column3 FROM TableName WHERE [Column1] NOT IN 
(SELECT [Column1] FROM TableName GROUP BY [Column1] HAVING COUNT(Column1) > 1) 

見:http://sqlfiddle.com/#!3/d99a8/5/0