1 year ago

#374075

test-img

Sam

SQL statement for grouping financial transactions by a dollar range

I am using Report Builder to generate a report of companies (agencies) that shows a payment range and then a count of transactions within each range. Ideally, it will be grouped by Agency and will list each range and the number of transactions per agency. I have some other filters to add later, but I am primarily looking for suggestions on the transaction range grouping.

The result may looking something like this:

I have tried using case expressions like below, but am struggling to use them when trying to determine the number of transactions for each bucket. It keeps returning the same value for each bucket.

SELECT t.PaymentVendor,
COUNT(case when t.TransactionAmount * -1 >= 1 and t.TransactionAmount       * -1 <= 50 then 1 else 0 end) AS [1-50],
COUNT(case when t.TransactionAmount * -1 >= 51 and          t.TransactionAmount * -1     <= 100 then 1 else 0 end) AS [51-100],
COUNT(case when                         t.TransactionAmount * -1 >= 101 and        t.TransactionAmount * -1 <= 200 then 1 else 0 end) AS        [101-200],
COUNT(case when t.TransactionAmount * -1 >= 201 and t.TransactionAmount * -1 <= 300     then 1 else 0 end) AS [201-300],
COUNT(case when t.TransactionAmount * -1 >= 301 and                       t.TransactionAmount * -1 <= 500 then 1 else 0 end) AS [301-500],
COUNT(case when                               t.TransactionAmount * -1 >= 501 and t.TransactionAmount * -1 <= 700 then 1 else 0 end) AS [501-        700],
COUNT(case when t.TransactionAmount * -1 >= 701 and t.TransactionAmount * -1 <= 900 then     1 else 0 end) AS [701-900],
COUNT(case when t.TransactionAmount * -1 >= 901 and                                 t.TransactionAmount * -1 <= 1000 then 1 else 0 end) AS [901-1000],
COUNT(case when                           t.TransactionAmount * -1 >= 1001 and t.TransactionAmount * -1 <= 3000 then 1 else 0 end) AS             [1001-3000],
COUNT(case when t.TransactionAmount * -1 >= 3001 and t.TransactionAmount * -1 <=       5000 then 1 else 0 end) AS [3001-5000],
COUNT(case when t.TransactionAmount * -1 >= 5001 and         t.TransactionAmount * -1 <= 10000 then 1 else 0 end) AS [5001-10000],
COUNT(case when                      t.TransactionAmount * -1 >= 10001 and t.TransactionAmount * -1 <= 50000 then 1 else 0 end) AS           [10001-50000]
FROM SecurityCredit.dbo.RawTransactionData t
WHERE t.TransactionType IN ('BT',     'FOR')
AND t.TransactionStatus = 'U' AND t.TransactionDate BETWEEN @StartDate AND @EndDate
GROUP BY t.PaymentVendor
ORDER BY t.PaymentVendor

Does anyone have a simple SQL statement example for matching a record to each bucket or for displaying the results using a Report Builder dataset?

sql

reportbuilder

0 Answers

Your Answer

Accepted video resources