Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Difference between sets of data

Author  Topic 

Peter Dutch
Posting Yak Master

127 Posts

Posted - 2003-01-09 : 05:15:50
CREATE TABLE Products(ProductID INT, Description VARCHAR(50)) --  all the different products
CREATE TABLE ProductIngredients (ProductID INT, IngredientID INT) -- links ingredients to products
CREATE TABLE Ingredients (IngredientID INT, Description VARCHAR(50)) -- all different ingredients
CREATE TABLE ProductionRuns (ProductionRunID INT, ProductID INT) -- details on the different 'productionruns'
CREATE TABLE ProductionRunDetails (ProductionRunID INT, IngredientID INT) -- which ingredients were used in the runs

INSERT Products (ProductID, Description) VALUES (1,'Cooked Shrimp')
INSERT Products (ProductID, Description) VALUES (2,'Shrimp in Brine')

INSERT Ingredients (IngredientID, Description) VALUES (1,'Class A shrimp')
INSERT Ingredients (IngredientID, Description) VALUES (2,'Class B shrimp')
INSERT Ingredients (IngredientID, Description) VALUES (3,'Class C shrimp')
INSERT Ingredients (IngredientID, Description) VALUES (4,'Brine')
INSERT Ingredients (IngredientID, Description) VALUES (5,'Salt')
INSERT Ingredients (IngredientID, Description) VALUES (6,'Liquor')

INSERT ProductIngredients (ProductID, IngredientID) VALUES (1,1)
INSERT ProductIngredients (ProductID, IngredientID) VALUES (1,5)
INSERT ProductIngredients (ProductID, IngredientID) VALUES (2,1)
INSERT ProductIngredients (ProductID, IngredientID) VALUES (2,2)
INSERT ProductIngredients (ProductID, IngredientID) VALUES (2,4)

INSERT ProductionRuns (ProductionRunID, ProductID) VALUES (100,1)
INSERT ProductionRuns (ProductionRunID, ProductID) VALUES (101,1)
INSERT ProductionRuns (ProductionRunID, ProductID) VALUES (102,1)
INSERT ProductionRuns (ProductionRunID, ProductID) VALUES (103,2)
INSERT ProductionRuns (ProductionRunID, ProductID) VALUES (104,2)

INSERT ProductionRunDetails (ProductionRunID, IngredientID) VALUES (100,1)
INSERT ProductionRunDetails (ProductionRunID, IngredientID) VALUES (100,5)
INSERT ProductionRunDetails (ProductionRunID, IngredientID) VALUES (103,1)
INSERT ProductionRunDetails (ProductionRunID, IngredientID) VALUES (103,2)
INSERT ProductionRunDetails (ProductionRunID, IngredientID) VALUES (103,4)
INSERT ProductionRunDetails (ProductionRunID, IngredientID) VALUES (104,1)
INSERT ProductionRunDetails (ProductionRunID, IngredientID) VALUES (104,3)
INSERT ProductionRunDetails (ProductionRunID, IngredientID) VALUES (104,6)



We produce a series of products. The ingredients for every product are
stored in the ProductIngredients table. These are the preffered ingredients,
due to different reasons on the productionfloor someone may decide to use
a different ingredient for a certain product. I need a query that returns the
ProductionRunIDs in which were used the 'wrong' ingredients.

In my sampledata that would be ProductionRunID 104.

I can think of a solution looping through the ProductionRuns but I'm almost certain there's a 'set-based' way of doing this.

Any input is much appreciated.

Peter



nr
SQLTeam MVY

12543 Posts

Posted - 2003-01-09 : 05:41:27
something like

select distinct pr.ProductionRunID
from ProductionRuns pr, ProductionRunDetails prd
where pr.ProductionRunID = prd.ProductionRunID
and not exists
(select * from ProductIngredients pi
where pi.ProductID = pr.ProductID
and pi.IngredientID = prd.IngredientID)

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2003-01-09 : 05:44:00
Damn nr you sniped me, I was just about to post something similar and I did a quick refresh

But Peter, I have to say THANK YOU for posting such good DDL and DML for us to play with.

If everyone did that, solving problems on SQLTeam would be SO much easier.

Damian
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2003-01-09 : 05:46:05
Yep - good question.
Sorry I didn't actually run the script to test it .

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Peter Dutch
Posting Yak Master

127 Posts

Posted - 2003-01-09 : 05:46:43
nr,

Thank you very much!

Damian,

quote:
But Peter, I have to say THANK YOU for posting such good DDL and DML for us to play with.



hehe, well I answer lots of questions on another forum (aspmessageboard.com ) so I know how nice it can be if someone makes a little effort in posting a question.

Go to Top of Page

Peter Dutch
Posting Yak Master

127 Posts

Posted - 2003-01-09 : 06:57:33
Thanks again for your input. It was almost correct, but guess that is because I wasn't clear enough.

It's also possible that more or less ingredients are added, so I added

OR SELECT COUNT(DISTINCT IngredientID) FROM ProductIngredients WHERE [..]) != (SELECT COUNT(DISTINCT IngredientID) FROM ProductionRunDetails WHERE [..])

and it works great!

Peter


Go to Top of Page
   

- Advertisement -