我需要通过使用group by子句从表中进行选择,然后通过order by子句
select id,EXID,Rate,Date,Currency from tb_exchange where Boolean='True' group by id,EXID,Rate,Date,Currency ORDER BY id DESC
但它通常会返回
select * from tb_exchange where Boolean='True' ORDER BY id DESC
我需要首先返回最新的商品,并按货币名称分组。我的货币名称是(THB和USD)
请帮助我
提前谢谢。
答案 0 :(得分:0)
您可以在此处尝试使用ROW_NUMBER
:
SELECT id, EXID, Rate, Date, Currency
FROM
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY Currency ORDER BY Date DESC) rn
FROM tb_exchange
WHERE Boolean = 'True'
) t
WHERE rn = 1
ORDER BY id DESC;
此答案假设您想要每个Currency
组中的最新记录,并且Date
列中记录了给定记录的最新记录或旧记录。