Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the error
Quote:
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.

I want to get random question every time so i use
SQL
order by newid()
<pre lang="sql">select top (4)* from
 (
       select  q.Description,q.Ques_Id,ac.ch_number,ac.Choice
       from Question as q,  course as c,Ques_Choice as ac
        where (q.crs_id = c.crs_id) and (q.Ques_Id=ac.Ques_Id)and (c.crs_name='oop' and q.type=2)
        order by newid()
  ) as alr

pivot
  (
      min(Choice)
      for ch_number in (a,b,c,d)
  ) as pvt

  union
  select top (2)* from
 (
       select  q.Description,q.Ques_Id,ac.ch_number,ac.Choice
       from Question as q,  course as c,Ques_Choice as ac
        where (q.crs_id = c.crs_id) and (q.Ques_Id=ac.Ques_Id)and (c.crs_name='oop' and q.type=1)
        order by newid()
  ) as alr

pivot
  (
      min(Choice)
      for ch_number in (a,b,c,d)
  ) as pvt</pre>
Posted
Updated 20-Dec-14 0:26am
v3
Comments
Maciej Los 20-Dec-14 15:41pm    
Please, post sample data and show us expected output...

1 solution

Don't take my word for it but I seem to remember that when I used a UNION query the ORDER BY came after the UNION combination thus applying to all elements. In other words there was only one ORDER BY and not two, one for each part.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900