Home > Mobile >  Get list from two columns
Get list from two columns

Time:03-23

I want to create a SQL query which gives a list in an order based on the below sequence

X|Y|
----
D|C|
B|A|
C|B|

I want one list in sequence

Z
    
A
B
C
D

No Idea, from where to start

CodePudding user response:

select a.X as Z from table a
union
select b.Y as Z from table b
order by Z

CodePudding user response:

use this

select  X  from (
select X from table 
union  
select y from table 
)main
order by X

CodePudding user response:

create table #data(X nvarchar(40), Y nvarchar(40) )

INSERT INTO #data
SELECT 'D', 'C' union
SELECT 'B', 'A' union
SELECT 'C', 'B'


SELECT X FROM #data
UNION
SELECT Y FROM #data
ORDER BY 1

enter image description here

CodePudding user response:

Maybe something like this?

SELECT DISTINCT a
FROM (
  (SELECT X AS a FROM table1)
  UNION
  (SELECT y AS a FROM table1)
)
ORDER BY a;
  • Related