嗨,鉴于我有一个有2列的表。
餐桌预订 列数量 - TransactionDate
获取最后2个transactionDate之间的总金额。
你是怎么做到的?你怎么得到最后一笔交易但01 有什么建议吗?
答案 0 :(得分:1)
您可以使用公用表表达式(CTE)根据事务日期的降序为每行分配序列号。然后选择带过滤器的行以获取最后两行。
此查询显示表中的最后两个事务
WITH BookingCTE AS (
SELECT ROW_NUMBER() OVER (ORDER BY TransactionDate DESC) as Sequence,
Amount, TransactionDate
FROM Booking
)
SELECT Sequence, Amount, TransactionDate
FROM BookingCTE
WHERE Sequence <= 2
;
此查询为您提供最近两笔交易的总金额。
WITH BookingCTE AS (
SELECT ROW_NUMBER() OVER (ORDER BY TransactionDate DESC) as Sequence, Amount, TransactionDate
FROM Booking
)
SELECT SUM(Amount) AS TotalAmount
FROM BookingCTE
WHERE Sequence <= 2
;