所以我需要编写一个存储过程来生成报告。在本报告中,我需要显示支票金额和支票计数的平均每日计数。这就是我现在所拥有的:
SELECT SendingBank,
CheckCount As TotalCount,
CheckAmt As TotalAmt,
AVG(CheckCount/30) AS AvgDailyCount,
AVG(CheckAmt/30) AS AvgDailyAmt
FROM tblRptT001
WHERE InwardClearingDate = @asInwardClearingDt
GROUP BY InwardClearingDate, SendingBank, CheckCount, CheckAmt
ORDER BY InwardClearingDate
我知道我为平均数做了什么是完全错误的,我坚持认为,因为不是每个月都有相同的天数。我测试了这个sp并且它工作,除了我想要的平均场的方式。
关于如何处理这个问题的任何想法?非常感谢任何形式的帮助。谢谢!
修改:这是示例数据
InwardClearingDate SendingBank CheckCount CheckAmt
2014-03-03 ABC Bank 1800 70000.00
2014-03-21 BBC Bank 526 456090.00
和我想要的结果
Sending Bank|Total Count| Total Amt| Daily Avg Count| Daily Avg Amt
ABC Bank | 1800 | 70000.00 | 60 |2333.00
参数是内向结算日期,例如2014-03-03
。
答案 0 :(得分:1)
可能是这样的
SELECT S.SendingBank,
S.TotalCount,
S.TotalAmt,
AVG(T.CheckCount) AS AvgDailyCount,
AVG(T.CheckAmt) AS AvgDailyAmt
FROM
(
SELECT SendingBank, CheckCount As TotalCount, CheckAmt As TotalAmt
FROM tblRptT001
WHERE InwardClearingDate = @asInwardClearingDt
GROUP BY InwardClearingDate, SendingBank, CheckCount, CheckAmt
) S JOIN tblRptT001 T ON S.SendingBank = T.SendingBank
GROUP BY S.SendingBank,S.TotalCount,S.TotalAmt,T.InwardClearingDate
<强> FIDDLE DEMO 强>
答案 1 :(得分:1)
我知道我为平均数做了什么是完全错误的,我坚持认为,因为不是每个月都有相同的天数。
要解决这个问题,请考虑在InwardClearingDate上应用DAY和EOMONTH()函数
SELECT SendingBank, CheckCount As TotalCount, CheckAmt As TotalAmt,
AVG(CheckCount/DAY(EOMONTH(InwardClearingDate)) AS DaysInMonth) AS AvgDailyCount,
AVG(CheckAmt/DAY(EOMONTH(InwardClearingDate)) AS DaysInMonth) AS AvgDailyAmt
FROM tblRptT001
WHERE InwardClearingDate = @asInwardClearingDt
GROUP BY InwardClearingDate, SendingBank, ChequeCount, ChequeAmt
ORDER BY InwardClearingDate
答案 2 :(得分:1)
请查看这是否是您要找的,
Declare @asInwardClearingDt Date
Declare @month int = (Datediff(Day,@asInwardClearingDt,Dateadd(Month,1,@asInwardClearingDt)))
Select SendingBank,
Sum(CheckCount) As TotalCount,
Sum(CheckAmt) As TotalAmt,
Sum(CheckCount)/@month As AvgDailyCount,
Sum(CheckAmt)/@month As AvgDailyAmt
From tblRptT001
Where Datepart(MONTH,InwardClearingDate) = Datepart(MONTH,@asInwardClearingDt)
And Datepart(MONTH,InwardClearingDate) = Datepart(MONTH,@asInwardClearingDt)
Group By SendingBank
nf91,请发表评论