0

Here is the query I am using:

select top 1 'Return To IPACS' as name, 'http://domain:88' as link 
union 
select name,link 
from jrm_intranetlinks l 
inner join jrm_intranetpermissions p on l.id = p.linkid 
where p.userid = 155
order by case name when 'Home' then 2 when 'Team' then 1 end desc, name

Here is the error message I am receiving:

Msg 104, Level 16, State 1, Line 1
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.

The bottom set above returns a list we use for link names, and the right column provides the path they link to. We need to add a default link for everyone which is why we are trying the union part since this link everyone will get and the other table displays links based on permission.

It works just fine without the order by clause, but i need the return to ipacs one at the top, then home, then team and rest ordered desc.

What am I doing wrong here?

2
  • Have you tried dropping the CASE? Also, TOP typically pairs with ORDER BY Commented Jul 2, 2013 at 21:49
  • @JasonMcCreary yeah it seems to work with out the case. But I need them sorted specifically for three of them. Might have to adopt the solution by Smandoli Commented Jul 2, 2013 at 21:53

6 Answers 6

2

How about:

select 
    'Return To IPACS' as name, 
    'http://domain:88' as link, 
    1 as sort_me 
union all
(select 
    name, 
    link, 
    2 as sort_me 
from jrm_intranetlinks l 
inner join jrm_intranetpermissions p on l.id = p.linkid 
where p.userid = 155 )
order by sort_me

I am worried about your nesting. I added some parenthesis. Anyway, the point is that you should just add values as shown to force the sort.

Sign up to request clarification or add additional context in comments.

1 Comment

I was able to adopt this and it works like a charm now. Thank you.
2
select 
    'Return To IPACS' as name, 
    'www.home.com' as link,
    3 sortOrder
union all
select 
    name, 
    link,
    case name 
        when 'Home' then 2 
        when 'Team' then 1 
    end sortOrder
from links l
inner join jrm_intranetpermissions p on l.id = p.linkid 
where p.userid = 155 
order by sortOrder desc

demo

Comments

1

What you have are two different "columns"...

l.[Name] 

and

(case [Name] when 'Home' then 2 when 'Team' then 1 end)

...the former is in the select list and the latter is not.

Several workarounds exist. My favorite is...

select top 1 'Return To IPACS' as [Name]
    ,'http://domain:88' as [Link]
    , 3 as [Order]

union 

select l.name as [Name]
    ,l.link as [Link]
    ,(case [Name] when 'Home' then 2 when 'Team' then 1 end) as [Order]
from jrm_intranetlinks l
inner join jrm_intranetpermissions p on l.id = p.linkid 
where p.userid = 155
order by [Order], [Name]

Comments

0

You are only selecting "name" and "link" so those are the only items you can use in your ORDER BY when you are using a UNION, INTERSECT, or EXPECT.

In otherwords, you need to add all the items in your 'order by' clause to your select statement.

4 Comments

I only have name and link in both selects? Do you know how the case messes this up?
the case in the order clause shouldn't be a problem
@andre.barata I can't think of what else it would be. It works without the case, and errors with it.
Your case statement seems off blog.sqlauthority.com/2007/07/17/…
0

Did you try making it an inner query and specifiyng the order outside?

Something like this:

select name, link
from (
    select top 1 'Return To IPACS' as name, 'http://domain:88' as link 
    union 
    select name,link 
    from jrm_intranetlinks l 
    inner join jrm_intranetpermissions p on l.id = p.linkid 
    where p.userid = 155
) tbl
order by case name when 'Home' then 2 when 'Team' then 1 end desc, name

1 Comment

also consider changing the union to union all, since the union keyword also makes the results distinct
0
select name,link from (
  select
    'Return To IPACS' as name,
    'http://domain:88' as link,
    0 as sort_order
  union 
  select
    name,
    link
    case name when 'Home' then 1 when 'Team' then 2 as sort_order
  from jrm_intranetlinks l 
  inner join jrm_intranetpermissions p on l.id = p.linkid 
  where p.userid = 155
) t
order by sort_order, name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.