Hi I am using SQL Server 2012 and I have a table with 1000 questions in it as rows. I need to randomly select 20 of those questions and then insert them into a single row on another table as separate columns.
I want to do something like this.
select top 20 questionid from questions order by newid()
insert into questionsets values (q1,q2,q3,q4,q5,q6....)
I have tried doing this various ways but am having trouble getting my head around in in a not to expensive way.
Please try inserting after PIVOTing your result set:
INSERT INTO
questionsets (
Q1,
Q2,
.
.
Q20)
SELECT * FROM(
SELECT *, ROW_NUMBER() OVER (ORDER BY questionid) RNum FROM(
SELECT TOP 20 questionid FROM questions ORDER BY NEWID()
)x)y PIVOT (MAX(questionid) FOR RNum IN ([1], [2], [3], ..., [20]))as pvt
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.