爲了執行這個轉換,您將需要UNPIVOT
然後PIVOT
的數據。 UNPIVOT
將從您的q_optionID
和q_ans_text
列中取值,並將其轉換爲兩列,一列爲值和列名稱。
有兩種方法你可以PIVOT
這個,你可以使用靜態版本硬編碼所有的值,或者你可以使用動態sql。爲了UNPIVOT
數據您需要確保數據具有相同的數據類型,因此轉換可能是必要的。
靜態PIVOT:
select clientid, surveyid,
questionid,
value,
col
from
(
select clientid, surveyid, questionid,
cast(q_optionID as varchar(4)) q_optionID,
q_ans_text
from yourtable
) s
unpivot
(
value
for col in (q_optionID, q_ans_text)
) un
見SQL Fiddle with Demo
逆透視結果:
| CLIENTID | SURVEYID | QUESTIONID | VALUE | COL |
---------------------------------------------------------
| 1 | 1 | 1 | Yes | q_ans_text |
| 1 | 1 | 2 | 18 | q_optionID |
| 1 | 1 | 3 | 19 | q_optionID |
| 2 | 1 | 1 | No | q_ans_text |
| 2 | 1 | 2 | 18 | q_optionID |
| 2 | 1 | 3 | 19 | q_optionID |
| 3 | 2 | 1 | Yes | q_ans_text |
| 3 | 2 | 2 | 15 | q_optionID |
| 3 | 2 | 3 | 13 | q_optionID |
然後將應用PIVOT
的結果,讓您的最終產品。
select *
from
(
select clientid, surveyid,
'Q'+cast(questionid as varchar(10)) question,
value
from
(
select clientid, surveyid, questionid,
cast(q_optionID as varchar(4)) q_optionID,
q_ans_text
from yourtable
) s
unpivot
(
value
for col in (q_optionID, q_ans_text)
) un
) src
pivot
(
max(value)
for question in (Q1, Q2, Q3)
) piv
見SQL Fiddle with demo
動態PIVOT:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME('Q'+cast(questionid as varchar(10)))
from yourtable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT clientid, surveyid,' + @cols + ' from
(
select clientid, surveyid,
''Q''+cast(questionid as varchar(10)) question,
value
from
(
select clientid, surveyid, questionid,
cast(q_optionID as varchar(4)) q_optionID,
q_ans_text
from yourtable
) s
unpivot
(
value
for col in (q_optionID, q_ans_text)
) un
) x
pivot
(
max(value)
for question in (' + @cols + ')
) p '
execute(@query)
見SQL Fiddle with Demo
UNION ALL /骨料與案例版本:
現在,如果你在一個沒有PIVOT
然後一個系統的工作,你可以使用UNION ALL
到UNPIVOT
和聚合函數與CASE
到PIVOT
:
select clientid, surveyid,
max(case when questionid = 1 then value end) Q1,
max(case when questionid = 2 then value end) Q2,
max(case when questionid = 3 then value end) Q3
from
(
select clientid, surveyid, questionid, cast(q_optionID as varchar(10)) value, 'q_optionID' col
from yourtable
union all
select clientid, surveyid, questionid, q_ans_text value, 'q_ans_text' col
from yourtable
) unpiv
group by clientid, surveyid
見SQL Fiddle with Demo
所有三會產生相同的結果:
| CLIENTID | SURVEYID | Q1 | Q2 | Q3 |
---------------------------------------
| 1 | 1 | Yes | 18 | 19 |
| 2 | 1 | No | 18 | 19 |
| 3 | 2 | Yes | 15 | 13 |
+1涼爽演示 – brian
感謝您的快速回復......儘管數據類型不匹配。我試着在q_optionID上進行強制轉換或轉換,但它並不真正起作用。 – TehFoobar
@TehFoobar請參閱我編輯爲'UNPIVOT'數據類型必須相同的數據 – Taryn