[英]How to get count and sum of records in SQL Server 2014?
I have a table that has the following fields:我有一个包含以下字段的表:
The StatusID can either be 1, 2 or 11. StatusID 可以是 1、2 或 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.我想显示状态 1、2 和 11 中的 AccountNumbers 计数以及每个状态中所有账户的 CurrentBalance 总和,并且 LastTransaction 超过 6 个月。
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.您在 Group By 中有 AccountNumber。
--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如果您需要对 AccountNumber 进行计数,您现在应该在同一查询中使用 in count 和 group by
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.