Click here to Skip to main content
15,898,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this table
column1
1
0
2
4
5
1
8
2
6

How would I get a result like this with sql query
column1
1
1
2
2
Posted

try this hope it will help you

SQL
SELECT
    column1, COUNT(column1)
FROM
    table_name
GROUP BY
    column1
HAVING
    COUNT(column1) > 1
 
Share this answer
 
DECLARE @table TABLE (column1 int)
INSERT INTO @table VALUES (1)
INSERT INTO @table VALUES (0)
INSERT INTO @table VALUES (2)
INSERT INTO @table VALUES (4)
INSERT INTO @table VALUES (5)
INSERT INTO @table VALUES (1)
INSERT INTO @table VALUES (8)
INSERT INTO @table VALUES (2)
INSERT INTO @table VALUES (6)

SELECT * 
FROM @table
WHERE column1 IN 
(
	SELECT t.column1
	FROM @table t
	GROUP BY column1
	HAVING COUNT(t.column1) > 1
)
ORDER BY 1
 
Share this answer
 
Comments
wizy@2020 31-Jan-14 10:41am    
what if I have something like this
column1 column2
a 1
a 1
a 2
b 5
b 5
b 4
I want something like this
column1 column2
a 1
a 1
b 5
b 5
Malte Klena 31-Jan-14 12:45pm    
DECLARE @table TABLE (column1 char(1), column2 int)
INSERT INTO @table VALUES ('a',1)
INSERT INTO @table VALUES ('a',1)
INSERT INTO @table VALUES ('a',2)
INSERT INTO @table VALUES ('b',5)
INSERT INTO @table VALUES ('b',5)
INSERT INTO @table VALUES ('b',4)


SELECT *
FROM @table
WHERE column2 IN
(
SELECT t.column2
FROM @table t
GROUP BY column2
HAVING COUNT(t.column2) > 1
)
ORDER BY 1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900