Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL在使用union后将列拆分为行_Sql_Union_Rows - Fatal编程技术网

SQL在使用union后将列拆分为行

SQL在使用union后将列拆分为行,sql,union,rows,Sql,Union,Rows,我的桌子 +-----------+---------+ | Date | Letter | +-----------+---------+ | 13.02.2013| B | | 01.03.2016| A | | 28.12.2003| C | | 12.01.2017| B | | 25.04.2011| A | +-----------+---------+ 我创建了一个查询,返回正确的数据,但不是预期的: SEL

我的桌子

+-----------+---------+
|    Date   |  Letter |
+-----------+---------+
| 13.02.2013|    B    |
| 01.03.2016|    A    |
| 28.12.2003|    C    |
| 12.01.2017|    B    |
| 25.04.2011|    A    |
+-----------+---------+
我创建了一个查询,返回正确的数据,但不是预期的:

SELECT * from
(
SELECT TOP 1 Date as Date1, Letter as Letter1 from TAB where
Letter = 'A'
order by Date DESC
) TAB
UNION
SELECT * from
(
SELECT TOP 1 Date as Date2, Letter as Letter2 from TAB where 
Letter = 'B'
order by Datum DESC
) TAB
预期产出:

+-----------+---------+-----------+---------+
|    Date1  | Letter1 |    Date2  | Letter2 |
+-----------+---------+-----------+---------+
| 01.03.2016|    A    | 12.01.2017|    B    |
+-----------+---------+-----------+---------+
输出:

+-----------+---------+
|    Date1  | Letter1 |
+-----------+---------+
| 01.03.2016|    A    | 
| 12.01.2017|    B    |
+-----------+---------+
是否可以使用UNION获得预期的所有4行


谢谢。

尝试将第二个查询更改为(日期1和字母1)。列必须相同。

您可以对两个子查询使用
联接,而不是
联合所有
,并使用
别名调用两个子查询中的列。

如果需要同一行上的结果,则需要联接(不是工会)例如:


希望我能正确理解你的问题

请检查下面的查询

select Date1 , Letter1 , Date2 , Letter2
from
(SELECT TOP 1 Date as Date1, Letter as Letter1 from TAB where
Letter = 'A'
order by Date DESC) a join 
(
SELECT TOP 1 Date as Date2, Letter as Letter2 from TAB where 
Letter = 'B'
order by Date DESC
) b;

这不是UNION所做的。它只是将给定的两个数据集合并为一个,正如您在示例中所看到的。您可以尝试使用如下答案中的一个案例:

如果我理解正确,您似乎想要“a”和“B”s位于列中的列表中。这实际上不是关系输出,因为行中的列彼此之间没有关系。但是,可以使用条件聚合: 前1名日期为日期1,字母为字母1

select max(case when letter = 'A' then date end) as date1,
       'A' as letter1,
       max(case when letter = 'B' then date end) as date2,
       'B' as letter2
from (select t.*,
             row_number() over (partition by letter order by date desc) as seqnum
      from t
      where letter in ('A', 'B')
     ) t
group by seqnum
order by seqnum;

我想这会有帮助。你用的是哪种数据库管理系统?嗨,戈登。非常感谢。你的解决方案非常好。我还添加了ID,我得到了我想要的。
select max(case when letter = 'A' then date end) as date1,
       'A' as letter1,
       max(case when letter = 'B' then date end) as date2,
       'B' as letter2
from (select t.*,
             row_number() over (partition by letter order by date desc) as seqnum
      from t
      where letter in ('A', 'B')
     ) t
group by seqnum
order by seqnum;