कुछ तरीके हैं जिनसे आप ऐसा कर सकते हैं।
यदि आपके पास ज्ञात प्रश्नों/उत्तरों की संख्या है तो आप row_number()
. का उपयोग कर सकते हैं एक समग्र कार्य और एक CASE व्यंजक के साथ:
select id,
max(case when rn = 1 then question end) question1,
max(case when rn = 1 then answer end) answer1,
max(case when rn = 2 then question end) question2,
max(case when rn = 2 then answer end) answer2,
max(case when rn = 3 then question end) question3,
max(case when rn = 3 then answer end) answer3
from
(
select id, question, answer,
row_number() over(partition by id order by id, question) rn
from yt
) src
group by id;
देखें SQL Fiddle with Demo
एक अन्य सुझाव परिणाम प्राप्त करने के लिए UNPIVOT और PIVOT फ़ंक्शन दोनों का उपयोग करना होगा। UNPIVOT आपका question
लेगा और answer
कॉलम और उन्हें कई पंक्तियों में परिवर्तित करें।
UNPIVOT का मूल सिंटैक्स होगा:
select id,
col+cast(rn as varchar(10)) col,
value
from
(
-- when you perform an unpivot the datatypes have to be the same.
-- you might have to cast the datatypes in this query
select id, question, cast(answer as varchar(500)) answer,
row_number() over(partition by id order by id, question) rn
from yt
) src
unpivot
(
value
for col in (question, answer)
) unpiv;
देखें डेमो . यह एक परिणाम देता है:
| ID | COL | VALUE |
--------------------------------------------------------------
| 4482515 | question1 | I would like to be informed by mail. |
| 4482515 | answer1 | No |
| 4482515 | question2 | Plan to Purchase? |
| 4482515 | answer2 | Over 12 months |
| 4482515 | question3 | Test Question Text |
| 4482515 | answer3 | some Answer |
जैसा कि आप देख सकते हैं, मैंने एक row_number()
जोड़ा है प्रारंभिक सबक्वेरी के लिए मूल्य ताकि आप प्रत्येक उत्तर को प्रश्न से जोड़ सकें। एक बार जब यह अप्रकाशित हो जाता है, तो आप question
के साथ परिणाम को नए कॉलम नामों पर पिवट कर सकते हैं /answer
संयोजित पंक्ति संख्या मान के साथ। PIVOT सिंटैक्स वाला कोड होगा:
select id, question1, answer1, question2, answer2,
question3, answer3
from
(
select id,
col+cast(rn as varchar(10)) col,
value
from
(
-- when you perform an unpivot the datatypes have to be the same.
-- you might have to cast the datatypes in this query
select id, question, cast(answer as varchar(500)) answer,
row_number() over(partition by id order by id, question) rn
from yt
) src
unpivot
(
value
for col in (question, answer)
) unpiv
) d
pivot
(
max(value)
for col in (question1, answer1, question2, answer2,
question3, answer3)
) piv;
देखें SQL Fiddle with Demo . अब आपकी स्थिति में, आपने कहा है कि आपके पास प्रश्नों/उत्तरों की एक गतिशील संख्या होगी। यदि ऐसा है, तो आपको परिणाम प्राप्त करने के लिए गतिशील SQL का उपयोग करने की आवश्यकता होगी:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(c.col+cast(rn as varchar(10)))
from
(
select row_number() over(partition by id
order by id, question) rn
from yt
) d
cross apply
(
select 'question' col, 1 sort union all select 'answer', 2
) c
group by col, rn, sort
order by rn, sort
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT id, ' + @cols + '
from
(
select id,
col+cast(rn as varchar(10)) col,
value
from
(
-- when you perform an unpivot the datatypes have to be the same.
-- you might have to cast the datatypes in this query
select id, question, cast(answer as varchar(500)) answer,
row_number() over(partition by id order by id, question) rn
from yt
) src
unpivot
(
value
for col in (question, answer)
) unpiv
) d
pivot
(
max(value)
for col in (' + @cols + ')
) p '
execute(@query);
देखें SQL Fiddle with Demo . ये परिणाम देते हैं:
| ID | QUESTION1 | ANSWER1 | QUESTION2 | ANSWER2 | QUESTION3 | ANSWER3 |
------------------------------------------------------------------------------------------------------------------------------------
| 4482515 | I would like to be informed by mail. | No | Plan to Purchase? | Over 12 months | Test Question Text | some Answer |