简体   繁体   中英

How to get count and sum of records in SQL Server 2014?

I have a table that has the following fields:

  1. AccountNumber
  2. CurrentBalance
  3. StatusID
  4. LastTransaction

The StatusID can either be 1, 2 or 11.

I would like to show a count of the AccountNumbers in Statuses 1, 2 and 11 and Sum of CurrentBalance of all Accounts in each Status AND where the LastTransaction is older than 6 months.

My code so far:

SELECT Count(AccountNumber) NoOfAccts
      ,SUM(CurrentBalance) TotalBalance          
      ,StatusId

FROM Accounts
WHERE DATEDIFF (DAY, LastTransaction, GETDATE()) > 180
GROUP BY AccountNumber, StatusId

This code gives me individual rows for each account

|NoOfAccounts | TotalBalance | StatusId|
-----------------------------------------
|           1 |       364.24 |        1|
-----------------------------------------
|           1 |       856.25 |        2|
-----------------------------------------
|           1 |       189.24 |        1|
-----------------------------------------

and so on...

My sample Data is as follows:

|AccountsNumbr|CurrentBalance| StatusId| LastTransaction|
---------------------------------------------------------
|      215481 |       364.24 |        1| 2018=01-05     |
---------------------------------------------------------
|      215587 |       856.25 |        2| 2017=05-02     |
---------------------------------------------------------
|      216744 |       189.24 |        1| 2017=03-21     |
---------------------------------------------------------
|      548756 |       693.15 |        1| 2017=08-20     |
---------------------------------------------------------
|      235687 |       547.69 |        1| 2018=03-21     |
---------------------------------------------------------
|      895647 |       786.65 |       11| 2017=02-28     |
---------------------------------------------------------

how about this below. Note the grouping. You had AccountNumber in the Group By.

--sample data
IF object_id('tempdb..#Accounts') is not null drop table #Accounts
CREATE TABLE #Accounts (AccountNumber INT, CurrentBalance MONEY, StatusID INT, LastTransaction DATE)
INSERT INTO #Accounts (AccountNumber,CurrentBalance,StatusID,LastTransaction) VALUES
(1, 100, 1, DATEADD(MONTH,-3, GETDATE())),
(2, 200, 2, DATEADD(MONTH,-4, GETDATE())),
(3, 50, 11, DATEADD(MONTH,-7, GETDATE())), -- older than 6 months
(4, 300, 1, DATEADD(MONTH,-8, GETDATE())), -- older than 6 months
(5, 10, 2, DATEADD(MONTH,-10, GETDATE())), --older than 6 months 
(6, 20, 11, DATEADD(MONTH,-12, GETDATE())), --older than 6 months 
(7, 5, 1, DATEADD(MONTH,-1, GETDATE())),
(8, 50, 2, DATEADD(MONTH,-2, GETDATE())),
(9, 100, 11, DATEADD(MONTH,-4, GETDATE())),
(10, 400, 1, DATEADD(MONTH,-11, GETDATE())) --older than 6 months

--query
SELECT  StatusID,
        COUNT(AccountNumber) AS 'Accounts',
        SUM(CurrentBalance) AS 'CurrentBalance'
FROM #Accounts
WHERE LastTransaction < DATEADD(Month,-6, GETDATE())
GROUP BY StatusID

output - according to the temp table data, this is correct

StatusID    Accounts    CurrentBalance
1              2           700.00
2              1           10.00
11             2           70.00

If you need count for AccountNumber you should now use in count and in group by in the same query

so you should use

SELECT Count(AccountNumber) NoOfAccts
  ,SUM(CurrentBalance) TotalBalance          
  ,StatusId

FROM Accounts
WHERE DATEDIFF (DAY, LastTransaction, GETDATE()) > 180
GROUP BY StatusId

or

SELECT Count(*) NoOfAccts
  ,SUM(CurrentBalance) TotalBalance          
  ,StatusId

FROM Accounts
WHERE DATEDIFF (DAY, LastTransaction, GETDATE()) > 180
GROUP BY StatusId,  AccountNumber

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM