【发布时间】:2017-11-22 17:04:08
【问题描述】:
我需要从交易完成的 2 个 lats 日期的交易表中获取交易。并检查,如果最后一天的交易量比前一天的交易量超过 10%。 我的表有 AccountId、SubAccountId、Amount、Date 和 UserId 列。 例如:
CREATE TABLE Transactions
(`id` int, `AccountId` int, `SubAccountId` int, `Amount` decimal
,`Date` datetime, `User` int);
INSERT INTO Transactions
(`id`, `AccountId`, `SubAccountId`, `Amount`, `Date`, `User`)
VALUES
(1, 1, 2, 100, '06/15/2018', 1),
(2, 1, 2, 40, '06/15/2018', 1),
(3, 1, 2, 20, '06/14/2018', 1),
(4, 1, 2, 0, '06/10/2018', 1),
;
在此示例中,我只需要选择日期 06/15/2018 和 06/14/2018 的交易,并显示这几天的交易总和。 到目前为止,我可以选择最后的交易,如下所示:
select distinct AccountId,
SubAccountId,
UserId,
Amount,
Date AS lastDate,
min(Date)
over (partition by PayerAccount order by Date
rows between 1 preceding and 1 preceding) as PrevDate
from Transactions
order by UserId
【问题讨论】:
-
你能分享所需输出的样本吗?
-
您的表定义和查询不匹配。请更正并删除列中的`
-
尝试
dense_rank() over(..) rnk,然后尝试rnk<=2
标签: sql sql-server