如何在sql server中合并两行

问题描述 投票:0回答:2

我有一张类似这个的桌子

Date    | Cond   | Time
--------|--------|------
18/03/19|   1    | 13:07
18/03/19|   0    | 16:07

我希望有一个选择,可以生成类似于使用join或union或任何条件的东西

 Date    |Time1| Time2
 --------|-----|------
 18/03/19|13:07| 16:07

最好的祝福

sql sql-server
2个回答
4
投票

您可以使用条件聚合:

select date, max(case when cond = 1 then time end) as time_1,
       max(case when cond = 0 then time end) as time_0
from t
group by date
order by date;

0
投票

使用聚合函数

select date,min(time),max(time)
from table group by date
© www.soinside.com 2019 - 2024. All rights reserved.