gpt4 book ai didi

sql - 我想转表

转载 作者:行者123 更新时间:2023-12-01 12:09:23 26 4
gpt4 key购买 nike

我想按照下面给出的格式对数据进行透视,在下面,我放置了一个代码,用于使用我已有的数据创建表。

CREATE TABLE #temp
(
SHOW VARCHAR(100),
moviename VARCHAR(100),
ticketrate INT,
collectiondate DATETIME,
totalcaroccupancy DECIMAL(18,2)
)

INSERT INTO #temp
(
SHOW,
moviename,
ticketrate,
collectiondate,
totalcaroccupancy
)
SELECT 'Evening','DASSEHRA',70,'2018-10-26 00:00:00.000','11440.00' union all
SELECT 'Evening','DASSEHRA',70,'2018-10-27 00:00:00.000','16016.00' union all
SELECT 'Night','Test Thugs',70,'2018-10-26 00:00:00.000','14560.00' union all
SELECT 'Night','Test Thugs',70,'2018-10-27 00:00:00.000','15600.00'

我当前的表数据如下:

SHOW       moviename     ticketrate    collectiondate      totalcaroccupancy
Evening DASSEHRA 70 2018-10-26 00:00:00.000 11440.00
Evening DASSEHRA 70 2018-10-27 00:00:00.000 16016.00
Night Test Thugs 70 2018-10-26 00:00:00.000 14560.00
Night Test Thugs 70 2018-10-27 00:00:00.000 15600.00

我想要如下输出:

collectiondate             eveningticketrate    eveningticketrate    eveningMovieName    nightMovieName    eveningtotalcaroccupancy    nighttotalcaroccupancy
2018-10-26 00:00:00.000 70 70 DASSEHRA Test Thugs 11440.00 14560.00
2018-10-27 00:00:00.000 70 70 DASSEHRA Test Thugs 16016.00 15600.00

最佳答案

只需使用如下所示的条件 case 语句

select  collectiondate,
max(case when SHOW = 'Evening' then ticketrate end) as eveningticketrate,
max(case when SHOW = 'Night' then ticketrate end) as nightticketrate,
max(case when SHOW = 'Evening' then moviename end) as eveningmoviename,
max(case when SHOW = 'Night' then moviename end) as nightmoviename,
max(case when SHOW = 'Evening' then totalcaroccupancy end) as eveningtotalcaroccupancy,
max(case when SHOW = 'Night' then totalcaroccupancy end) as nighttotalcaroccupancy
from #temp
group by collectiondate

关于sql - 我想转表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53387647/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com