SELECT列哪一列是不同的?

时间:2012-11-14 15:19:37

标签: tsql oledbcommand

有人可以帮我查询吗?

我尝试了以下操作,但它出现了错误

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

子查询[Column1]中的MEMO,OLE或超链接对象无效。

3 个答案:

答案 0 :(得分:4)

Group ByHaving子句一起使用:

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

应该可以工作,因为每个“组”只有一行。

答案 1 :(得分:1)

您的原始查询应该只使用列而不是column1。

 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

答案 2 :(得分:0)

据我所知,你需要[Column1]唯一的所有数据(只出现一次)

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 )