【发布时间】:2023-01-16 16:22:27
【问题描述】:
我在查询中发现了一个问题:
SELECT * FROM stocks_historic
WHERE ticker = x
ORDER BY YEAR ASC
LIMIT 10
当前表是这个:
stocks_historic (
historic_id SMALLSERIAL UNIQUE,
ticker VARCHAR(10) NOT NULL,
year VARCHAR(5) NOT NULL,
eps NUMERIC(6,2) NOT NULL,
operatingCashFlow NUMERIC(12,4) NOT NULL,
shares NUMERIC(12,4) NOT NULL,
cash NUMERIC(12,4) NOT NULL,
currentLiabilities NUMERIC(12,4) NOT NULL,
currentAssets NUMERIC(12,4) NOT NULL,
incomeBeforeTax NUMERIC(12,4) NOT NULL,
incomeTaxExpense NUMERIC(12,4) NOT NULL,
totalDebt NUMERIC(12,4) NOT NULL,
revenue NUMERIC(12,4) NOT NULL,
costOfGoodSold NUMERIC(12,4) NOT NULL,
operatingIncome NUMERIC(12,4) NOT NULL,
equity NUMERIC(12,4) NOT NULL,
capitalExpenditures NUMERIC(12,4) NOT NULL,
fcf NUMERIC(12,4) NOT NULL,
PRIMARY KEY (ticker, year)
);
预期结果:
如果从 2010 年到 2025 年那一年有 15 行具有相同的代码,我希望获得最近 10 年的 ASC 顺序:
historic_id: 1,
year: 2015,
historic_id: 2,
year: 2016,
historic_id: 3,
year: 2016,
...
结果是我得到了前 10 个数字,但因为我想要最高的 10 个,所以不起作用。
我知道我可以实现将 ASC 更改为 DESC,但这会让我以不希望的顺序返回几年。前端预计 ASC 订单数年,因此我必须更改所有内容以适应它。
有没有办法让那 10 行 ASC 订购?
谢谢,鲁本。
【问题讨论】:
-
请添加示例数据和预期结果
标签: sql database postgresql