寻找高价值的交易

时间:2017-11-15 06:47:07

标签: sql sql-server

我有一个表结构如下 -

Account Number | Transaction Type | Amount | Date

交易类型可以是信用卡或借记卡。我需要检查信用卡或借记卡交易金额是否大于某个阈值。如果是,请检查金额是否大于收入* 10(存储在其他表中)。

可能有多种情况(考虑信用额度> = 10000,借方阈值> = 20000,收入= 1000) -

1. Account Number | Transaction Type | Amount | Date
   A1             | Credit           | 10000   | 11-11-2017

此示例中不存在借记行,但A1满足所有条件

2. Account Number | Transaction Type | Amount | Date
   A2             | Debit            | 20000   | 12-11-2017

此示例中不存在Credit行,但A2满足所有条件

3. Account Number | Transaction Type | Amount | Date
   A3             | Credit           | 10000   | 13-11-2017
   A3             | Debit            | 5000    | 13-11-2017

此示例中存在Credit和Debit行,但A3满足Credit条件

4. Account Number | Transaction Type | Amount | Date
   A4             | Credit           | 5000   | 14-11-2017
   A4             | Debit            | 20000  | 14-11-2017

此示例中存在Credit和Debit行,但A4满足借方条件

5. Account Number | Transaction Type | Amount | Date
   A5             | Credit           | 10000   | 15-11-2017
   A5             | Debit            | 20000   | 15-11-2017

此示例中存在Credit和Debit行,但A5应满足Debit条件,因为它具有更大的值(在这种情况下需要最大值)

6. Account Number | Transaction Type | Amount | Date
   A6             | Credit           | 10000   | 16-11-2017
   A6             | Debit            | 20000   | 16-11-2017

在此示例中考虑Income = 3000,此示例中存在Credit和Debit行,但A6不应满足任何条件,因为Income * 10的值大于Credit / Debit交易值。

我已经使用UNION和LEFT& RIGHT加入以查找Credit / Debit值,但查找是否有更好的方法来编写此脚本。如果任何信用/借记值不存在,请将其视为零。我正在使用SQL Server 2012。

到目前为止开发的脚本(高级别):

SELECT
temp.Number as Number,
temp.Amount1 as CrAmt,
temp.Amount2 as DrAmt
FROM
(
SELECT
    t1.Account_Number AS Number,
    t1.Amount AS Amount1,
    isnull(t2.Amount, 0) as Amount2
FROM
    TableName AS t1 WITH (NOLOCK) LEFT JOIN
    TableName AS t2 WITH (NOLOCK) on isnull(t2.Transaction_Type, 'Debit') = 'Debit' and isnull(t2.Account_Number,t1.Account_Number) = t1.Account_Number
WHERE
    t1.Transaction_Type = 'Credit'

UNION

SELECT
    t1.Account_Number AS Number,
    t1.Amount AS Amount1,
    isnull(t2.Amount, 0) as Amount2
FROM
    TableName AS t1 WITH (NOLOCK) RIGHT JOIN
    TableName AS t2 WITH (NOLOCK) on isnull(t2.Transaction_Type, 'Debit') = 'Debit' and isnull(t2.Account_Number,t1.Account_Number) = t1.Account_Number
WHERE
    t1.Transaction_Type = 'Credit'
) temp
where
(temp.Amount1 >= 10000 and temp.Amount1>= Income * 10) OR (temp.Amount2 >= 20000 and temp.Amount2>= Income * 10)

1 个答案:

答案 0 :(得分:0)

这是一种更清洁的方法

DECLARE @income       decimal(18, 2) = 1000*10,
        @CrThreshold  decimal(18, 2) = 10000,
        @DrThreshold  decimal(18, 2) = 20000

SELECT AccountNumber, CrAmt, DrAmt
FROM (
  SELECT AccountNumber, 
         CrAmt = SUM(CASE WHEN TransactionType = 'Credit' THEN Amount ELSE 0 END), 
         DrAmt = SUM(CASE WHEN TransactionType = 'Debit' THEN Amount ELSE 0 END)
  FROM TableName
  GROUP BY AccountNumber, [Date]) T
WHERE (CrAmt >= @CrThreshold AND CrAmt >= @income)
OR (DrAmt >= @DrThreshold AND DrAmt >= @income)