以下是我正在使用的查询:
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
以下是我收到的错误消息:
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.
上面的底部集返回我们用于链接名称的列表,右列提供了它们链接到的路径。我们需要为每个人添加一个默认链接,这就是我们尝试联合部分的原因,因为每个人都会得到这个链接,另一个表根据权限显示链接。
没有order by子句就可以正常工作,但我需要在顶部返回ipacs,然后返回home,然后是团队和休息命令desc。
我在这里做错了什么?
答案 0 :(得分:2)
怎么样:
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
我担心你的筑巢。我添加了一些括号。无论如何,重点是你应该只添加如图所示的值来强制排序。
答案 1 :(得分: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
答案 2 :(得分:1)
你有两个不同的“专栏”......
l.[Name]
和
(case [Name] when 'Home' then 2 when 'Team' then 1 end)
......前者在选择列表中而后者不在。
存在一些变通方法。我最喜欢的是......
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]
答案 3 :(得分:0)
您只是选择“名称”和“链接”,因此当您使用ORDER BY
,UNION
或{{INTERSECT
时,这些是您在EXPECT
中可以使用的唯一项目1}}。
换句话说,您需要将“order by
”子句中的所有项添加到select语句中。
答案 4 :(得分:0)
您是否尝试将其作为内部查询并在外部指定订单?
这样的事情:
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
答案 5 :(得分: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