简体   繁体   English

SQL-UNION:如何按顺序对不匹配的列进行 UNION 4 SELECT

[英]SQL- UNION : How to UNION 4 SELECT of unmatched column by Order By

SELECT 'ABC-D Header1' As Seq1
UNION
SELECT 'ABC-D Header2' AS seq2
UNION
SELECT  ABC-D data1 data2..3..data4 ' AS seq3
UNION
SELECT 'ABC-D Tail' AS Seq4
ORDER BY SEQ1 ASC

 OUTPUT:  
 ABC-D data1 data2..3..data4
 ABC-D Header1
 ABC-D Header2     
 ABC-D Tail

EXPECTED RESULT :

ABC-D Header1
ABC-D Header2
ABC-D data1 data2..3..data4
ABC-D Tail

Could anyone help me how UNION works internally, I am dealing with hardcoded values, which should come in two rows ( Header1, Header2) followed by data and Tail part.任何人都可以帮助我 UNION 在内部如何工作,我正在处理硬编码值,它应该分为两行(Header1、Header2),然后是数据和尾部。 I have tried ASC and DESC as well, nothing seems working in giving exact result as expected.我也尝试过 ASC 和 DESC,但似乎没有任何方法可以提供预期的准确结果。

Any help would be appreciated.任何帮助,将不胜感激。

You can use VALUES with position :您可以将VALUES与 position 一起使用:

SELECT t.Header
FROM ( VALUES (1, 'ABC-D Header1'), 
              (2, 'ABC-D Header2'), 
              (3, 'ABC-D data1 data2..3..data4 '), 
              (4, 'ABC-D Tail'))
     ) t(Seq, Header)
ORDER BY seq;

add another column for the required ordering为所需的排序添加另一列

SELECT 'ABC-D Header1' As Seq1, s = 1
UNION
SELECT 'ABC-D Header2' AS seq2, s = 2
UNION
SELECT  'ABC-D data1 data2..3..data4 ' AS seq3, s = 3
UNION
SELECT 'ABC-D Tail' AS Seq4, s = 4
ORDER BY s ASC

EDIT: If you do not wish to have the s in result, use cte or derived table编辑:如果您不希望结果中包含s ,请使用ctederived table

; WITH CTE AS
(
    SELECT 'ABC-D Header1' As Seq1, s = 1
    UNION
    SELECT 'ABC-D Header2' AS seq2, s = 2
    UNION
    SELECT  'ABC-D data1 data2..3..data4 ' AS seq3, s = 3
    UNION
    SELECT 'ABC-D Tail' AS Seq4, s = 4
)
SELECT  Seq1
FROM    CTE
ORDER BY s ASC
select Seq1
from
(
SELECT 'ABC-D Header1' As Seq1, number = 1
UNION
SELECT 'ABC-D Header2' AS seq2, number = 2
UNION
SELECT  'ABC-D data1 data2..3..data4 ' AS seq3, number = 3
UNION
SELECT 'ABC-D Tail' AS Seq4, number = 4
) a 
ORDER BY number ASC

Or you can also use Desired table like this way without redundant columns或者你也可以像这样使用Desired table没有多余的列

SELECT Seq1
FROM(
 VALUES
 ('ABC-D Header1'),
 ('ABC-D Header2'),
 ('ABC-D data1 data2..3..data4 '),
 ('ABC-D Tail')
)v(Seq1)

Result here 结果在这里

在此处输入图片说明

This is one way to do it also.这也是一种方法。 With unpivot .随着unpivot

select seq from 
(select convert(varchar(50), 'ABC-D Header1') as seq1
        , convert(varchar(50), 'ABC-D Header2') as seq2
        , convert(varchar(50),'ABC-D data1 data2..3..data4') as seq3
        , convert(varchar(50),'ABC-D Tail') as seq4) a
unpivot (seq for seq_order in ([seq1], [seq2], [seq3], [seq4])
) as b

here is a demo 这是一个演示

The convert was used becuase without it the error was thrown that the seq3 conflicts with the type of other columns specified in the UNPIVOT list使用convert是因为没有它会抛出错误,即 seq3 与 UNPIVOT 列表中指定的其他列的类型冲突

Result :结果

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM