我不认为SQL Pivot表可以解决我的问题,但现在这是我的最佳猜测解决方案。我不是SQL专家,所以我所有的选择都是经过研究
我使用的一个软件每天向Microsoft SQL发送一份报告,该报告中包含约35,000行数据,所有这些行都包含有关我使用的应用程序的不同统计信息。现在,虽然我不需要每一行的数据点,但也许我需要110数据行。
我当前获得的列在所附的屏幕截图中
当前样本:
+---------+-----------+----------------------+------------+-----------------+
| Data_id | report_id | data_point_group_key | data_point | data_point_value|
+---------+-----------+----------------------+------------+-----------------+
| 1 | 517 | 111 | Table Name | Work_state |
+---------+-----------+----------------------+------------+-----------------+
| 2 | 517 | 111 | # Rows | 3 |
+---------+-----------+----------------------+------------+-----------------+
| 3 | 517 | 111 | Size | 100 |
+---------+-----------+----------------------+------------+-----------------+
| 4 | 517 | 222 | Mode Name | New App |
+---------+-----------+----------------------+------------+-----------------+
| 5 | 517 | 333 | Total Size | 5000000 |
+---------+-----------+----------------------+------------+-----------------+
| 6 | 517 | 444 | Table Name | Offline Users |
+---------+-----------+----------------------+------------+-----------------+
| 7 | 517 | 444 | # Rows | 100 |
+---------+-----------+----------------------+------------+-----------------+
| 8 | 517 | 444 | Size | 500 |
+---------+-----------+----------------------+------------+-----------------+
| 9 | 517 | 555 | URI | C:\File1 |
+---------+-----------+----------------------+------------+-----------------+
我必须将所有这些数据移动到一个辅助工具中,该工具需要通过列方法而不是行方法来摄取数据。
我毫无疑问地开始输入110行,但是我正试图从长远来看使这一过程自动化。
我从此SQL命令开始进行转换,它不能完全正常工作,但它是一个 开始我想
SELECT * FROM
(
SELECT [data_id],
[report_id],
[data_point],
[data_point_value]
FROM dbo.tblReportData
) AS SourceTable PIVOT(MAX([report_id]) FOR [data_point_value] IN([Default Time Zone],
[Version],
[File Path],
[# of Active Data Feeds],
[Log Path])) AS PivotTable;
所需输出
+---------+-----------+------------+-----------+-----+-----------+--------------+-------+
| Data_id | report_id | Table Name | # of Rows | Size| Mode Name | Total Size | URI |
+---------+-----------+------------+-----------+-----+-----------+--------------+-------+
| 1 | 517 | Work State| 3 | 100 | NULL | NULL | NULL |
+---------+-----------+------------+-----------+-----+-----------+--------------+-------+
| 2 | 517 | NULL | NULL | NULL| New App | NULL | NULL |
+---------+-----------+------------+-----------+-----+-----------+--------------+-------+
| 3 | 517 | NULL | NULL | NULL| NULL | 500000 | NULL |
+---------+-----------+------------+-----------+-----+-----------+--------------+-------+
| 4 | 517 |Offline user| 100 | 500 | NULL | NULL | NULL |
+---------+-----------+------------+-----------+-----+-----------+--------------+-------+
| 5 | 517 | NULL | NULL | NULL| NULL | NULL |C:\File|
+---------+-----------+------------+-----------+-----+-----------+--------------+-------+
答案 0 :(得分:0)
您可以尝试将CASE WHEN
与MAX
聚合函数结合使用来创建数据透视表。
TestDLL
CREATE TABLE tblReportData(
Data_id INT,
report_id INT,
data_point_group_key INT,
data_point VARCHAR(50),
data_point_value VARCHAR(50)
);
INSERT INTO tblReportData VALUES ( 1,517 , 111 ,'Table Name','Work_state');
INSERT INTO tblReportData VALUES ( 2,517 , 111 ,'# Rows','3');
INSERT INTO tblReportData VALUES ( 3,517 , 111 ,'Size','100');
INSERT INTO tblReportData VALUES ( 4,517 , 222 ,'Mode Name','New App');
INSERT INTO tblReportData VALUES ( 5,517 , 333 ,'Total Size','5000000');
INSERT INTO tblReportData VALUES ( 6,517 , 444 ,'Table Name','Offline Users');
INSERT INTO tblReportData VALUES ( 7,517 , 444 ,'# Rows','100');
INSERT INTO tblReportData VALUES ( 8,517 , 444 ,'Size','500');
INSERT INTO tblReportData VALUES ( 9,517 , 555 ,'URI','C:\File1');
查询
SELECT Row_number() over(order by data_point_group_key) 'Data_id',
report_id,
MAX(CASE WHEN data_point = 'Table Name' THEN data_point_value END) as 'Table Name',
MAX(CASE WHEN data_point = '# Rows' THEN data_point_value END),
MAX(CASE WHEN data_point = 'Size' THEN data_point_value END) as 'Size',
MAX(CASE WHEN data_point = 'Mode Name' THEN data_point_value END) as 'Mode Name',
MAX(CASE WHEN data_point = 'Total Size' THEN data_point_value END)as 'Total Size',
MAX(CASE WHEN data_point = 'URI' THEN data_point_value END) as 'URI'
FROM tblReportData
group by report_id,data_point_group_key
[结果] :
| Data_id | report_id | Table Name | # of Rows | Size | Mode Name | Total Size | URI |
|---------|-----------|---------------|-----------|--------|-----------|------------|----------|
| 1 | 517 | Work_state | 3 | 100 | (null) | (null) | (null) |
| 2 | 517 | (null) | (null) | (null) | New App | (null) | (null) |
| 3 | 517 | (null) | (null) | (null) | (null) | 5000000 | (null) |
| 4 | 517 | Offline Users | 100 | 500 | (null) | (null) | (null) |
| 5 | 517 | (null) | (null) | (null) | (null) | (null) | C:\File1 |