使用 Group by 的多个聚合计算
Multiple aggregate calculations using Group by
我有一个数据集 df1,我想在其中:
- 根据 TotalB 列的分组取 TotalB 列的平均值。
- 然后我想取这个新列并减去自由值以获得已用值
df1
date name rack TotalB freeB
11/20/2020 a yes 11 5
11/20/2020 a yes 10 5
11/20/2020 a yes 12 5
11/20/2020 a yes 10 5
11/20/2020 b no 5 2
11/20/2020 b no 5 2
11/20/2020 b no 6 2
期望的结果
date name rack TotalB freeB UsedB
11/20/2020 a yes 10.75 5 5.75
11/20/2020 b no 5.33 2 3.33
我在做什么:
SELECT AVG(TotalB) AS AVG_TotalB, date, name, rack, TotalB, freeB, (AVG_TotalB - freeB) AS UsedB
FROM df1
GROUP BY rack
任何建议都会有所帮助。
您的查询似乎很接近。大多数情况下,您需要修复 GROUP BY
:
SELECT date, name, rack,
AVG(TotalB) AS AVG_TotalB, freeB,
(AVG(TotalB) - freeB) AS UsedB
FROM df1
GROUP BY date, name, rack, freeB;
请注意,您不能在同一个 SELECT
中重复使用列别名。您需要重复表达。
您需要将它们转换为 DECIMAL 以避免丢失小数部分。
DECLARE @test TABLE (dateval date, name varchar(50), rack varchar(10), TotalB int, freeB int)
INSERt INTO @test
values
('11/20/2020','a','yes', 11 ,5),
('11/20/2020','a','yes', 10 ,5),
('11/20/2020','a','yes', 12 ,5),
('11/20/2020','a','yes', 10 ,5),
('11/20/2020','b','no', 5 ,2),
('11/20/2020','b','no', 5 ,2),
('11/20/2020','b','no', 6 ,2);
SELECT dateval,name, rack, freeB, cast(AVG(cast(totalB as decimal(5,2))) as decimal(5,2)) as TotalB,
cast(AVG(cast(totalB as decimal(5,2))) as decimal(5,2)) - freeB as UsedB FROM @test
GROUP BY dateval,name, rack, freeB
+------------+------+------+-------+--------+-------+
| dateval | name | rack | freeB | TotalB | UsedB |
+------------+------+------+-------+--------+-------+
| 2020-11-20 | a | yes | 5 | 10.75 | 5.75 |
| 2020-11-20 | b | no | 2 | 5.33 | 3.33 |
+------------+------+------+-------+--------+-------+
我有一个数据集 df1,我想在其中:
- 根据 TotalB 列的分组取 TotalB 列的平均值。
- 然后我想取这个新列并减去自由值以获得已用值
df1
date name rack TotalB freeB
11/20/2020 a yes 11 5
11/20/2020 a yes 10 5
11/20/2020 a yes 12 5
11/20/2020 a yes 10 5
11/20/2020 b no 5 2
11/20/2020 b no 5 2
11/20/2020 b no 6 2
期望的结果
date name rack TotalB freeB UsedB
11/20/2020 a yes 10.75 5 5.75
11/20/2020 b no 5.33 2 3.33
我在做什么:
SELECT AVG(TotalB) AS AVG_TotalB, date, name, rack, TotalB, freeB, (AVG_TotalB - freeB) AS UsedB
FROM df1
GROUP BY rack
任何建议都会有所帮助。
您的查询似乎很接近。大多数情况下,您需要修复 GROUP BY
:
SELECT date, name, rack,
AVG(TotalB) AS AVG_TotalB, freeB,
(AVG(TotalB) - freeB) AS UsedB
FROM df1
GROUP BY date, name, rack, freeB;
请注意,您不能在同一个 SELECT
中重复使用列别名。您需要重复表达。
您需要将它们转换为 DECIMAL 以避免丢失小数部分。
DECLARE @test TABLE (dateval date, name varchar(50), rack varchar(10), TotalB int, freeB int)
INSERt INTO @test
values
('11/20/2020','a','yes', 11 ,5),
('11/20/2020','a','yes', 10 ,5),
('11/20/2020','a','yes', 12 ,5),
('11/20/2020','a','yes', 10 ,5),
('11/20/2020','b','no', 5 ,2),
('11/20/2020','b','no', 5 ,2),
('11/20/2020','b','no', 6 ,2);
SELECT dateval,name, rack, freeB, cast(AVG(cast(totalB as decimal(5,2))) as decimal(5,2)) as TotalB,
cast(AVG(cast(totalB as decimal(5,2))) as decimal(5,2)) - freeB as UsedB FROM @test
GROUP BY dateval,name, rack, freeB
+------------+------+------+-------+--------+-------+
| dateval | name | rack | freeB | TotalB | UsedB |
+------------+------+------+-------+--------+-------+
| 2020-11-20 | a | yes | 5 | 10.75 | 5.75 |
| 2020-11-20 | b | no | 2 | 5.33 | 3.33 |
+------------+------+------+-------+--------+-------+