从没有临时表的另一个选择中选择?

时间:2014-11-18 08:45:49

标签: sql sql-server

有一个包含值的数字表[1,1,2,3,7,3,4,2,7] 我想只选择重复项,因此输出集将包含[1,2,3,7],因此过滤掉了4。

我的代码是我这样做的:

DROP TABLE #tempTable;
SELECT  [numbers] as nums, COUNT(*) as cny
INTO #tempTable
  FROM [testBase].[dbo].[numbers] group by numbers;

SELECT nums from #tempTable where nums > 1;

现在我想知道我是否可以在没有先选择#tempTable的情况下获得它? 更像是从select中选择,还是只能这样做?

5 个答案:

答案 0 :(得分:3)

在一个查询中相同。

GROUP BY 之后的条件应添加到 HAVING 子句中。

SELECT  [numbers] as nums, COUNT(*) as cny
FROM [testBase].[dbo].[numbers] 
GROUP BY numbers
HAVING COUNT(*) > 1

更多关于此

HAVING (Transact-SQL)

Having clause tutorial

答案 1 :(得分:0)

试试这个: -

SELECT [numbers] AS NUM
FROM TAB_NAME
HAVING COUNT([numbers]) > 1;

答案 2 :(得分:0)

试试这个..

select count(numbers) from TABLE group by numbers having count(*)>1

答案 3 :(得分:0)

使用Having子句。 Having指定SELECT语句中使用的组或聚合函数的搜索条件。因此,在having clause中,您可以筛选不同的行。

SELECT [numbers]
FROM   [testBase].[dbo].[numbers]
GROUP  BY [numbers]
HAVING Count([numbers]) > 1 

答案 4 :(得分:0)

 SELECT numbers FROM 
 numbers
 GROUP BY numbers 
 having Count(numbers) >= 2