选择TOP 1000 +自定义用户ID

时间:2018-05-23 13:36:03

标签: teradata teradata-sql-assistant

除了用户ID的自定义列表(这些不是前1000名的一部分)之外,我想根据收入选择前1000个用户ID

换句话说,我想在下面的查询中查询:

SELECT TOP 1000 * FROM XX
ORDER BY REVENUE DESC

SELECT * FROM XX
WHERE USER_ID IN (CUSTOM_LIST)

1 个答案:

答案 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)