MS SQL – Selecting rows in specified order for each value from chosen colum

  Kiến thức lập trình

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?

Thank you in advance!

I was searching for similar problem, but I couldn’t find it anywhere.

New contributor

Rantoryu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT