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.
| Author |
Topic |
|
soaphope
Starting Member
13 Posts |
Posted - 2011-03-24 : 10:18:46
|
| I have products that are made up of components. Each product can contain any amount of one or more components. Each component has a wholesale price and a maker (manufacturer). To store this information, I have a products table, a components table, and a details table that lists the components and their quantities for each product. I need to be able to determine which products have the same underlying maker, component type and wholesale cost structure. In other words, if product 1 is made of one $3.50 widget from Abe and two $3.50 widgets from Bob, I need all other products that are made of one $3.50 type Q widget from Abe and two $3.50 type Z widgets from Bob. The widgets do not have to be the exact same widget, they just have to be the same type, same wholesale cost and from the same maker.I've written the queries to isolate the maker, component type, quantity, and wholesale cost data by product, but I'm having trouble creating the SQL statement needed to select all the matching sets. DECLARE @findthis intSELECT @findthis = 202;WITH T1 AS (SELECT pdc.productdbuid product, SUM(pdc.componentcount) compcount, c.makerdbuid maker, c.comptypedbuid comptype, c.wholesaleprice compwspriceFROM ProductDetailsComponents pdc INNER JOIN Components c ON pdc.componentdbuid = c.dbuidWHERE pdc.productdbuid = @findthisGROUP BY pdc.productdbuid, c.makerdbuid, c.comptypedbuid, c.wholesaleprice) ,T2 AS (SELECT pdc.productdbuid product, SUM(pdc.componentcount) compcount, c.makerdbuid maker, c.comptypedbuid comptype, c.wholesaleprice compwspriceFROM ProductDetailsComponents pdc INNER JOIN Components c ON pdc.componentdbuid = c.dbuidGROUP BY pdc.productdbuid, c.makerdbuid, c.comptypedbuid, c.wholesaleprice)SELECT DISTINCT product FROM T2 WHERE ***all the rows in T1 match a row in the T2 product subset*** AND ***all the rows in the T2 product subset match a row in T1***In this case T1 is:product compcount maker comptype compwsprice202 1 7 1 4.00202 1 7 5 3.25And some sample data from T2 is:198 5 2 1 3.00199 5 2 1 3.00200 1 7 1 4.00200 1 7 5 3.25201 1 7 1 4.00201 1 7 5 3.25202 1 7 1 4.00202 1 7 5 3.25211 1 3 2 6.75211 1 3 3 3.50211 2 12 10 0.00211 3 12 45 0.00What I need for output are the three matching subsets:200201202Any help with the WHERE, or a different approach, is appreciated! |
|
|
soaphope
Starting Member
13 Posts |
Posted - 2011-03-24 : 11:48:50
|
| Actually I resolved this (I think) with help from here: http://www.bigresource.com/Tracker/Track-ms_sql-8i9gU5pO/I post the resolution for anyone else who needs help with the problem: finding all matching groups of detail records.WITH T AS (SELECT pdc.productdbuid product, SUM(pdc.componentcount) compcount, c.makerdbuid maker, c.comptypedbuid comptype, c.wholesaleprice compwsprice, COUNT(pdc.productdbuid) OVER (PARTITION BY (select productdbuid)) setsizeFROM ProductDetailsComponents pdc INNER JOIN Components c ON pdc.componentdbuid = c.dbuidGROUP BY pdc.productdbuid, c.makerdbuid, c.comptypedbuid, c.wholesaleprice)SELECT T2.product from T T2 join T T1 on T2.compcount = T1.compcount AND T2.maker = T1.maker AND T2.comptype = T1.comptype AND T2.compwsprice = T1.compwsprice AND T2.setsize = T1.setsize AND T1.product = @findthisGROUP BY T2.productHAVING COUNT(T2.product) = (SELECT COUNT(product) FROM T WHERE product = @findthis)Salah BoukadoumFounder, Soap Hope Where 100% of profits go to lift women from poverty - http://soaphope.com |
 |
|
|
|
|
|
|
|