假设我有下表:
Symbol| profit | person
AAPL +723 A
AAPL -100 B
BBY +123 A
NTAP +513 A
我想通过利润选择前n个和下n个符号(忽略它是人A还是B)。因此,AAPL应该是第一位的,并且应该显示623的利润。
...在sql server
中现在我正在做类似的事情:
SELECT TOP 15 sym profit FROM x
GROUP BY sym, profit
ORDER BY profit DESC
答案 0 :(得分:3)
这应该有效:
(
SELECT TOP 15
Symbol,
SUM(Profit)
FROM YourTable
GROUP BY Symbol
ORDER BY SUM(Profit)
) UNION (
SELECT TOP 15
Symbol,
SUM(Profit)
FROM YourTable
GROUP BY Symbol
ORDER BY SUM(Profit) DESC
)
答案 1 :(得分:1)