Select TOP 1000 + 自定义用户 ID

Select TOP 1000 + custom user id's

除了用户 ID 的自定义列表(这些不属于前 1000 名的一部分)之外,我还想select基于收入的前 1000 个用户 ID

换句话说,我想在 1 个查询中包含以下查询:

SELECT TOP 1000 * FROM XX
ORDER BY REVENUE DESC

SELECT * FROM XX
WHERE USER_ID IN (CUSTOM_LIST)

您需要一个 Union,但由于 Set-Select 中不允许使用 TOP,您必须将其嵌套在 Derived Table:

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)