【问题标题】:Get last transactions from Transaction table by date按日期从交易表中获取最后一笔交易
【发布时间】: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


【解决方案1】:
with CTE1 as
(
select accountID, Date, sum(Amount) as Amount
from Transactions
where Date between '2018-06-14' and '2018-06-16' -- Apply date restriction here
group by accountID, Date
)
, CTE2 as
(
select accountID, Amount, Date,
       row_number() over (partition by accountID order by date desc) as rn
from Transactions
)
select a1.accountID, a1.Amount, a1.Date, a2.Date, a2.Amount
from CTE2 a1
left join CTE2 a2
on a1.accountID = a2.accountID
and a2.rn = a1.rn+1

这将在一行中通过 accountID 为您提供每天的交易和前一天的交易。从这里您可以比较值。

【讨论】:

  • 我不需要每天的交易,我只需要最近两天的交易。
  • @ВасяПупкин 所以应用 where 子句...我们是来指导的,而不是为你做所有事情
【解决方案2】:

您想按日期分组并对金额求和

select Date,sum(Amount) from Transactions /*where contitions*/ group by Date

【讨论】:

    【解决方案3】:

    你可以使用它。希望对你有用。

    SELECT 
    *
    FROM Transactions tb
    INNER JOIN 
    (
      SELECT MAX([Date]) AS [Date] FROM Transactions
    
      UNION ALL 
    
      SELECT MAX([Date]) AS [Date] FROM Transactions WHERE [Date] < (SELECT MAX([Date]) AS [Date] FROM Transactions)
    
    ) tb1 ON tb1.[Date] = tb.[Date]
    

    【讨论】:

      【解决方案4】:

      这会检查当天的 sum amount 和前一天的 sum amount(以确认它大于 10%),然后执行 top 2 以仅提取最后两天...

      WITH CTE AS(
          select
              Date,
              sum(Amount) as SumAmount,
              rownum = ROW_NUMBER() OVER(ORDER BY Date)
          from Transactions
          group by Date
      )
      select top 2 CTE.date, CTE.SumAmount, CTE.rownum, CASE WHEN prev.sumamount > CTE.sumamount * 0.10 THEN 1 else 0 END isgreaterthan10per
      from CTE
      LEFT JOIN CTE prev ON prev.rownum = CTE.rownum - 1
      order by CTE.date desc
      

      【讨论】:

        【解决方案5】:

        您可以查看以下查询以获取最后两个日期和这两个日期的金额总和。

        select distinct accountid,subaccountid,user,trandate,sum(amount) over(partition by date) 从交易 where date>=(select max(date) from transactions where date

        【讨论】:

        • 欢迎来到 Stack Overflow!感谢您提供此代码 sn-p,它可能会提供一些即时帮助。一个正确的解释would greatly improve 其教育价值通过展示为什么这是一个很好的解决问题的方法,并将使它对未来有类似但不相同的问题的读者更有用。请edit您的答案添加解释,并说明适用的限制和假设。
        猜你喜欢
        • 2017-04-24
        • 2020-07-10
        • 2021-10-25
        • 2013-06-12
        • 1970-01-01
        • 2022-01-01
        • 2013-08-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多