0

除了自定义用户 ID 列表之外,我还想根据收入选择前 1000 个用户 ID(这些不属于前 1000 个)

换句话说,我希望在 1 个查询中使用以下查询:

SELECT TOP 1000 * FROM XX
ORDER BY REVENUE DESC

SELECT * FROM XX
WHERE USER_ID IN (CUSTOM_LIST)
4

1 回答 1

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)
于 2018-05-23T18:10:09.223 回答