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 |
deanglen
Yak Posting Veteran
65 Posts |
Posted - 2014-06-17 : 06:27:42
|
HiYesterday I got this script working. The script basically maps ALL products to customer levels. The Delete statement at the start is needed to make sure that any deleted products or temporary mapped products are removed once a week.However I want to map some products to CustomerID's 9,10 and 14 only. But when I run this statement it maps all products to all customer levels. What I want is thisAll products mapped to customer levels 9,10 and 14Then every other product mapped to customer levels 0,5,7, and 8 but not those extra ones in 9,10 and 14.Any ideas?DELETE FROM ProductCustomerLevelWHERE CustomerLevelID IN (0, 5, 7, 8, 9, 10, 14)INSERT ProductCustomerLevel(ProductID,CustomerLevelID)SELECT ProductID,CustomerLevelFROM dbo.Product pCROSS JOIN (SELECT 0 AS CustomerLevel UNION ALLSELECT 5 UNION ALLSELECT 7 UNION ALLSELECT 8 UNION ALLSELECT 9 UNION ALLSELECT 10 UNION ALLSELECT 14)cWHERE ProductID NOT IN ( SELECT ProductID FROM ProductCustomerLevel WHERE CustomerLevelID=9 OR CustomerLevelID=10 OR CustomerLevelID=14) |
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2014-06-17 : 07:31:36
|
If you are doing a cross join do you need the where at the bottom?wont the ProductID be the same here, the OR at the bottom will calc those because you are saying ok then well do these IDs too?DELETE FROM ProductCustomerLevelWHERE CustomerLevelID IN (0, 5, 7, 8, 9, 10, 14)INSERT ProductCustomerLevel(ProductID,CustomerLevelID)SELECT ProductID,CustomerLevelFROM dbo.Product pCROSS JOIN (SELECT 0 AS CustomerLevel UNION ALLSELECT 5 UNION ALLSELECT 7 UNION ALLSELECT 8 UNION ALLSELECT 9 UNION ALLSELECT 10 UNION ALLSELECT 14)cWHERE ProductID NOT IN (9,10,14) SELECT ProductID FROM ProductCustomerLevelWe are the creators of our own reality! |
|
|
deanglen
Yak Posting Veteran
65 Posts |
Posted - 2014-06-17 : 08:03:10
|
Thanks But did not work unfortunately. I guess the closest I have is this DELETE FROM ProductCustomerLevelWHERE CustomerLevelID IN (0, 5, 7, 8)INSERT ProductCustomerLevel(ProductID,CustomerLevelID)SELECT ProductID,CustomerLevelFROM dbo.Product pCROSS JOIN (SELECT 0 AS CustomerLevel UNION ALLSELECT 5 UNION ALLSELECT 7 UNION ALLSELECT 8)cWHERE ProductID NOT IN (SELECT ProductID FROM ProductCustomerLevelWHERE CustomerLevelID=9OR CustomerLevelID=10OR CustomerLevelID=14) |
|
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2014-06-17 : 08:52:03
|
Why have you aliased dbo.Product? you have not called it?WHERE p.ProductID NOT IN (9,10,14) SELECT ProductID FROM ProductCustomerLevelWe are the creators of our own reality! |
|
|
deanglen
Yak Posting Veteran
65 Posts |
Posted - 2014-06-17 : 09:58:11
|
Yes I did include it, the last statement was just what was working before. It still maps every product to all categories however. |
|
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2014-06-17 : 11:02:59
|
Without the data its awkward but really a merge would be a better option, also case statement:DELETE FROM ProductCustomerLevelWHERE CustomerLevelID IN (0, 5, 7, 8)INSERT ProductCustomerLevel(ProductID,CustomerLevelID)SELECT [ProductID], [CustomerLevel, 0 AS CustomerLevel UNION ALL, 5 UNION ALL, 7 UNION ALL, 8, [Level] = Case When [CustomerLevelID] In (9,10,14) Then p.[ProductID] When [CustomerLevelID] In (0,5,7,8) Then p.[ProductID] EndFROM dbo.ProductWe are the creators of our own reality! |
|
|
|
|
|
|
|