Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I work on sql server 2017 i face issue i can't get partnumber

that have at least source type website and at least have stockid equal 1 per partnumber

so with another meaning i need to get part numers that have stockid 1 and source type website

so i need to make select query get part numbers that must have source type website and stock =1 Based on Part number

sample data

create table #stockyes
 (
 PartNumber varchar(100),
 sourcetype varchar(100),
 stockid tinyint
 )
 insert into #stockyes(PartNumber,sourcetype,stockid)
 values
 (1233,'Website',1),
 (1233,'Website',0),
 (1233,'Datasheet',1),
    
 (8901,'Website',1),
 (8901,'Website',0),
    
 (9015,'Website',1),
    
 (7801,'Website',1),
 (7801,'Website',1),
    
 (9401,'Datasheet',0),
    
 (5688,'Datasheet',0),
 (5688,'Datasheet',0),
    
 (5688,'Lifecycle',2),
 (5688,'Lifecycle',2),
    
 (8409,'Lifecycle',1),
    
 (6703,'Website',0),
 (6703,'Website',0),
 (6703,'Website',0),
    
 (3099,'Website',0),
 (3099,'Website',0),
    
 (8541,'Website',0)


expected result as below

PartNumber
1233
8901
9015
7801


What I have tried:

SQL
select partnumber from #stockyes
where stockid=1 and sourcetype='website'
group by partnumber
Posted
Updated 27-Feb-22 2:00am
v2
Comments
Richard MacCutchan 27-Feb-22 7:36am    
You have not explained exactly what results you get.
Maciej Los 27-Feb-22 11:02am    
If you use COLLATE Latin1_General_CS_AS, then your query will not return any value.

1 solution

SQL
select DISTINCT partnumber from [#stockyes]
where stockid=1 and sourcetype='website'


:)
 
Share this answer
 
v2
Comments
Maciej Los 27-Feb-22 11:03am    
Luc, i think that DISTINCT is redundant. Please, read my comment to the question.
Sorry, my bad. OP wants to get unique values.
5ed!

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