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 |
|
kfblake
Starting Member
4 Posts |
Posted - 2009-01-19 : 13:21:28
|
| Hey guys,I could really use some help here. I'm locked into using a table structure that is making things very difficult. Basically, I have products and categories. But, those products can appear in multiple categories. Which categories each product should appear in are defined in a table with a structure like:ProductCodeCategoryID1DisplayOrder1CategoryID2DisplayOrder2CategoryID3DisplayOrder3CategoryID4DisplayOrder4CategoryID5DisplayOrder5I think this is a horrible way to do this, but, like I said, it wasn't my creation and I can't change it.I need to execute a query that uses this table to display multiple results as indicated by the values in the table. So, Product A might be in Category A, Category B and Category C. And if so, in this table, the entry for this row in the table described above might be:ProductCodeAA1B3C4nullnullnullnullThe following sql executes the Category portion of this properly, but I am having a problem coordinating which DisplayOrder field to return: SELECT ... ...product table... LEFT JOIN ProductCategories ON Categories.CategoryID = ProductCategories.CategoryID1 OR Categories.CategoryID = ProductCategories.CategoryID2 OR Categories.CategoryID = ProductCategories.CategoryID3 OR Categories.CategoryID = ProductCategories.CategoryID4 OR Categories.CategoryID = ProductCategories.CategoryID5 If the join is using CategoryID2, I need to select DisplayOrder2, if it uses CategoryID3, I need to select DisplayOrder3.I'm at the end of my rope. I've thought of working the COALESE function in, but that won't work, because this returns all DisplayOrder fields and may not be using the first one that is null (they might all have values but I only want to use whichever one the join is associating with).Any thoughts?This is totally fubar, so let me know if I need to explain the situation further.keith |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-01-19 : 14:29:38
|
| "If the join is using CategoryID2, I need to select DisplayOrder2,.. "DisplayOrder2 for all categories you mean ? |
 |
|
|
kfblake
Starting Member
4 Posts |
Posted - 2009-01-19 : 14:35:13
|
| DisplayOrder2 for all categories you mean ?[/quote]No, sorry. If the join is being performed on CategoryID3, I need to return/use DisplayOrder3 and not the other 4. The other 4 DisplayOrders may or may not have values, but I need to ignore them and only use DisplayOrder3. |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-01-19 : 14:44:41
|
| Can you post sample data and output so that its clear. |
 |
|
|
kfblake
Starting Member
4 Posts |
Posted - 2009-01-19 : 14:57:59
|
| Sorry, this is pretty confusing, I know.ProductCategories Table DefinitionProductCode varchar(20)CategoryID1 decimalDisplayOrder1 int...CategoryID5 decimalDisplayOrder5 intSample ProductCategories Table DataPRODA 1.0 1 2.9 3 6.0 2 null null null nullPRODB 1.0 2 null null null null null nullPRODC 2.0 1 1.0 3 6.0 1 null null null nullWhat I need is to execute a query that returns the Products in each category. But, a product can be in multiple categories. So, for the above example, I need the following result:PRODA 1.0 1PRODB 1.0 2PRODC 1.0 3PRODC 2.0 1PRODA 2.9 3PRODC 6.0 1PRODA 6.0 2Is that a little more clear? |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-01-20 : 01:18:29
|
| Hi blake,upto my knowledge we can't write the two unpivot statements u have to write two select statements i think sotry thisselect productcode , category,categoryvalfrom ProductCategories punpivot(categoryval for category in (CategoryID1,CategoryID2,CategoryID3,CategoryID4,CategoryID5)) as unpvtselect productcode , displayorder,displayordervalfrom ProductCategories punpivot(displayorderval for displayorder in (DisplayOrder1,DisplayOrder2,DisplayOrder3,DisplayOrder4,DisplayOrder5)) as pvt |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-20 : 02:25:09
|
| [code]SELECT ProductCode,ID,OrdFROM(SELECT ProductCode,CategoryID1 as ID,DisplayOrder1 AS OrdFROM TableUNION ALLSELECT ProductCode,CategoryID2 ,DisplayOrder2 FROM TableUNION ALLSELECT ProductCode,CategoryID3 ,DisplayOrder3 FROM TableUNION ALLSELECT ProductCode,CategoryID4 ,DisplayOrder4 FROM TableUNION ALLSELECT ProductCode,CategoryID5 ,DisplayOrder5 FROM Table)tORDER BY ID,Ord[/code] |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-01-20 : 06:28:09
|
Small change to Visakh's code:SELECT ProductCode,ID,OrdFROM(SELECT ProductCode,CategoryID1 as ID,DisplayOrder1 AS OrdFROM TableUNION ALLSELECT ProductCode,CategoryID2 ,DisplayOrder2 FROM TableUNION ALLSELECT ProductCode,CategoryID3 ,DisplayOrder3 FROM TableUNION ALLSELECT ProductCode,CategoryID4 ,DisplayOrder4 FROM TableUNION ALLSELECT ProductCode,CategoryID5 ,DisplayOrder5 FROM Table)tWHERE ID IS NOT NULL and Ord IS NOT NULLORDER BY ID,Ord |
 |
|
|
kfblake
Starting Member
4 Posts |
Posted - 2009-01-20 : 13:34:04
|
| Looks like this works perfectly!I think just laying it out for you to review/understand helped a ton to begin with.Thanks for the help!K |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-21 : 09:21:13
|
| welcome |
 |
|
|
|
|
|
|
|