看來您需要首先取消轉移列Column3
,Column4
和Column5
才能返回非空行,然後將Column2
項目轉爲列。如果您不知道列名,則需要在存儲過程中使用動態SQL。
在使用動態sql之前,我首先編寫查詢的靜態版本以獲取正確的邏輯。由於使用的是SQL Server 2012中,你可以使用CROSS APPLY和值unpivot的數據:
select
m.docId,
m.Date,
m.column1,
m.column2,
c.value
from dbo.mappingTest m
cross apply
(
values
('Column3', Column3),
('Column4', Column4),
('Column5', convert(varchar(10), Column5, 120))
) c (Col, Value)
where c.value is not null
見Demo。您的數據現在看起來是這樣:
| DOCID | DATE | COLUMN1 | COLUMN2 | VALUE |
|------------|------------|----------|--------------|---------|
| ABC00| 2014-04-11 | approval | project name | ABC |
| ABC00| 2014-04-11 | approval | article name | Art 01 |
| ABC00| 2014-04-11 | approval | customer | ACME |
| ABC00| 2014-04-11 | approval | department | Dept. A |
| ABC00| 2014-04-11 | approval | plant | Europe |
| ABC00| 2014-04-11 | approval | sop month | 10 |
你必須爲每個DocID
與最終你還是會希望在一列的Column2
項目下的數值多行。現在您可以應用PIVOT功能:
select
DocID,
Date,
Column1,
[project name], [article name], [customer],
[department], [plant], [sop month],
[sop year], [eop month], [eop year], [budget], [savings]
from
(
select
m.docId,
m.Date,
m.column1,
m.column2,
c.value
from dbo.mappingTest m
cross apply
(
values
('Column3', Column3),
('Column4', Column4),
('Column5', convert(varchar(10), Column5, 120))
) c (Col, Value)
where c.value is not null
) d
pivot
(
max(value)
for column2 in ([project name], [article name], [customer],
[department], [plant], [sop month],
[sop year], [eop month], [eop year], [budget], [savings])
) p
請參閱SQL Fiddle with Demo。我已經包含了所有列名稱,但在PIVOT IN
中,您只包含您想要作爲新列的值。
現在,如果你想使用動態SQL,你會調整代碼爲:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(COLUMN2)
from dbo.mappingTest
group by COLUMN2
order by COLUMN2
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query
= 'SELECT
DocID,
Date,
Column1,' + @cols + '
from
(
select
m.docId,
m.Date,
m.column1,
m.column2,
c.value
from dbo.mappingTest m
cross apply
(
values
(''Column3'', Column3),
(''Column4'', Column4),
(''Column5'', convert(varchar(10), Column5, 120))
) c (Col, Value)
where c.value is not null
) d
pivot
(
min(value)
for column2 in (' + @cols + ')
) p '
exec sp_executesql @query
見SQL Fiddle with Demo。兩種版本都會給出以下結果:
| DOCID | DATE | COLUMN1 | ARTICLE NAME | BUDGET | CUSTOMER | DEPARTMENT | EOP MONTH | EOP YEAR | PLANT | PROJECT NAME | SAVINGS | SOP MONTH | SOP YEAR |
|------------|------------|----------|--------------|--------|----------|------------|-----------|----------|--------|--------------|---------|-----------|----------|
| ABC00| 2014-04-11 | approval | Art 01 | 17890 | ACME | Dept. A | 0 | 21019 | Europe | ABC | (null) | 10 | 2014 |
| ABC00| 2014-04-11 | project | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | -0,020 | (null) | (null) |
| DEF00| 2014-05-11 | approval | Art 02 | (null) | (null) | (null) | (null) | (null) | (null) | DEF | (null) | (null) | (null) |
列標題是固定的,不能依賴於數據,除非您使用動態SQL。 – 2014-12-02 14:32:42
你可以在sql小提琴中使用減價輸出來包含你的問題中的所有sql?另外,您是否可以根據該數據集顯示所需的輸出(如果需要,可以修剪)? – Tanner 2014-12-02 14:33:22