SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Holy Case Statements Batman!<!-- 864 -->


Holy Case Statements Batman!<!-- 864 -->

Author
Message
sugarlee5
sugarlee5
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 202
Here's the scenario... I have two tables.

1. OriginalItem
2. AlternateRecommendations

For each OriginalItem, I need to come up with up to eight alternate recommendations.

Currently my query looks something like:

Select
OriginalItem.ItemCode
,(Case When [BestMatch].[RecNumber]=1 Then [BestMatch].[AlternateItem] Else Null End) [Alt1]
(Case When [NestBestMatch].[RecNumber]=2 Then [NextBestMatch].[AlternateItem] Else Null End) [Alt2]
From OriginalItem
Left Join
(Select * From
(Select ItemCode As OriginalItem,
AlternateItem,
Row_Number() over (Partition by ItemCode Order by Date desc) as RecNumber
From AlternateRecommendations) As [RecommendationRank]
Where RecNumber = 1) As BestMatch
On BestMatch.OriginalItem = OriginalItem.ItemCode
Left Join
(Select * From
(Select ItemCode As OriginalItem,
AlternateItem,
Row_Number() over (Partition by ItemCode Order by Date desc) as RecNumber
From AlternateRecommendations) As [RecommendationRank]
Where RecNumber = 2) As NextBestMatch
On NextBestMatch.OriginalItem = OriginalItem.ItemCode


Obviously, this isn't very maintainable, I'd have to have eight left joins and eight case statements to come up with all necessary recommendations.

Any suggestions on a better solution?
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38989 Visits: 38508
Not enough to work with here. If you could post the DDL (CREATE TABE statement) for the two tables, some sample data for the tables (INSERT INTO statements), and the expected results based on the sample data I am sure we can help you with your query.

If you need some help with the above information, please read the first article I have referenced below in my signature block. It will walk you through what you should post and how to post it to get the best possible answers.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
mickyT
mickyT
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1564 Visits: 3317
Hi

You could try
WITH RecommendationRank AS (
SELECT ItemCode As OriginalItem,
AlternateItem,
Row_Number() over (Partition by ItemCode Order by Date desc) as RecNumber
From AlternateRecommendations
)
SELECT
o.ItemCode
,MAX(CASE WHEN r.RecNumber = 1 THEN r.AlternateItem ELSE NULL END) Alt1
,MAX(CASE WHEN r.RecNumber = 2 THEN r.AlternateItem ELSE NULL END) Alt2
,MAX(CASE WHEN r.RecNumber = 3 THEN r.AlternateItem ELSE NULL END) Alt3
,MAX(CASE WHEN r.RecNumber = 4 THEN r.AlternateItem ELSE NULL END) Alt4
,MAX(CASE WHEN r.RecNumber = 5 THEN r.AlternateItem ELSE NULL END) Alt5
,MAX(CASE WHEN r.RecNumber = 6 THEN r.AlternateItem ELSE NULL END) Alt6
,MAX(CASE WHEN r.RecNumber = 7 THEN r.AlternateItem ELSE NULL END) Alt7
,MAX(CASE WHEN r.RecNumber = 8 THEN r.AlternateItem ELSE NULL END) Alt8
FROM OriginalItem o
CROSS APPLY (SELECT TOP 8 * FROM RecommendationRank r WHERE o.ItemCode = r.ItemCode ORDER BY r.RecNumber) a
GROUP BY o.ItemCode;


sugarlee5
sugarlee5
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 202
Brilliant mickyT!

This solved my problem! Thank you sooooooooo much!
mickyT
mickyT
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1564 Visits: 3317
No problem, glad to help
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search