将数据从单个 table 转换为这种特定格式的 T-SQl 语法是什么?
What is the T-SQl syntax to transform data from a single table into this specific format?
我正在使用 SQL Server 2014,并且我的数据库中有一个名为 MyTable 的 table。其中摘录如下:
Hotel Market Desc Jan16 Feb16
ABC France SUP HB 158 33
ABC France SUP AI 35 6
ABC France DLX HB 27 10
我需要转换这个 table 以便我的 SQL 查询输出如下:
Hotel Market Desc Date Values
ABC France SUP HB Jan16 158
ABC France SUP HB Feb16 33
ABC France SUP AI Jan16 35
ABC France SUP AI Feb16 6
ABC France DLX HB Jan16 27
ABC France DLX HB Feb16 10
如何使用 T-SQL 实现此输出?在网上阅读了一些关于这个问题的内容后,我相信 UNPIVOT 语法可能会解决问题,但我完全不知道如何在这种情况下使用它。
一种简单的方法是使用 UNION
:
SELECT Hotel, Market, Desc, 'Jan16' AS Date, Jan16 AS Values
FROM MyTable
UNION ALL
SELECT Hotel, Market, Desc, 'Feb16', Feb16
FROM MyTable
如果您希望得到比这更复杂的东西,例如超过两列,那么你可以尝试使用 UNPIVOT
.
select *
from mytable unpivot ("values" for "date" in (Jan16,Feb16)) u
+-------+--------+--------+--------+-------+
| Hotel | Market | Desc | values | date |
+-------+--------+--------+--------+-------+
| ABC | France | SUP HB | 158 | Jan16 |
+-------+--------+--------+--------+-------+
| ABC | France | SUP HB | 33 | Feb16 |
+-------+--------+--------+--------+-------+
| ABC | France | SUP AI | 35 | Jan16 |
+-------+--------+--------+--------+-------+
| ABC | France | SUP AI | 6 | Feb16 |
+-------+--------+--------+--------+-------+
| ABC | France | DLX HB | 27 | Jan16 |
+-------+--------+--------+--------+-------+
| ABC | France | DLX HB | 10 | Feb16 |
+-------+--------+--------+--------+-------+
select Hotel , Market,[desc],marks,Date
from #c s
unpivot
(
marks
for date in (jan16, feb16)
) u;
或
2)
SELECT HOTEL ,MARKET,A.[DESC] MARKET,[DATE] ,V.[DESC]
FROM #C A
CROSS APPLY (VALUES('JAN16', JAN16),
('FEB16', FEB16))
V (DATE, [DESC]);
output
Hotel Market desc marks Date
ABC France SUP HB 158 Jan16
ABC France SUP HB 33 Feb16
ABC France SUP AI 35 Jan16
ABC France SUP AI 6 Feb16
ABC France DLX HB 27 Jan16
ABC France DLX HB 10 Feb16
我正在使用 SQL Server 2014,并且我的数据库中有一个名为 MyTable 的 table。其中摘录如下:
Hotel Market Desc Jan16 Feb16
ABC France SUP HB 158 33
ABC France SUP AI 35 6
ABC France DLX HB 27 10
我需要转换这个 table 以便我的 SQL 查询输出如下:
Hotel Market Desc Date Values
ABC France SUP HB Jan16 158
ABC France SUP HB Feb16 33
ABC France SUP AI Jan16 35
ABC France SUP AI Feb16 6
ABC France DLX HB Jan16 27
ABC France DLX HB Feb16 10
如何使用 T-SQL 实现此输出?在网上阅读了一些关于这个问题的内容后,我相信 UNPIVOT 语法可能会解决问题,但我完全不知道如何在这种情况下使用它。
一种简单的方法是使用 UNION
:
SELECT Hotel, Market, Desc, 'Jan16' AS Date, Jan16 AS Values
FROM MyTable
UNION ALL
SELECT Hotel, Market, Desc, 'Feb16', Feb16
FROM MyTable
如果您希望得到比这更复杂的东西,例如超过两列,那么你可以尝试使用 UNPIVOT
.
select *
from mytable unpivot ("values" for "date" in (Jan16,Feb16)) u
+-------+--------+--------+--------+-------+
| Hotel | Market | Desc | values | date |
+-------+--------+--------+--------+-------+
| ABC | France | SUP HB | 158 | Jan16 |
+-------+--------+--------+--------+-------+
| ABC | France | SUP HB | 33 | Feb16 |
+-------+--------+--------+--------+-------+
| ABC | France | SUP AI | 35 | Jan16 |
+-------+--------+--------+--------+-------+
| ABC | France | SUP AI | 6 | Feb16 |
+-------+--------+--------+--------+-------+
| ABC | France | DLX HB | 27 | Jan16 |
+-------+--------+--------+--------+-------+
| ABC | France | DLX HB | 10 | Feb16 |
+-------+--------+--------+--------+-------+
select Hotel , Market,[desc],marks,Date
from #c s
unpivot
(
marks
for date in (jan16, feb16)
) u;
或 2)
SELECT HOTEL ,MARKET,A.[DESC] MARKET,[DATE] ,V.[DESC]
FROM #C A
CROSS APPLY (VALUES('JAN16', JAN16),
('FEB16', FEB16))
V (DATE, [DESC]);
output
Hotel Market desc marks Date
ABC France SUP HB 158 Jan16
ABC France SUP HB 33 Feb16
ABC France SUP AI 35 Jan16
ABC France SUP AI 6 Feb16
ABC France DLX HB 27 Jan16
ABC France DLX HB 10 Feb16