SQLTeam.com | Weblogs | Forums

How to get rows that have specific sourctype only when group by two columns?

I work on sql server 2014 i need to get rows that have source type 484456

when group by two columns group by GivenPartNumber_Non and vcompanyid

so i need to make select query display every group of rows by GivenPartNumber_Non and

vcompanyid have source type 484456 only may be this group one row or 2rows or 3 rows or

more etc any way i need to retrieve it .

sample data

create table #notmappedsources
(
GivenPartNumber_Non varchar(200),
vcompanyid  int,
SourceType int
)
insert into #notmappedsources(GivenPartNumber_Non,vcompanyid,SourceType)
values
('ADFGH22',1233,484456),
('ADFGH22',1233,484456),
('ADFGH22',1233,484456),

('XFDY990',5489,484456),
('XFDY990',5489,484456),

('GX84322',2549,484456),




('LKHG23',3201,484320),
('LKHG23',3201,484320),
('LKHG23',3201,484320),

('DGHJ66',7041,484320),

('FDYH36',8901,484320),
('FDYH36',8901,484320),

('MNH32',5601,489561),
('MNH32',5601,489561),
('MNH32',5601,484456),

('NUI34',9076,489561),

('KLMD33',5022,489561),
('KLMD33',5022,484456)

expected result

GivenPartNumber_Non vcompanyid SourceType
ADFGH22 1233 484456
ADFGH22 1233 484456
ADFGH22 1233 484456
XFDY990 5489 484456
XFDY990 5489 484456
GX84322 2549 484456

what i try is :slight_smile:

select GivenPartNumber_Non,vcompanyid from #notmappedsources
where SourceType=484456
group by GivenPartNumber_Non,vcompanyid

someone already answered it here for you

1 Like