如何在查询结果中加入一行平均值?
对SQL算是个打酱油的,向各位高手求助。
数据表结构如下
单位月份数据1数据2数据3数据4
公司113.30 9.00 2.00 2.00
公司214.20 9.60 2.10 2.00
公司311.00 9.00 6.50 2.00
公司123.00 2.00 2.00 7.80
公司234.20 9.60 2.10 2.00 
我想查询某个月每个单位的数据情况,在查询结果的最后加上一行全部单位数据的平均值。
查某一个月的语句如下
select 单位, 数据1, 数据2, 数据3, 数据4 from DataTable where 月份 = Month
请问怎么在这个查询结果的最后加上一行平均值呢?
最后实现比如查询1月份的数据,能够得到下图中的结果
[解决办法]
select 单位,数据1,数据2,数据3,数据4 from DataTable where 月份=1
union all
select isnull('平均','')单位,avg(数据1)数据1,avg(数据2)数据2,avg(数据3)数据3,avg(数据4)数据4 from DataTable where 月份=1
/*单位 数据1 数据2 数据3 数据4
---------- ---------------------- ---------------------- ---------------------- ----------------------
公司1 3.3 9 2 2
公司2 4.2 9.6 2.1 2
公司3 1 9 6.5 2
平均 2.83333333333333 9.2 3.53333333333333 2
(4 行受影响)
[解决办法]
select 位,1,2,3,4
from
(
select 0 排序,位,1,2,3,4 from 表 where 月份=1
union all
select 1 排序,'平均',cast(1/sl decimal(12,2)),cast(2/sl decimal(12,2)),cast(3/sl decimal(12,2)),cast(4/sl decimal(12,2))
from
(
select count(*) sl,sum(1) 1,sum(2) 2,sum(3) 3,sum(4) 4 from 表 where 月份=1
) a
) b
order by 排序,位
[解决办法]
IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE name = 'test1')
BEGIN
DROP TABLE test1
END
GO
CREATE TABLE test1
(
name varchar(100),
imonth int,
value1 DECIMAL(10,2),
value2 DECIMAL(10,2),
value3 DECIMAL(10,2),
value4 DECIMAL(10,2)
)
GO
INSERT INTO test1
SELECT '公司1',1,3.30,9.00,2.00,2.00 UNION ALL
SELECT '公司2',1,4.20, 9.60, 2.10, 2.00 UNION ALL
SELECT '公司3',1,1.00, 9.00, 6.50, 2.00 UNION ALL
SELECT '公司1',2,3.00, 2.00, 2.00, 7.80 UNION ALL
SELECT '公司2',3,4.20, 9.60, 2.10, 2.00
结果
namevalue1value2value3value4
公司13.3000009.0000002.0000002.000000
公司24.2000009.6000002.1000002.000000
公司31.0000009.0000006.5000002.000000
平均2.8333339.2000003.5333332.000000
[解决办法]
--> 测试数据:@T
declare @T table([单位] varchar(5),[月份] VARCHAR(20),[数据1] numeric(3,2),[数据2] numeric(3,2),[数据3] numeric(3,2),[数据4] numeric(3,2))
insert @T
select '公司1',1,3.30,9.00,2.00,2.00 union all
select '公司2',1,4.20,9.60,2.10,2.00 union all
select '公司3',1,1.00,9.00,6.50,2.00 union all
select '公司1',2,3.00,2.00,2.00,7.80 union all
select '公司2',3,4.20,9.60,2.10,2.00
;WITH maco AS
(
select * from @T WHERE [月份]=1
)
SELECT * FROM maco
UNION ALL
SELECT '平均','',
CAST(AVG([数据1]) AS DECIMAL(18,2)),
CAST(AVG([数据2]) AS DECIMAL(18,2)),
CAST(AVG([数据3]) AS DECIMAL(18,2)),
CAST(AVG([数据4]) AS DECIMAL(18,2))
FROM maco
/*
单位 月份 数据1 数据2 数据3 数据4
----- -------------------- --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
公司1 1 3.30 9.00 2.00 2.00
公司2 1 4.20 9.60 2.10 2.00
公司3 1 1.00 9.00 6.50 2.00
平均 2.83 9.20 3.53 2.00
*/
[解决办法]
楼主上面的答案全是错的!
[解决办法]
学习 学习 groupby
[解决办法]
if object_id('test') is not null
drop table test
go
create table test(单位 varchar(10),月份 int, 数据1 decimal(8,2),
数据2 decimal(8,2), 数据3 decimal(8,2), 数据4 decimal(8,2))
insert into test
select '公司1' ,1, 3.30 , 9.00 , 2.00 , 2.00 union
select '公司2', 1, 4.20, 9.60 , 2.10, 2.00 union
select '公司3', 1 ,1.00 , 9.00, 6.50 , 2.00 union
select '公司1', 2, 3.00 , 2.00 , 2.00 , 7.80 union
select '公司2', 3 ,4.20, 9.60 , 2.10 , 2.00
select isnull(单位,'平均值') as 单位,avg(数据1),avg(数据2),avg(数据3),avg(数据4) from test where 月份=1
group by 单位 with rollup
/*
单位
---------- --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
公司1 3.300000 9.000000 2.000000 2.000000
公司2 4.200000 9.600000 2.100000 2.000000
公司3 1.000000 9.000000 6.500000 2.000000
平均值 2.833333 9.200000 3.533333 2.000000
(4 row(s) affected)
*/
[解决办法]
不是楼主想要的结果