这是我的查询:
SELECT TOP 8 id, rssi1, date
FROM history
WHERE (siteName = 'CCL03412')
ORDER BY id DESC
这是结果:
如何使用 SQL 根据日期 (Column2) 反转此表?
这是我的查询:
SELECT TOP 8 id, rssi1, date
FROM history
WHERE (siteName = 'CCL03412')
ORDER BY id DESC
这是结果:
如何使用 SQL 根据日期 (Column2) 反转此表?
您可以使用第一个查询来获取匹配的 id,并将它们用作IN
子句的一部分:
SELECT id, rssi1, date
FROM history
WHERE id IN
(
SELECT TOP 8 id
FROM history
WHERE (siteName = 'CCL03412')
ORDER BY id DESC
)
ORDER BY date ASC
没有运行它,但我认为它应该很顺利
WITH cte AS
(
SELECT id, rssi1, date, RANK() OVER (ORDER BY ID DESC) AS Rank
FROM history
WHERE (siteName = 'CCL03412')
)
SELECT id, rssi1, date
FROM cte
WHERE Rank <= 8
ORDER BY Date DESC
试试下面的:
select * from (SELECT TOP 8 id, rssi1, date
FROM history
WHERE (siteName = 'CCL03412')
ORDER BY id DESC ) aa order by aa.date DESC
我没有运行它,但我认为它会起作用。执行并让我知道您是否遇到错误
select id, rssi1, date from (SELECT TOP 8 id, rssi1, date FROM history WHERE (siteName = 'CCL03412') ORDER BY id DESC) 按日期排序;