我试图在单个查询中查找列的Max,min,avg和last值。 平台:SQL Server 2012
样本表:
SN Month Acc Bal
------------------------
1 7 101 1,000/-
2 7 101 1,500/-
3 7 101 1,700/-
4 8 101 1,200/-
5 8 101 900/-
6 9 101 2,500/-
查询我写道:
select
[Month], [Acc],
min(Bal) as MinBal,
avg(Bal) as AvgBal,
max(Bal) as MaxBal
--, ??? for as LastBal
from
MyTable
Group By
[Month], [Acc]
使用Last_Value
查询将返回所有记录而不是聚合记录
select
[Month], [Acc],
min(Bal) as MinBal,
avg(Bal) as AvgBal,
max(Bal) as MaxBal,
LAST_VALUE(Bal) OVER (partition by [Acc] order by [Month]) as LastBal
from
MyTable
Group By
[Month], [Acc], Bal
同样包括last_value(bal)
正在按列表
列'Bal'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
答案 0 :(得分:4)
请尝试此解决方案 -
数据生成
CREATE TABLE Alls
(
SN INT
,[Month] INT
,Acc INT
,Bal INT
)
GO
INSERT INTO Alls VALUES
(1, 7, 101, 1000),
(2, 7, 101, 1500),
(3, 7, 101, 1700),
(4, 8, 101, 1200),
(5, 8, 101, 900),
(6, 9, 101, 2500)
GO
<强>解强>
SELECT sn,Acc,[Month] ,Bal
, MIN(Bal) OVER(PARTITION BY Acc,[Month]) MinBal
, AVG(Bal*1.) OVER(PARTITION BY Acc,[Month]) AvgBal
, MAX(Bal) OVER(PARTITION BY Acc,[Month]) MaxBal
, FIRST_VALUE(Bal) OVER(PARTITION BY Acc,[Month] ORDER BY SN DESC) lastVal
FROM Alls
ORDER By SN
<强>输出强>
sn Acc Month Bal MinBal AvgBal MaxBal lastVal
----------- ----------- ----------- ----------- ----------- ---------------- ----------- -----------
1 101 7 1000 1000 1400.000000 1700 1700
2 101 7 1500 1000 1400.000000 1700 1700
3 101 7 1700 1000 1400.000000 1700 1700
4 101 8 1200 900 1050.000000 1200 900
5 101 8 900 900 1050.000000 1200 900
6 101 9 2500 2500 2500.000000 2500 2500
(6 rows affected)
如果您只需要acc,month和其他汇总列,请使用以下 -
<强>解强>
SELECT Acc,[Month],MAX(MinBal)MinBal,MAX(AvgBal)AvgBal,MAX(MaxBal)MaxBal,MAX(lastVal)lastVal
FROM
(
SELECT sn,Acc,[Month] ,Bal
, MIN(Bal) OVER(PARTITION BY Acc,[Month]) MinBal
, AVG(Bal*1.) OVER(PARTITION BY Acc,[Month]) AvgBal
, MAX(Bal) OVER(PARTITION BY Acc,[Month]) MaxBal
, FIRST_VALUE(Bal) OVER(PARTITION BY Acc,[Month] ORDER BY SN DESC) lastVal
FROM Alls
)u GROUP BY Acc,[Month]
<强>输出强>
Acc Month MinBal AvgBal MaxBal lastVal
----------- ----------- ----------- ---------------- ----------- -----------
101 7 1000 1400.000000 1700 1700
101 8 900 1050.000000 1200 900
101 9 2500 2500.000000 2500 2500
(3 rows affected)
答案 1 :(得分:2)
select *
from
( SELECT sn, Acc, [Month], Bal
, MIN(Bal) OVER(PARTITION BY Acc, [Month]) MinBal
, AVG(Bal) OVER(PARTITION BY Acc, [Month]) AvgBal
, MAX(Bal) OVER(PARTITION BY Acc, [Month]) MaxBal
, row_number() OVER(PARTITION BY Acc, [Month] ORDER BY SN DESC) as rn
) tt
where rn = 1
ORDER By sn
答案 2 :(得分:0)
您可以达到以下目标:
select
tt.Month
, tt.Acc
, min(Bal) as MinBal
, avg(Bal) as AvgBal
, max(Bal) as MaxBal
, latest.balance
FROM #tbl1 as tt
JOIN (
SELECT
id
,month
,acc
,bal as balance
FROM #tbl1 AS t1
WHERE id = (SELECT MAX(id)
FROM #tbl1 AS t2
WHERE t1.month = t2.month
AND t1.acc = t2.acc
GROUP BY month, acc)
) as latest
on tt.month = latest.month
AND tt.acc = latest.acc
Group By tt.Month, tt.Acc, latest.balance
DROP TABLE #tbl1