简体   繁体   English

数据透视表获取要显示的数据是这样的吗?

[英]Pivot table to get data to be displayed like this?

Bit of an SQL newbie here. SQL新手介绍。 I have an SQL table which stores data at specific dates for a specific unique identifier. 我有一个SQL表,该表在特定日期存储特定唯一标识符的数据。

Example: 例:

UniqueIdentifier    someData1   Date1         SomeData2     Date2       SomeData3   Date3
1                   1000        01/01/2017    2000          01/02/2017  2000        01/03/2017
2                   2000        21/01/2017    2500          21/02/2017  3540        21/03/2017
3                   3000        05/01/2017    3000          08/07/2016  3000        12/05/2016

I would like to somehow convert the data so it ends up like this: 我想以某种方式转换数据,使其最终像这样:

UniqueIdentifier    Data    Date
1                   1000    01/01/2017
1                   2000    01/02/2017
1                   2000    01/03/2017
2                   2000    21/01/2017
2                   2500    21/02/2017
2                   3540    21/03/2017
3                   3000    05/01/2017
3                   3000    08/07/2016
3                   3000    12/05/2016

Basically extracting each date and it's corresponding value into a separate line. 基本上将每个日期及其对应的值提取到单独的行中。 I'm guessing I need to use some sort of pivot table to do this, but I'm not quite sure exactly how to do it! 我猜想我需要使用某种类型的数据透视表来执行此操作,但是我不太确定该怎么做! Any help would be greatly appreciated! 任何帮助将不胜感激!

Using Union 使用联盟

select UniqueIdentifier, someData1 as 'Data', Date1 as 'Date' from <yourtable>   
union
select UniqueIdentifier, someData2 as 'Data', Date2 as 'Date' from <yourtable>
union
select UniqueIdentifier, someData3 as 'Data', Date3 as 'Date' from <yourtable>

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

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