Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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 1 row or 2 rows or 3 rows or more, etc. Anyway, I need to retrieve it.

Sample data:

SQL
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)

The expected result is as below:
GivenPartNumber_Non	vcompanyid	SourceType
ADFGH22	1233	484456
ADFGH22	1233	484456
ADFGH22	1233	484456
XFDY990	5489	484456
XFDY990	5489	484456
GX84322	2549	484456


What I have tried:

SQL
select GivenPartNumber_Non,vcompanyid from #notmappedsources
where SourceType=484456
group by GivenPartNumber_Non,vcompanyid
Posted
Updated 24-Feb-22 3:28am
v2
Comments
yosiasz 24-Feb-22 15:31pm    
https://forums.sqlteam.com/t/how-to-get-rows-that-have-specific-sourctype-only-when-group-by-two-columns/20654/2

This person posts the same question on multiple forums:

How to get rows that have specific sourctype only when group by two columns? - Transact-SQL - SQLTeam.com Forums[^]

and seems to just be trying to get others to do the work.
 
Share this answer
 
v2
Comments
John 13564154 24-Feb-22 11:28am    
Apologies. I just realized this should have been a comment, not a "solution", but the point is valid.
Why not just use:
SQL
select GivenPartNumber_Non,vcompanyid, SourceType from #notmappedsources
where SourceType=484456
It gives me:
GivenPartNumber_Non	vcompanyid	SourceType
ADFGH22	                  1233	    484456
ADFGH22	                  1233	    484456
ADFGH22	                  1233	    484456
XFDY990	                  5489	    484456
XFDY990	                  5489	    484456
GX84322	                  2549	    484456
MNH32	                  5601	    484456
KLMD33	                  5022	    484456
Which looks like what you ask for ...
 
Share this answer
 
Comments
ahmed_sa 23-Feb-22 15:39pm    
no not that what i need
i need vcompany and given part number that have only one source type 484456
ahmed_sa 23-Feb-22 15:43pm    
MNH32 5601 484456
KLMD33 5022 484456

these two rows i don't need it because i need group that have only one souce 484456
OriginalGriff 23-Feb-22 15:54pm    
In which case you need to do two queries and JOIN them together: the second query Does the GROUP BY and returns the part number, with a HAVING clause that eliminates the singletons using the COUNT aggregate function.
The JOIN then limits the original query to just those parts.
ahmed_sa 23-Feb-22 16:18pm    
can you show me how by code if possible
OriginalGriff 23-Feb-22 16:59pm    
Oh come on! You know how to do a GROUP BY, and you know how to do a JOIN!
So what's the problem with putting the two together?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900