Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 18th, 2003, 06:55 AM
Authorized User
 
Join Date: Jun 2003
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default Find Common Data

I have a product table and a product format table joined by a link table as you can have multiple product formats.

I have a stored procedure which I receive product ids and store them in a temp table. I want to return the common product formats between these products passed in.

I could do with some ideas to kick start my brain this morning.

Nickie
 
Old December 18th, 2003, 11:37 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Perhaps I don't understand your requirement; won't a SELECT DISTINCT on the JOIN of the temp and the link do the job?
Code:
SELECT DISTINCT Format
FROM #temp INNER JOIN ProductsAndFormats
    ON #temp.ProductID=ProductsAndFormats.ProductID;
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old December 18th, 2003, 11:50 AM
Authorized User
 
Join Date: Jun 2003
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That would give me all of the product formats and I am only looking for the common ones. e.g.

Say I had the following data
Prod1 with formats A, B, C, D
Prod2 with formats B, D, E, G
Prod3 with formats A, B, C, D

I am looking for B and D to be returned as they are common to the temp tables products.

Nickie
 
Old December 18th, 2003, 02:34 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

If I understand you correctly, what you are looking to do is called 'relational division'.

I'll assume a 'ProductsAndFormats' table which is a linking table containing a Product Code/ID column called 'Product' and another column called 'Format' (assuming foreign key constraints to their respective tables). There is another table called 'Temp' which lists just Product codes/ID, i.e. 'Product':
Code:
SELECT PF1.Format
    FROM ProductsAndFormats AS PF1 INNER JOIN Temp T1
        ON PF1.Product=T1.Product
GROUP BY PF1.Format
HAVING COUNT(PF1.Product) = (SELECT COUNT(Product) FROM Temp);
(I think :D)

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old December 19th, 2003, 05:25 AM
Authorized User
 
Join Date: Jun 2003
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Your assumptions were correct and the SQL returns exactly what I am looking for.

Thanks a lot. \o/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Find out N/A data zeroffzero Excel VBA 3 August 25th, 2008 08:40 AM
Where will I find the data costorceroy BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 1 October 26th, 2004 05:23 AM
Find a particular data using ResultSet bamboat_45 Java Databases 2 August 5th, 2004 12:54 AM
Common question, not so common answer? flyin ADO.NET 5 March 24th, 2004 06:50 PM
I can't find the sample code(C# Data Security..). snowstar Wrox Book Feedback 1 November 21st, 2003 12:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.