I'm trying to find a solution for my problem using MS SQL Server:
Table 1:
Column A |
---|
Value1 |
Value2 |
Value3 |
For above values from column A, I'm trying to "SELECT UNION" data from different tables - (a,b,c represents few columns with data).
Problem is that my query output looks like this:
Col | Col2 |
---|---|
a | Value1 |
a | Value2 |
a | Value3 |
b | Value1 |
b | Value2 |
b | Value3 |
c | Value1 |
c | Value2 |
c | Value3 |
And I want it to look like this:
Col | Col2 |
---|---|
a | Value1 |
b | Value1 |
c | Value1 |
a | Value2 |
b | Value2 |
c | Value2 |
a | Value3 |
b | Value3 |
c | Value3 |
What can I do to output rows in a certain order for each value as in above example?
Maybe there is some built in option from Transact SQL to manipulate with order like that? Can I solve it with case() function?
You want to order by two columns: first by col2
and then by col
. You can specify multiple columns in the ORDER BY
clause to achieve this:
ORDER BY col2, col