Say I have this table
id | data | value
-----------------
1 | a | A
2 | a | A
3 | a | A
4 | a | B
5 | b | C
6 | c | A
7 | c | C
8 | c | C
I want to remove these lines with a duplicate value for each information, keeping one of them with a minimum id, for example. the result will be
id | data | value
-----------------
1 | a | A
4 | a | B
5 | b | C
6 | c | A
7 | c | C
I know a way to do this to make a union like:
SELECT 1 [id], 'a' [data], 'A' [value] INTO
UNION SELECT 3, 'a', 'A' UNION SELECT 4, 'a', 'B'
UNION SELECT 5, 'b', 'C' UNION SELECT 6, 'c', 'A'
UNION SELECT 7, 'c', 'C' UNION SELECT 8, 'c', 'C'
SELECT * FROM
SELECT MIN(id) FROM
GROUP BY [data], [value]
HAVING COUNT(1) > 1
UNION
SELECT MIN(id) FROM
GROUP BY [data], [value]
HAVING COUNT(1) <= 1
)
but this solution should repeat the same group twice (consider that the real case is a massive group with> 20 columns)
I would prefer a simpler answer with less code than against complex ones. Is there an easier way to code this?
thank
source
share