所有人都给出了正确的结果,而工会给出了错误的结果。
SELECT *
FROM (SELECT TOP 20 *
FROM noble_win
WHERE [YEAR ] = 1970
AND [SUBJECT ] NOT IN ('Economics', 'Chemistry')
ORDER BY [SUBJECT ],[WINNER ]) AS A
UNION
SELECT *
FROM (SELECT TOP 20 *
FROM noble_win
WHERE [YEAR ]=1970
AND [SUBJECT ] IN ('Economics', 'Chemistry')
ORDER BY [SUBJECT ],[WINNER ]) AS B
此查询先给出Chemistry
和Economics
行,然后给出Economics
和Chemistry
以外的行。但是,我首先要使用Economics
和Chemistry
以外的行。我不知道为什么会给出错误的结果。
答案 0 :(得分:7)
ORDER BY
必须在最外面的查询级别应用:
select * from
(select top 20 *,1 AS ord from noble_win
where [YEAR ]=1970 and [SUBJECT ] NOT IN ('Economics', 'Chemistry')
order by [SUBJECT ],[WINNER ]
) as A
union
select * from
(select top 20 *, 2 AS ord from noble_win
where [YEAR ]=1970 and [SUBJECT ] IN ('Economics', 'Chemistry')
order by [SUBJECT ],[WINNER ]
) as B
order by ord, [SUBJECT ],[WINNER ])
您是DBA Revenge竞赛的获胜者:带空格的标识符。
命名约定
- 保留
- 空白
- 隐藏
- 重复
- Unicode
答案 1 :(得分:0)
您可以使用CASE语句进行条件排序:
<Sidebar
sidebar={<b>Sidebar content</b>}
open={this.state.sidebarOpen}
onSetOpen={this.onSetSidebarOpen}
styles={{ sidebar: { background: "white" } }}
>
<button onClick={() => this.onSetSidebarOpen(true)}> //button as children
Open sidebar
</button>
</Sidebar>
答案 2 :(得分:0)
如果使用union
/ union all
,则order by
必须位于最外面的选择上。但是,您不需要它们。您可以只使用表达式和窗口函数:
select nw.*
from (select nw.*,
row_number() over (partition by year,
(case when [SUBJECT ] in ('Economics', 'Chemistry') then 1 else 2 end)
order by [SUBJECT ], [WINNER ])
) as seqnum
from noble_win nw
) nw
where seqnum <= 20
order by (case when [SUBJECT ] in ('Economics', 'Chemistry') then 1 else 2 end) desc,
[WINNER ]);
并为您修复列名!