Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query Help!

Status
Not open for further replies.
Sep 13, 2001
11
US
I need to pull only Service Order Ids that are made up of entirely a certain type of product. Service Orders can contain many Products (1 ServiceOrder to Many LineItems) and I am looking for Service Order Ids that only contain ProductNames like "Update" and no other products.

Here are the tables:

->ServiceOrder
Fld1:ServiceOrderId

->LineItems
Fld1:ServiceOrderId
Fld2:productId

->Products
Fld1:productId
Fld2:productName


I hope thats clear enough -

Thanks for the help!
 
SELECT Distinct ServiceOrderId
FROM LineItems l (NOLOCK), Products p (NOLOCK)
WHERE l.productid=p.productid and
p.productname like '%update%'


?? I am not sure if this is what you are looking for??
 
Thanks wsmall73 - but that gets me service order ids that also may contain other products, not just update products.

I just need service order ids that contain only update products and no other products.
Maybe this illustration will help:

ServiceOrderId ProductName
1 Website
1 Form
1 Update-Form
2 Update-Form
2 Update-Website

I only want to return service order id 2 - because it only has "Update" type products.

THANKs Again!
 
This kind of query is always difficult. Here is one solution.

Select o.ServiceOrderId, p.ProductName
From ServiceOrder o
Inner Join LineItems i
On o.ServiceOrderId=i.ServiceOrderId
Inner Join Products p
On i.ProductId=p.ProductId
Inner Join
(Select o.ServiceOrderId
From ServiceOrder o
Join LineItems i
On o.ServiceOrderId=i.ServiceOrderId
Join Products p
On i.ProductId=p.ProductId
Group By o.ServiceOrderId
Having Count(*)=Sum(
Case When charindex('update',p.ProductName)>0
Then 1 Else 0 End)) q
On o.ServiceOrderId=q.ServiceOrderId Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
Terry -

Muchos Gracias for the code! It really did the trick! Your use with the Having clause was excellent.

Thanks again!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top