![](/img/trans.png)
[英]SQL Server: Combine several SELECT statements with “WITH” part into a UNION
[英]SQL - combine UNION and SORT BY for several statements
我有一個100.000用戶的列表,我需要查詢電子郵件域並得分為10000。 首先需要取得4分。 如果沒有足夠的分數,則得分為3,依此類推。優先級查詢的含義我收到的解決方案對於一條語句工作正常
SELECT TOP 10000 *
FROM [table]
WHERE Email like '%@test.com' and score in ( 1, 2, 3, 4)
order by score desc
當我嘗試將其用於多個語句時,我收到無效的語法錯誤:
SELECT TOP 10000 * FROM
[table]
WHERE Email like '%@test.com'and score in ( 1, 2)
order by score desc
UNION
SELECT TOP 1000 * FROM
[table] WHERE Email like '%@test2.com' and score in ( 1, 2, 3, 4)
order by score desc
我需要能夠添加任意數量的EMAILS。 非常感謝您的幫助:)
從第一個查詢中刪除訂單。
只需在底部留下訂單,然后刪除其他訂單
SELECT TOP 10000 * FROM [table]
WHERE Email like '%@test.com' and score in ( 1, 2)
UNION
SELECT TOP 1000 * FROM [table]
WHERE Email like '%@test2.com' and score in ( 1, 2, 3, 4)
order by score desc
否則,如果您需要在每個序列中選擇有序的值,請選擇單獨的構建,然后選擇uing()
select TOP 11000 * from
(SELECT TOP 10000 * FROM [table]
WHERE Email like '%@test.com' and score in ( 1, 2)
order by score desc ) t1
UNION
select * from
( SELECT TOP 1000 * FROM [table]
WHERE Email like '%@test2.com' and score in ( 1, 2, 3, 4)
order by score desc ) t2
ORDER BY score DESC
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.