select sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000) as 三层面积,
t0.classid as 班别,
t0.inputdate as 日期,
(sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000)*0.0274279*0.29586/5)as A班主机手
from t_zbinput_d t1,order_ba1 t2,t_zbinput_M t0
where t1.orderid=t2.orderid and t0.sid=t1.mastersid and t2.pfloor in(2,3) and t0.inputdate>='2015-06-01 00:00:00.000' and t0.inputdate<='2015-06-30 00:00:00.000'
and t0.classid='A班'
group by t0.classid, t0.inputdate
/*
三层面积 班别 日期 A班主机手
10930.723500 A班 2015-06-01 22:11:19.203 17.740167
1636.878961 A班 2015-06-02 03:30:36.670 2.656595
4344.250800 A班 2015-06-02 06:34:24.140 7.050561
2395.518390 A班 2015-06-02 07:01:25.547 3.887839
2419.762900 A班 2015-06-02 07:06:41.313 3.927187
730.198875 A班 2015-06-02 07:11:25.217 1.185086
2574.210600 A班 2015-06-02 07:16:33.877 4.177850
6451.640450 A班 2015-06-02 07:26:10.467 10.470778
3087.875700 A班 2015-06-02 07:52:52.703 5.011510
5600.476600 A班 2015-06-03 06:26:48.107 9.089370
844.440000 A班 2015-06-03 07:02:02.560 1.370495
37520.916646 A班 2015-06-04 08:56:09.437 60.895085
6129.695240 A班 2015-06-04 19:46:30.250 9.948272
13061.659975 A班 2015-06-04 21:00:49.890 21.198600
3946.726562 A班 2015-06-04 21:57:40.360 6.405394
1507.478400 A班 2015-06-04 23:07:20.030 2.446582
3048.430275 A班 2015-06-04 23:41:57.420 4.947491
要求:面积求和,班别分类别小计,主机手求和。谢谢
10 个解决方案
#1
程序部分处理一下咯咯咯
#2
WITH a(三层面积,班别,日期,主机手) AS (
/* 把你原先的 SQL 放在这里,下面的测试数据不要 */
SELECT 10930.723500,'A班','2015-06-01 22:11:19.203',17.740167 UNION ALL
SELECT 1636.878961,'A班','2015-06-02 03:30:36.670',2.656595 UNION ALL
SELECT 4344.250800,'A班','2015-06-02 06:34:24.140',7.050561 UNION ALL
SELECT 2395.518390,'B班','2015-06-02 07:01:25.547',3.887839 UNION ALL
SELECT 2419.762900,'B班','2015-06-02 07:06:41.313',3.927187
)
,b AS (
SELECT 1 flag,
SUM(三层面积) 三层面积,
班别,
'小计' 日期,
SUM(主机手) 主机手
FROM a
GROUP BY 班别
UNION ALL
SELECT 0 flag, *
FROM a
)
SELECT 三层面积,班别,日期,主机手
FROM b
ORDER BY 班别,flag
三层面积 班别 日期 主机手
-------------- ---- ----------------------- --------------
10930.723500 A班 2015-06-01 22:11:19.203 17.740167
1636.878961 A班 2015-06-02 03:30:36.670 2.656595
4344.250800 A班 2015-06-02 06:34:24.140 7.050561
16911.853261 A班 小计 27.447323
2395.518390 B班 2015-06-02 07:01:25.547 3.887839
2419.762900 B班 2015-06-02 07:06:41.313 3.927187
4815.281290 B班 小计 7.815026
#3
-- 参考一下这个语法
select o.id ,o.name , c.xtype, COUNT(*)
from sysobjects o , syscolumns c
where o.id = c.id and o.xtype = 'U'
group by rollup(o.id,o.name,c.xtype)
#4
非常感谢。不过,我现在将代码改成了如下:
WITH a(日期,三层面积,班别,主机手) AS (
select CONVERT(varchar(10),t0.inputdate,23) as 日期,
sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000) as 三层面积,
t0.classid as 班别,
(sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000)*0.0274279*0.29586/5)as 主机手
from t_zbinput_d t1,order_ba1 t2,t_zbinput_M t0
where t1.orderid=t2.orderid and t0.sid=t1.mastersid and t2.pfloor in(2,3) and t0.inputdate>='2015-06-01 00:00:00.000' and t0.inputdate<='2015-06-30 00:00:00.000'
group by t0.classid, t0.inputdate,t1.orderid,t0.inputdate),
b AS (
SELECT 1 flag,
'小计' 日期,
SUM(三层面积) 三层面积,
班别,
SUM(主机手) 主机手
FROM a
GROUP BY 班别
UNION ALL
SELECT 0 flag, *
FROM a
)
SELECT 日期,三层面积,班别,主机手
FROM b
ORDER BY 班别,flag
/*
日期 三层面积 班别 主机手
2015-06-01 4337.280000 A班 7.039248
2015-06-01 1566.734400 A班 2.542753
2015-06-27 186.020000 A班 0.301903
2015-06-27 183.190400 A班 0.297311
2015-06-27 963.168000 A班 1.563186
2015-06-27 496.006000 A班 0.804999
2015-06-27 7142.196900 A班 11.591526
2015-06-27 1075.488000 A班 1.745478
2015-06-27 269.358100 A班 0.437158
2015-06-27 262.080000 A班 0.425346
2015-06-27 2423.070000 A班 3.932554
2015-06-27 187.680000 A班 0.304597
小计 897601.015136 A班 1456.773600
2015-06-04 509.443200 B班 0.826807
2015-06-04 396.740000 B班 0.643894
2015-06-04 390.000200 B班 0.632956
2015-06-29 564.375000 B班 0.915960
2015-06-29 777.382200 B班 1.261663
2015-06-29 1094.683200 B班 1.776631
小计 1087678.074338 B班 1765.261642
2015-06-12 1850.688000 处理 3.003599
小计 1850.688000 处理 3.003599
2015-06-01 115.000000 单瓦 0.186640
2015-06-01 1179.502500 单瓦 1.914289
2015-06-18 5315.212800 单瓦 8.626397
2015-06-18 609.000000 单瓦 0.988384
2015-06-18 800.400000 单瓦 1.299020
2015-06-23 1409.362500 单瓦 2.287344
2015-06-23 557.613000 单瓦 0.904985
2015-06-23 558.280800 单瓦 0.906069
2015-06-23 876.887200 单瓦 1.423156
2015-06-23 909.744800 单瓦 1.476482
2015-06-23 665.648750 单瓦 1.080323
2015-06-23 823.741800 单瓦 1.336903
2015-06-23 4433.322000 单瓦 7.195120
2015-06-23 2534.400000 单瓦 4.113239
5-06-27 478.56250 单瓦 0.776689
2015-06-27 325.820250 单瓦 0.528794
2015-06-27 361.974000 单瓦 0.587470
2015-06-27 893.142250 单瓦 1.449537
2015-06-27 542.976000 单瓦 0.881230
2015-06-27 3311.153750 单瓦 5.373882
小计 457214.564300 单瓦 742.042572
2015-06-15 210.000000 废改 0.340822
2015-06-15 32.640000 废改 0.052973
小计 242.640000 废改 0.393795
现在,我只想,按班别,分类,汇总,每个月的,生成一条记录,就行。请问怎么改?
#5
没看明白!
把你的预期结果贴出来。
把你的预期结果贴出来。
#6
/*想做成这样的结果:
日期 三层面积 班别 主机手
2015年6月 897601.0151 A班 1456.7736
2015年6月 1087678.074 B班 1765.261642
2015年6月 457214.5643 单瓦 742.042572
*/
实际上,就是只要小计.按班别+月份进行汇总.
#7
只是“求小计”,别说“加小计”啊。“加”么明细和小计都得有。
select CONVERT(varchar(7),MAX(t0.inputdate),120) as 日期,
sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000) as 三层面积,
t0.classid as 班别,
(sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000)*0.0274279*0.29586/5) as 主机手
from t_zbinput_d t1,order_ba1 t2,t_zbinput_M t0
where t1.orderid=t2.orderid
and t0.sid=t1.mastersid
and t2.pfloor in(2,3)
and t0.inputdate>='2015-06-01 00:00:00.000' and t0.inputdate<='2015-06-30 00:00:00.000'
group by t0.classid
#8
不好意思,实际上,我是要算生产线上各岗位的日产量工资.
现在代码我改成如下:
select CONVERT(varchar(10),MAX(t0.inputdate),23) as 日期,
sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000) as 面积,
t0.classid as 班别,
(sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000)*0.0274279*0.29586/5)as 主机手,
(sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000)*0.0274279*0.33889/7)as 副机手,
(sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000)*0.0274279*0.06992)as 代班,
(sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000)*0.0274279*0.29533/6)as 抬板,
(sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000)*0.002/6)as 打捆,
(sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000)*0.0021/2)as 查板
from t_zbinput_d t1,order_ba1 t2,t_zbinput_M t0
where t1.orderid=t2.orderid
and t0.sid=t1.mastersid
and t2.pfloor in(2,3)
group by t0.classid,t0.inputdate order by t0.inputdate asc
/*
日期 面积 班别 主机手 副机手 代班 抬板 打捆 查板
2013-01-04 2197.965000 A 3.567217 2.918596 4.215167 2.967356 0.732655 2.307863
2013-01-04 2355.920000 A 3.823572 3.128339 4.518086 3.180602 0.785306 2.473716
2013-01-04 448.500000 D 0.727899 0.595546 0.860115 0.605496 0.149500 0.470925
2013-01-05 3928.963500 A 6.376565 5.217125 7.534804 5.304285 1.309654 4.125411
2013-01-05 643.977000 A 1.045151 0.855113 1.234993 0.869399 0.214659 0.676176
2013-01-05 738.385875 A 1.198373 0.980475 1.416046 0.996855 0.246128 0.775305
*/
我想统计日产量工资,为什么每日的产量没有进行合计?麻烦你了.
#9
group by t0.classid,CONVERT(varchar(10),MAX(t0.inputdate),23)
#10
你可以考虑使用GROUPING,ROLLUP和CUBE
进行小计和合计
http://www.cnblogs.com/nikyxxx/archive/2012/11/27/2791001.html
进行小计和合计
http://www.cnblogs.com/nikyxxx/archive/2012/11/27/2791001.html
WITH a(三层面积,班别,日期,主机手) AS (
SELECT 10930.723500,N'A班','2015-06-01 22:11:19.203',17.740167 UNION ALL
SELECT 1636.878961,N'A班','2015-06-02 03:30:36.670',2.656595 UNION ALL
SELECT 4344.250800,N'A班','2015-06-02 06:34:24.140',7.050561 UNION ALL
SELECT 2395.518390,N'B班','2015-06-02 07:01:25.547',3.887839 UNION ALL
SELECT 2419.762900,N'B班','2015-06-02 07:06:41.313',3.927187
)
SELECT convert(nvarchar,YEAR(日期))+N'年'+convert(nvarchar, month(日期))+N'月',case when GROUPING(班别)=1 then N'班别小计' else 班别 end,主机手,sum(三层面积)
,GROUPING(主机手),GROUPING(班别),GROUPING(convert(nvarchar,YEAR(日期))+N'年'+convert(nvarchar, month(日期))+N'月')
FROM a GROUP BY convert(nvarchar,YEAR(日期))+N'年'+convert(nvarchar, month(日期))+N'月',主机手,班别 with rollup
#1
程序部分处理一下咯咯咯
#2
WITH a(三层面积,班别,日期,主机手) AS (
/* 把你原先的 SQL 放在这里,下面的测试数据不要 */
SELECT 10930.723500,'A班','2015-06-01 22:11:19.203',17.740167 UNION ALL
SELECT 1636.878961,'A班','2015-06-02 03:30:36.670',2.656595 UNION ALL
SELECT 4344.250800,'A班','2015-06-02 06:34:24.140',7.050561 UNION ALL
SELECT 2395.518390,'B班','2015-06-02 07:01:25.547',3.887839 UNION ALL
SELECT 2419.762900,'B班','2015-06-02 07:06:41.313',3.927187
)
,b AS (
SELECT 1 flag,
SUM(三层面积) 三层面积,
班别,
'小计' 日期,
SUM(主机手) 主机手
FROM a
GROUP BY 班别
UNION ALL
SELECT 0 flag, *
FROM a
)
SELECT 三层面积,班别,日期,主机手
FROM b
ORDER BY 班别,flag
三层面积 班别 日期 主机手
-------------- ---- ----------------------- --------------
10930.723500 A班 2015-06-01 22:11:19.203 17.740167
1636.878961 A班 2015-06-02 03:30:36.670 2.656595
4344.250800 A班 2015-06-02 06:34:24.140 7.050561
16911.853261 A班 小计 27.447323
2395.518390 B班 2015-06-02 07:01:25.547 3.887839
2419.762900 B班 2015-06-02 07:06:41.313 3.927187
4815.281290 B班 小计 7.815026
#3
-- 参考一下这个语法
select o.id ,o.name , c.xtype, COUNT(*)
from sysobjects o , syscolumns c
where o.id = c.id and o.xtype = 'U'
group by rollup(o.id,o.name,c.xtype)
#4
非常感谢。不过,我现在将代码改成了如下:
WITH a(日期,三层面积,班别,主机手) AS (
select CONVERT(varchar(10),t0.inputdate,23) as 日期,
sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000) as 三层面积,
t0.classid as 班别,
(sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000)*0.0274279*0.29586/5)as 主机手
from t_zbinput_d t1,order_ba1 t2,t_zbinput_M t0
where t1.orderid=t2.orderid and t0.sid=t1.mastersid and t2.pfloor in(2,3) and t0.inputdate>='2015-06-01 00:00:00.000' and t0.inputdate<='2015-06-30 00:00:00.000'
group by t0.classid, t0.inputdate,t1.orderid,t0.inputdate),
b AS (
SELECT 1 flag,
'小计' 日期,
SUM(三层面积) 三层面积,
班别,
SUM(主机手) 主机手
FROM a
GROUP BY 班别
UNION ALL
SELECT 0 flag, *
FROM a
)
SELECT 日期,三层面积,班别,主机手
FROM b
ORDER BY 班别,flag
/*
日期 三层面积 班别 主机手
2015-06-01 4337.280000 A班 7.039248
2015-06-01 1566.734400 A班 2.542753
2015-06-27 186.020000 A班 0.301903
2015-06-27 183.190400 A班 0.297311
2015-06-27 963.168000 A班 1.563186
2015-06-27 496.006000 A班 0.804999
2015-06-27 7142.196900 A班 11.591526
2015-06-27 1075.488000 A班 1.745478
2015-06-27 269.358100 A班 0.437158
2015-06-27 262.080000 A班 0.425346
2015-06-27 2423.070000 A班 3.932554
2015-06-27 187.680000 A班 0.304597
小计 897601.015136 A班 1456.773600
2015-06-04 509.443200 B班 0.826807
2015-06-04 396.740000 B班 0.643894
2015-06-04 390.000200 B班 0.632956
2015-06-29 564.375000 B班 0.915960
2015-06-29 777.382200 B班 1.261663
2015-06-29 1094.683200 B班 1.776631
小计 1087678.074338 B班 1765.261642
2015-06-12 1850.688000 处理 3.003599
小计 1850.688000 处理 3.003599
2015-06-01 115.000000 单瓦 0.186640
2015-06-01 1179.502500 单瓦 1.914289
2015-06-18 5315.212800 单瓦 8.626397
2015-06-18 609.000000 单瓦 0.988384
2015-06-18 800.400000 单瓦 1.299020
2015-06-23 1409.362500 单瓦 2.287344
2015-06-23 557.613000 单瓦 0.904985
2015-06-23 558.280800 单瓦 0.906069
2015-06-23 876.887200 单瓦 1.423156
2015-06-23 909.744800 单瓦 1.476482
2015-06-23 665.648750 单瓦 1.080323
2015-06-23 823.741800 单瓦 1.336903
2015-06-23 4433.322000 单瓦 7.195120
2015-06-23 2534.400000 单瓦 4.113239
5-06-27 478.56250 单瓦 0.776689
2015-06-27 325.820250 单瓦 0.528794
2015-06-27 361.974000 单瓦 0.587470
2015-06-27 893.142250 单瓦 1.449537
2015-06-27 542.976000 单瓦 0.881230
2015-06-27 3311.153750 单瓦 5.373882
小计 457214.564300 单瓦 742.042572
2015-06-15 210.000000 废改 0.340822
2015-06-15 32.640000 废改 0.052973
小计 242.640000 废改 0.393795
现在,我只想,按班别,分类,汇总,每个月的,生成一条记录,就行。请问怎么改?
#5
没看明白!
把你的预期结果贴出来。
把你的预期结果贴出来。
#6
/*想做成这样的结果:
日期 三层面积 班别 主机手
2015年6月 897601.0151 A班 1456.7736
2015年6月 1087678.074 B班 1765.261642
2015年6月 457214.5643 单瓦 742.042572
*/
实际上,就是只要小计.按班别+月份进行汇总.
#7
只是“求小计”,别说“加小计”啊。“加”么明细和小计都得有。
select CONVERT(varchar(7),MAX(t0.inputdate),120) as 日期,
sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000) as 三层面积,
t0.classid as 班别,
(sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000)*0.0274279*0.29586/5) as 主机手
from t_zbinput_d t1,order_ba1 t2,t_zbinput_M t0
where t1.orderid=t2.orderid
and t0.sid=t1.mastersid
and t2.pfloor in(2,3)
and t0.inputdate>='2015-06-01 00:00:00.000' and t0.inputdate<='2015-06-30 00:00:00.000'
group by t0.classid
#8
不好意思,实际上,我是要算生产线上各岗位的日产量工资.
现在代码我改成如下:
select CONVERT(varchar(10),MAX(t0.inputdate),23) as 日期,
sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000) as 面积,
t0.classid as 班别,
(sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000)*0.0274279*0.29586/5)as 主机手,
(sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000)*0.0274279*0.33889/7)as 副机手,
(sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000)*0.0274279*0.06992)as 代班,
(sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000)*0.0274279*0.29533/6)as 抬板,
(sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000)*0.002/6)as 打捆,
(sum(t1.goodnum*t2.PLONG*t2.WIDE/1000000)*0.0021/2)as 查板
from t_zbinput_d t1,order_ba1 t2,t_zbinput_M t0
where t1.orderid=t2.orderid
and t0.sid=t1.mastersid
and t2.pfloor in(2,3)
group by t0.classid,t0.inputdate order by t0.inputdate asc
/*
日期 面积 班别 主机手 副机手 代班 抬板 打捆 查板
2013-01-04 2197.965000 A 3.567217 2.918596 4.215167 2.967356 0.732655 2.307863
2013-01-04 2355.920000 A 3.823572 3.128339 4.518086 3.180602 0.785306 2.473716
2013-01-04 448.500000 D 0.727899 0.595546 0.860115 0.605496 0.149500 0.470925
2013-01-05 3928.963500 A 6.376565 5.217125 7.534804 5.304285 1.309654 4.125411
2013-01-05 643.977000 A 1.045151 0.855113 1.234993 0.869399 0.214659 0.676176
2013-01-05 738.385875 A 1.198373 0.980475 1.416046 0.996855 0.246128 0.775305
*/
我想统计日产量工资,为什么每日的产量没有进行合计?麻烦你了.
#9
group by t0.classid,CONVERT(varchar(10),MAX(t0.inputdate),23)
#10
你可以考虑使用GROUPING,ROLLUP和CUBE
进行小计和合计
http://www.cnblogs.com/nikyxxx/archive/2012/11/27/2791001.html
进行小计和合计
http://www.cnblogs.com/nikyxxx/archive/2012/11/27/2791001.html
WITH a(三层面积,班别,日期,主机手) AS (
SELECT 10930.723500,N'A班','2015-06-01 22:11:19.203',17.740167 UNION ALL
SELECT 1636.878961,N'A班','2015-06-02 03:30:36.670',2.656595 UNION ALL
SELECT 4344.250800,N'A班','2015-06-02 06:34:24.140',7.050561 UNION ALL
SELECT 2395.518390,N'B班','2015-06-02 07:01:25.547',3.887839 UNION ALL
SELECT 2419.762900,N'B班','2015-06-02 07:06:41.313',3.927187
)
SELECT convert(nvarchar,YEAR(日期))+N'年'+convert(nvarchar, month(日期))+N'月',case when GROUPING(班别)=1 then N'班别小计' else 班别 end,主机手,sum(三层面积)
,GROUPING(主机手),GROUPING(班别),GROUPING(convert(nvarchar,YEAR(日期))+N'年'+convert(nvarchar, month(日期))+N'月')
FROM a GROUP BY convert(nvarchar,YEAR(日期))+N'年'+convert(nvarchar, month(日期))+N'月',主机手,班别 with rollup