October 26, 2015 at 9:41 am
Ok, trying to bring this to the original topic.
This is what I was thinking as an approach.
Select Top(10) HotelName, Price, CustomerRating
From dbo.VacationDestinations
where Price <= @Price AND CustomerRating = @CustomerRating
Order By CASE WHEN @ratingWeight > @priceWeight THEN CustomerRating * -1 ELSE Price END,
CASE WHEN @ratingWeight > @priceWeight THEN Price ELSE CustomerRating * -1 END
However, I have a question.
If you're filtering by a specific Customer Rating, why do you need to order at all? In that case, it will always return the same results as this:
Select Top(10) HotelName, Price, CustomerRating
From dbo.VacationDestinations
where Price <= @Price AND CustomerRating = @CustomerRating
Order By Price
October 26, 2015 at 10:36 am
That will "only" handle the price and rating combination and only in that order. We're going to need some dynamic SQL here and it needs to be in the form of Gail Shaw's "Catch All" query.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 26, 2015 at 10:46 am
Hi, thanks for replying. The columns I've shown in this post are all of the columns of my current table. I might add more columns to my table in the future. Also priceWeight and ratingweight are not table columns, their only use is to determine the order in which the columns should be listed in the Order By clause.
I tested what difference it would make if I list the columns in different orders in the Order By clause and sure enough the order in which the results came back are different depending on the order of the columns.
October 26, 2015 at 10:47 am
The only columns mentioned have been Price, Rating and Location. If rating is a single value, I'll assume that location would be as well (You won't be looking for hotels in Atlanta and New York at the same time). Also, location ordered by description wouldn't be very useful. An order by distance from certain point should be better. Of course, those are just assumptions.
With more information, I'd agree that the catch-all query with dynamic SQL would be the best option. Meanwhile, it depends.
October 30, 2015 at 1:45 am
Try to insert your colum name with rates on a temporary table or variable table.
Order by rate.
Output the ordered colum name on comma delimited list.
Insert the comma delimited list in the order by clause on a dynamic sql.
Execute the dynamic sql
October 30, 2015 at 2:54 am
Do you mean like this?
Select Top(10) HotelName, Price, CustomerRating
From dbo.VacationDestinations
where Price <= @Price AND CustomerRating = @CustomerRating
Order By
CASE WHEN @ratingWeight >= @priceWeight THEN CustomerRating ELSE Price END DESC,
CASE WHEN @ratingWeight >= @priceWeight THEN Price ELSE CustomerRating END DESC
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 30, 2015 at 3:37 am
alan.cdes (10/30/2015)
Try to insert your colum name with rates on a temporary table or variable table.Order by rate.
Output the ordered colum name on comma delimited list.
Insert the comma delimited list in the order by clause on a dynamic sql.
Execute the dynamic sql
Or create TVF which builds character string to odrer by from rates and values .
Viewing 7 posts - 16 through 21 (of 21 total)
You must be logged in to reply to this topic. Login to reply