declare @T table
(
catalogue_code int,
purcha varchar(5),
stock_ varchar(5)
)
insert into @T values
(1001, 'Box', 'Box'),
(1001, 'Box', 'Box'),
(1002, 'AA', 'AA'),
(1002, 'MM', 'MM'),
(1002, 'EA', 'EA'),
(1002, 'Set', 'Set'),
(1002, 'Kit', 'Kit'),
(1004, 'Set', 'Set')
;with C as
(
select *,
row_number() over(partition by catalogue_code
order by case when purcha = 'EA'
then 0
else 1
end) as rn
from @T
)
select *
from C
where rn = 1
Kết quả:
catalogue_code purcha stock_ rn
-------------- ------ ------ --------------------
1001 Box Box 1
1002 EA EA 1
1004 Set Set 1
Hãy thử trên SE-Data Explorer: https://data.stackexchange.com/stackoverflow/ q / 114648 /