Re: Dynamic ORDER BY
- From: "Adam Machanic" <amachanic@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 7 Nov 2005 13:26:32 -0500
A few comments:
First of all, I find it much easier (and more logical) to wrap UNIONs in a
derived table for ordering:
SELECT *
FROM
(
SELECT Col11, Col12, Col13 FROM Table1 WHERE ....
UNION
SELECT Col21, Col22, Col23 FROM Table2 WHERE ....
) AS x (Col1, Col2, Col3)
ORDER BY
x.Col1,
x.Col2 DESC,
x.Col3
Second, you can stack case expressions to selectively sort (although it may
get confusing)... I have done something like the following in a project that
required no dynamic SQL:
ORDER BY
CASE @SortOrder1
WHEN 'ASC' THEN
CASE @SortCol1
WHEN 1 THEN ColA
WHEN 2 THEN ColB
...
ELSE NULL
END
ELSE NULL
END ASC,
CASE @SortOrder1
WHEN 'DESC' THEN
CASE @SortCol
WHEN 1 THEN ColA
WHEN 2 THEN ColB
...
ELSE NULL
END
ELSE NULL
END DESC,
...
So what's happening here?
If @SortOrder1 is 'ASC', a column value will be used in the ASC sort (look
right after the END of the outer CASE expressions for the sort); otherwise,
it will sort NULL, which means that the next sort condition will be used to
break the ties (every row will be tied) -- the next sort condition is sorted
descending, based on @SortOrder1's value of 'DESC'. We did this for every
sort column, for ascending/descending. I hope this makes sense. If not,
write back and I'll try to explain it differently...
Note that I would much prefer dynamic SQL to this technique from both a
readability and performance standpoint. But if you can't use dynamic SQL
and do need dynamic sorting, this will work.
--
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--
"LUIS" <LUIS@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:556D0DFB-D121-4826-9A04-B3BE8F7F21F6@xxxxxxxxxxxxxxxx
> Adam
>
> Thank you very much for your interest. Query is next:
>
>
> SELECT Col11, Col12, Col13 FROM Table1 WHERE ....
>
> UNION
>
> SELECT Col21, Col22, Col23 FROM Table2 WHERE ....
>
> ORDER BY ( 1 / 2,1 / 1 DESC, 3)
>
>
> Column Types for Col11/Col21,Col11/Col21,Col11/Col21 are the similar. For
> SELECTs UNIONed is necessary use numbers only. Last code line is to show
> what
> I want to do, to select betwwen 3 types of order criteria.
>
> Beforehand, thank you very much.
>
> --
> Luis Garcia
> IT Consultant
>
>
> "Adam Machanic" wrote:
>
>> I don't think it would be a problem with or without dynamic SQL ... but
>> can
>> you share some more information -- show exactly what you're trying to do?
>>
>>
>> --
>> Adam Machanic
>> Pro SQL Server 2005, available now
>> www.apress.com/book/bookDisplay.html?bID=457
>> --
>>
>>
>> "LUIS" <LUIS@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
>> news:FC98B0BE-41F5-4E44-8CBD-7D2DB80FC40D@xxxxxxxxxxxxxxxx
>> > Adam
>> >
>> > Thank you, very much for answering. Just one little thing. I want to
>> > use
>> > ORDER BY in a query with some SELECTs UNIONed; and some ORDER BY
>> > criteria
>> > have more than one column. I think you suggestion will not work. Is
>> > there
>> > another way, or only way to follow is Dynamic SQL?
>> >
>> > Once again, thank you very much.
>> >
>> > --
>> > Luis Garcia
>> > IT Consultant
>> >
>> >
>> > "Adam Machanic" wrote:
>> >
>> >> There are various ways to do that... one is to use a case expression:
>> >>
>> >> ORDER BY
>> >> CASE @MyOrderVariable
>> >> WHEN 1 THEN SomeCol
>> >> WHEN 2 THEN SomeOtherCol
>> >> WHEN 3 THEN ThirdCol
>> >> ....
>> >> ELSE NULL
>> >> END
>> >>
>> >>
>> >> Another is to use dynamic SQL. Depending on your environment one or
>> >> the
>> >> other may be more appropriate.
>> >>
>> >>
>> >> --
>> >> Adam Machanic
>> >> Pro SQL Server 2005, available now
>> >> www.apress.com/book/bookDisplay.html?bID=457
>> >> --
>> >>
>> >>
>> >> "LUIS" <LUIS@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
>> >> news:87500F36-072A-46CD-BAB5-AE89AEEA7A58@xxxxxxxxxxxxxxxx
>> >> > Is there a way to make a Dynamic ORDER BY, allowing some kind of
>> >> > "variables"
>> >> > to to pass values and define it ?
>> >> >
>> >> > Beforehand, thank you very much.
>> >> > --
>> >> > Luis Garcia
>> >> > IT Consultant
>> >>
>> >>
>> >>
>>
>>
>>
.
- Follow-Ups:
- Re: Dynamic ORDER BY
- From: LUIS
- Re: Dynamic ORDER BY
- References:
- Re: Dynamic ORDER BY
- From: Adam Machanic
- Re: Dynamic ORDER BY
- From: Adam Machanic
- Re: Dynamic ORDER BY
- From: LUIS
- Re: Dynamic ORDER BY
- Prev by Date: Re: Dynamic ORDER BY
- Next by Date: Re: Dynamic ORDER BY
- Previous by thread: Re: Dynamic ORDER BY
- Next by thread: Re: Dynamic ORDER BY
- Index(es):
Relevant Pages
|