Shuffle records and sequential records in sql server -


i have written sql query returns me shuffled data using newid() in sql server 2012.

scenario: have table in have questions in table - "tblquest" whereas in "tblquestlinked" table have linked question related main question in "tblquest" table, below query outputs shuffled data correctly.

select row_number() on (order newid()) sno,* (select q.id [qid], q.question, q.solution, isnull(q.islinked,0) islinked, ql.linkquestion tblquest q left join tblquestlinked ql on q.id = ql.qid) 

enter image description here

i want dataset returned query should have linked question in it, should not shuffled, instead should next row linked "main" question.

edit

as these set of questions presented online examination application, shuffling of questions must.

one main question can have 0 many linked questions. , linked question appears next row corresponding 'main" question. passed ui , provide questions based on sno ( serial no)

please find screenshot (desired result):

enter image description here

schema scripts :

create table [dbo].[tblquest]( [id] [int] identity(1,1) not null, [islinked] [bit] null, [question] [nvarchar](500) null, [solution] [nvarchar](500) null, constraint [pk_tblquest] primary key clustered  ( [id] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off,        allow_row_locks = on, allow_page_locks = on) on [primary] ) go  create table [dbo].[tblquestlinked]( [id] [int] identity(1,1) not null, [qid] [int] null, [linkquestion] [nvarchar](max) null, [createddate] [datetime] null,  constraint [pk_tblquestlinked] primary key clustered  ( [id] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off,    allow_row_locks = on, allow_page_locks = on) on [primary] ) go   insert [dbo].[tblquest] ([islinked], [question], [solution]) values (0, n'which of ... 1 ', 'solution 1 ... ') insert [dbo].[tblquest] ([islinked], [question], [solution]) values (0, n'which of ... 2 ', 'solution 2 ... ') insert [dbo].[tblquest] ([islinked], [question], [solution]) values (0, n'which of ... 3 ', 'solution 3 ... ') insert [dbo].[tblquest] ([islinked], [question], [solution]) values (0, n'which of ... 4 ', 'solution 4 ... ') insert [dbo].[tblquest] ([islinked], [question], [solution]) values (0, n'which of ... 5 ', 'solution 5 ... ') insert [dbo].[tblquest] ([islinked], [question], [solution]) values (0, n'which of ... 6 ', 'solution 6 ... ') insert [dbo].[tblquest] ([islinked], [question], [solution]) values (1, n'which of ... 7 ', 'solution 7 ... ')   insert [dbo].[tblquestlinked] ( [qid], [linkquestion]) values (7, n'linked q : sub linked') 

if understood correctly, want linked questions under main question. 1 way that:

select   q.id, case when type = 0 question else linkquestion end, solution (   select row_number() on (order newid()) ord, *   tblquest q ) q outer apply (   select 0 type, 0 qid, convert(varchar(max), null) linkquestion   union   select 1, qid, l.linkquestion   tblquestlinked l q.id = l.qid ) x order q.ord, x.type, x.qid 

example in sql fiddle


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -