在3天内输出超过一定金额的交易

时间:2017-06-13 13:35:11

标签: c# sql-server

在一个月内,按AccountNo分开,必须输出3天内超过10,000金额的所有交易。

这是一个示例表:

AccountNo-----Date------------Amount
1-------------5/1/17----------8000
1-------------5/3/17----------1000
1-------------5/4/17----------1000
1-------------5/6/17----------1000
2-------------5/7/17----------3000
2-------------5/10/17---------2000
2-------------5/13/17---------2000
2-------------5/13/17---------3000
3-------------5/14/17---------3000
3-------------5/15/17---------3000
3-------------5/16/17---------9000
4-------------5/17/17---------1000
5-------------5/18/17---------1000
5-------------5/19/17---------1000
5-------------5/20/17---------1000

结果必须是:

AccountNo-----Date------------Amount
1-------------5/1/17----------8000
1-------------5/3/17----------1000
1-------------5/4/17----------1000
2-------------5/7/17----------3000
2-------------5/10/17---------2000
2-------------5/13/17---------2000
2-------------5/13/17---------3000
3-------------5/14/17---------3000
3-------------5/15/17---------3000
3-------------5/16/17---------9000

有一个代码给我,但它还没有完全正常工作。

Select A.*
 From  YourTable A
 Cross Apply (
            Select RT1 = sum(case when [Date] <= B2.TstDate then [Amount] else 0 end)
                  ,RT2 = sum(case when [Date] >= B2.TstDate then [Amount] else 0 end)
         From  YourTable B1
         Cross Join (Select TstDate=A.[Date]) B2
         Where [Date] between DateAdd(DAY,-2,A.[Date]) and DateAdd(DAY,2,A.[Date])
           and Year([Date])=Year(TstDate)
           and Month([Date])=Month(TstDate)
     ) B
 Where RT1>=10000 or RT2>=10000

2 个答案:

答案 0 :(得分:2)

公用表表达式:TransInfo

  • TransInfo是一个包含帐户,年,月,日,金额的表

主要查询:

  • [Tmiddle]查询根据每个帐号每月每天的金额添加一个row_number
  • [Tmiddle]再次与TransInfo合并,将结果限制为一个月的前3个AS [Touter],其中前3名> = 10.000
  • 最外面的查询再次将结果与表格相结合,以再次获得完整的交易信息

简而言之:

  1. 按每个帐户每年每月最多3笔金额过滤数据
  2. 检查金额总和&gt; = 10.000
  3. 显示结果
  4. 查询:

    WITH [TransInfo] ([AccountNo], [Year], [Month], [Day], [Amount], [Rownumber])
    AS
    (
        SELECT [AccountNo]
              ,[Year]
              ,[Month]
              ,[Day]
              ,[Amount]
              ,ROW_NUMBER() OVER 
              (
                  PARTITION BY [AccountNo], 
                  [Year],
                  [Month] 
                  ORDER BY [Amount] DESC
              ) AS [Rownumber]
        FROM
        (
            SELECT [AccountNo]
                    ,DATEPART(YEAR, [Date]) AS [Year]
                    ,DATEPART(MONTH, [Date]) AS [Month]
                    ,DATEPART(DAY, [Date]) AS [Day]
                    ,SUM([Amount]) AS [Amount]
            FROM [Test].[dbo].[Data]
            GROUP BY [AccountNo], 
                        DATEPART(MONTH, [Date]), 
                        DATEPART(YEAR, [Date]),
                        DATEPART(DAY, [Date])
        ) AS [Tinner]
    )
    SELECT [Data].[AccountNo]
          ,[Data].[Date]
          ,[Data].[Amount]
    FROM [Test].[dbo].[Data]
    INNER JOIN
    (
        SELECT [TransInfo].[AccountNo]
              ,[TransInfo].[Year]
              ,[TransInfo].[Month]
              ,[TransInfo].[Day]
              ,[TransInfo].[Amount]
        FROM [TransInfo]
        INNER JOIN
        (
            SELECT [AccountNo]
                  ,[Year]
                  ,[Month]
            FROM [TransInfo] 
            WHERE [TransInfo].[Rownumber] <= 3
            GROUP BY [TransInfo].[AccountNo], 
                     [TransInfo].[Year],
                     [TransInfo].[Month]
            HAVING SUM ([TransInfo].[Amount]) >= 10000
        ) AS [Tmiddle]
        ON [Tmiddle].[AccountNo] = [TransInfo].[AccountNo]
        AND [Tmiddle].[Year] = [TransInfo].[Year]
        AND [Tmiddle].[Month] = [TransInfo].[Month]
        WHERE [TransInfo].[Rownumber] <= 3
    ) AS [Touter]
    ON [Data].[AccountNo] = [TOuter].[AccountNo]
    AND DATEPART(YEAR, [Data].[Date]) = [TOuter].[Year]
    AND DATEPART(MONTH, [Data].[Date]) = [TOuter].[Month]
    AND DATEPART(DAY, [Data].[Date]) = [TOuter].[Day]
    

    结果:左边是查询结果,右边是完整表

    Query result

答案 1 :(得分:0)

这是否按预期工作? 根据“3天内”的含义,您可能需要将2更改为3

SELECT DISTINCT C.*
FROM YourTable C
INNER JOIN (
SELECT a.AccountNo, a.Date
FROM YourTable a
INNER JOIN YourTable b
ON a.AccountNo = b.AccountNo
AND DATEADD(DAY, 2, a.Date) <= b.Date AND b.Date >= a.Date
GROUP BY a.AccountNo, a.Date 
HAVING SUM(b.Amount) > 10000
) d
ON C.AccountNo = d.AccountNo
AND DATEADD(DAY, 2, D.Date) <= C.Date AND C.Date >= D.Date