SQL selects the first row entries for a specific column

I understand that my title probably doesn’t explain my situation very well, but I honestly don’t know how to say it.

I am using SQL to access a DB2 database.

Using my screenshot 1 below as a reference:

column 1 has three instances of “U11124” with three different descriptions (column 2)

I want this query to return the first instance of "U11124" and its description, but then also unique entries for other lines. Image 2 shows my desired result.

image 1

image1

image 2

image2

----- EDIT ----

to answer some questions / messages: technically, it should not be the first, only one of these entries. the problem is that we have three descriptions, and we need to show only one, now I said that it does not matter which one.

+3
source share
4 answers
SELECT STVNST, MAX(STDESC) FROM MY_TABLE GROUP BY STVNST;
+5
source

In SQL Server:

select stvnst, stdesc
from (
  select     
     stvnst, stdesc
     row_number() over (order by stdesc partition by stvnst) row
  from table
) a
where row = 1

This method has an advantage over a simple group because it will also work when there are more than two columns in a table.

+3
source

SELECT STVNST,FIRST(STDESC) from table group by STVNST BY what_you_want_first

+2

, , GROUP BY.

, STDESC? , , , , :

SELECT STVNST, MAX(STDESC) FROM MY_TABLE GROUP BY STVNST;
+1

Source: https://habr.com/ru/post/1789870/


All Articles