SQL PIVOT with dynamic header

2019-07-27 18:31发布

问题:

I have a SQL database table with the following structure from witch I'd like to give my users the option to export data in either a vertically or horizontally fashion.

SELECT [MEA_ID], [MEA_DateTime], [PTR_ID], [EXP_ID], [DUN_ID], [MEA_Value]
FROM [MeasurementData];

|--------|----------------------|--------|--------|--------|-----------|
| MEA_ID |     MEA_DateTime     | PTR_ID | EXP_ID | DUN_ID | MEA_Value |
|--------|----------------------|--------|--------|--------|-----------|
| 1      | 2009-08-10 00:00:00  | 24     | 14     | 2      | 15.1      |
| 2      | 2009-08-10 00:00:00  | 24     | 14     | 3      | 14.3      |
| 3      | 2009-08-10 00:00:00  | 24     | 14     | 4      | 16.7      |
| 4      | 2009-08-10 00:00:10  | 24     | 15     | 2      | 13.0      |
| 5      | 2009-08-10 00:00:10  | 24     | 15     | 4      | 13.4      |
| 6      | 2009-08-10 00:00:20  | 24     | 16     | 2      | 17.8      |
| 7      | 2009-08-10 00:00:20  | 24     | 16     | 3      | 17.7      |
| 8      | 2009-08-10 00:00:20  | 24     | 16     | 4      | 16.2      |
| 9      | 2009-08-10 00:00:00  | 25     | 14     | 3      | 34.0      |
| 10     | 2009-08-10 00:00:00  | 25     | 14     | 4      | 19.0      |
| 11     | 2009-08-10 00:00:10  | 25     | 15     | 2      | 22.1      |
| 12     | 2009-08-10 00:00:10  | 25     | 15     | 3      | 23.1      |
| 13     | 2009-08-10 00:00:20  | 25     | 16     | 2      | 24.6      |
| 14     | 2009-08-10 00:00:20  | 25     | 16     | 3      | 18.3      |
| 15     | 2009-08-10 00:00:20  | 25     | 16     | 4      | 18.2      |

This above table would be the vertical export.

Every combination of MEA_DateTime, PTR_ID, EXP_ID and DUN_ID is unique, so there can always only be 1 row with a given combination. What I am trying to accomplish is to turn the DUN_ID horizontally, to better be able to compare values.

It should look like this:

SELECT [MEA_DateTime], [PTR_ID], [EXP_ID], [MEA_Value]
FROM [MeasurementData]
PIVOT
(
    SUM([MEA_Value])
    FOR [DUN_ID] IN (????)
);

                                          DUN_ID  DUN_ID  DUN_ID
                                             |       |       |
                                             v       v       v
|----------------------|--------|--------|-------|-------|-------|
|     MEA_DateTime     | PTR_ID | EXP_ID |   2   |   3   |   4   |
|----------------------|--------|--------|-------|-------|-------|
| 2009-08-10 00:00:00  | 24     | 14     | 15.1  | 14.3  | 16.7  |
| 2009-08-10 00:00:10  | 24     | 15     | 13.0  | NULL  | 13.4  |
| 2009-08-10 00:00:20  | 24     | 16     | 17.8  | 17.7  | 16.2  |
| 2009-08-10 00:00:00  | 25     | 14     | NULL  | 34.0  | 19.0  |
| 2009-08-10 00:00:10  | 25     | 15     | 22.1  | 23.1  | NULL  |
| 2009-08-10 00:00:20  | 25     | 16     | 24.6  | 18.3  | 18.2  |

I tried to make it work with a PIVOT, but unfortunately never did something like that before and don't have much to show. From what I could figure out you'd need to know the column header names beforehand for it to work and I couldn't figure out how to use field values as column headers. Is what I'm trying to do possible or should I just build that structure manually in python afterwards?

Glad for any help.

EDIT: The database engine is Microsoft SQL.

EDIT: Solution in action: http://sqlfiddle.com/#!6/3afd7/1/0

回答1:

You can use this query.

DECLARE @ColNames NVARCHAR(MAX) = ''
SELECT @ColNames = @ColNames + ', ' + QUOTENAME( DUN_ID ) FROM MyTable
GROUP BY DUN_ID

DECLARE @SqlText NVARCHAR(MAX) = '
SELECT * FROM (SELECT MEA_DateTime, PTR_ID, EXP_ID, DUN_ID, MEA_Value FROM MyTable ) SRC
PIVOT( MAX(MEA_Value) FOR DUN_ID IN ( '+ STUFF(@ColNames,1,1,'') +') ) AS PVT ORDER BY PTR_ID, EXP_ID'

EXEC(@SqlText)

Result:

MEA_DateTime            PTR_ID      EXP_ID      2          3        4       
----------------------- ----------- ----------- ---------- -------- --------
2009-08-10 00:00:00.000 24          14          15.10      14.30    16.70
2009-08-10 00:00:10.000 24          15          13.00      NULL     13.40
2009-08-10 00:00:20.000 24          16          17.80      17.70    16.20
2009-08-10 00:00:00.000 25          14          NULL       34.00    19.00
2009-08-10 00:00:10.000 25          15          22.10      23.10    NULL
2009-08-10 00:00:20.000 25          16          24.60      18.30    18.20