我正在尝试将 ROW_NUMBER()... 设置为别名,以便可以在 OFFSET 中引用它。例如OFFSET some_alias - 1
。我需要从更大的查询中获取包含 ROW_NUMBER() 的单行。这是我的工作代码(得到正确的 ROW_NUMBER(),但没有被正确的数量抵消):
WITH FirstQuery AS (
SELECT "RepInitials", COUNT("OrderStatus"), ROW_NUMBER()OVER(ORDER BY COUNT("OrderStatus") DESC)
FROM "tblBulkSalesQuery"
WHERE "OrderStatus" = 'CMC'
GROUP BY "RepInitials"
)
SELECT "RepInitials", COUNT("OrderStatus"), ROW_NUMBER()OVER(ORDER BY COUNT("OrderStatus") DESC)
FROM "tblBulkSalesQuery"
WHERE "OrderStatus" = 'CMC'
GROUP BY "RepInitials"
LIMIT 1
OFFSET 1;