除了用户ID的自定义列表(这些不是前1000名的一部分)之外,我想根据收入选择前1000个用户ID
换句话说,我想在下面的查询中查询:
SELECT TOP 1000 * FROM XX
ORDER BY REVENUE DESC
SELECT * FROM XX
WHERE USER_ID IN (CUSTOM_LIST)
答案 0 :(得分:1)
您需要一个联盟,但由于在Set-Select中不允许使用TOP,您必须将其嵌套在派生表中:
SELECT *
FROM
(
SELECT TOP 1000 * FROM XX
ORDER BY REVENUE DESC
) as dt
UNION
-- UNION ALL is more efficient, but a user from the custom list who is
-- also in the top 1000 would be returned twice
SELECT * FROM XX
WHERE USER_ID IN (CUSTOM_LIST)