K
K
kill942015-11-15 23:50:33
SQL
kill94, 2015-11-15 23:50:33

How to properly group rows?

for example there is a table 9bd1c0470ae749cd8c0b00b6c82c98f7.jpg
in the column [Column2] there are repeating rows and I need to output without repeating.
How can I do that ? so that the result is
e86008a13b3c4868ac1fc81e95eac152.jpg

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2015-11-15
@Rsa97

If without `id`, then SELECT DISTINCT, otherwise GROUP BY

A
Artur Polozov, 2015-11-16
@Noxy

You can use window functions to solve:

-- вариант 1
SELECT s.id, s.Column1, s.Column2 
FROM (
    SELECT t.id, t.Column1, t.Column2, RN = ROW_NUMBER() OVER (PARTITION BY t.Column2 ORDER BY id ASC)
    FROM test1 AS t
) S
WHERE S.RN = 1

-- вариант 2 с CTE
;WITH S AS (
    SELECT t.id, t.Column1, t.Column2, RN = ROW_NUMBER() OVER (PARTITION BY t.Column2 ORDER BY id ASC)
    FROM test1 AS t 
)
SELECT s.id, s.Column1, s.Column2
FROM s
WHERE S.RN = 1

N
nozzy, 2015-11-16
@nozzy

Without window functions:

select 
id,
Column1,
Column2
from your_table
where id in (
  select min(id) from your_table 
  group by Column2
  )
order by id;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question