Use SQL GROUP BY and Case when select specific rows

  Kiến thức lập trình

Hi I have a data set where I want to keep specific value when groupby id. For example, I have this data:

create table #test1( 
id int, 
category1 varchar(20))
;

insert into #test1 values (1,'AAA');
insert into #test1 values (1,'ZZZ');
insert into #test1 values (1,'YYY');
insert into #test1 values (2,'BBB');
insert into #test1 values (2,'ZZZ');
insert into #test1 values (3,'ZZZ');

So in this example, I want to keep AAA and BBB when multiple rows are present for this id.
The result I want to get is:

id category1
1 AAA
2 BBB
3 zzz

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT