请教统计,查询的SQL语句。谢谢大家
create table t_kc--库存表
(
type varchar(50),--材料类别
mc varchar(50),--材料名称
c int, --材料长度
k int, --材料宽度
h int, --材料厚度
xh varchar(50),--材料型号
gg varchar(50),--材料规格
dj int, --材料单价
sl int, --出入库数量
status char(1),-- 出入库状态(1为入库,0为出库)
pch int --批次号
)
--板材入库
insert into t_kc select '板材 ', '钢板 ',6,1,3,null, '6*1*3 ',4000,10, '1 ',1
insert into t_kc select '板材 ', '钢板 ',6,1,5,null, '6*1*5 ',3000,10, '1 ',2
insert into t_kc select '板材 ', '锰板 ',10,2,5,null, '10*2*5 ',3500,10, '1 ',3
--型材入库
insert into t_kc select '型材 ', '槽钢 ',10,null,null, '10# ', '10#*10 ',3800,10, '1 ',4
insert into t_kc select '型材 ', '方管 ',10,null,null,null, '20*40 ',3500,10, '1 ',5
--板材出库
insert into t_kc select '板材 ', '钢板 ',1,1,3,null, '1*1*3 ',4000,8, '0 ',4
insert into t_kc select '板材 ', '钢板 ',1,1,5,null, '1*1*5 ',3000,8, '0 ',5
insert into t_kc select '板材 ', '锰板 ',1,2,5,null, '1*2*5 ',3500,8, '0 ',6
--型材出库
insert into t_kc select '型材 ', '槽钢 ',1,null,null, '10# ', '10#*1 ',3800,8, '0 ',7
insert into t_kc select '型材 ', '方管 ',1,null,null,null, '20*40 ',3500,8, '0 ',8
/*要求
1.对于板材:按照类别,名称进行分组,得到不同名称的板材的入库总面积,出库总面积,结余总面积
2.对于型材:按照类别,名称进行分组,得到不同名称的型材的入库总长度,出库总长度,结余总长度
3.查询出结余总面积大于0的板材的名称
4.查询出结余总长度大于0的型材的名称
*/
drop table t_kc
[解决办法]
try:
--1.对于板材:按照类别,名称进行分组,得到不同名称的板材的入库总面积,出库总面积,结余总面积
SELECT type,mc ,SUM(CASE status WHEN '1 ' THEN c*k ELSE 0 END) [入库总面积]
,SUM(CASE status WHEN '0 ' THEN c*k ELSE 0 END) [出库总面积]
,SUM(CASE status WHEN '1 ' THEN c*k ELSE -c*k END) [结余总面积]
FROM t_kc WHERE type= '板材 ' GROUP BY type,mc
--2.对于型材:按照类别,名称进行分组,得到不同名称的型材的入库总长度,出库总长度,结余总长度
SELECT type,mc ,SUM(CASE status WHEN '1 ' THEN c ELSE 0 END) [入库总长度]
,SUM(CASE status WHEN '0 ' THEN c ELSE 0 END) [出库总长度]
,SUM(CASE status WHEN '1 ' THEN c ELSE -c END) [余总长度]
FROM t_kc WHERE type= '型材 ' GROUP BY type,mc
--3.查询出结余总面积大于0的板材的名称
SELECT mc FROM t_kc WHERE type= '板材 ' GROUP BY type,mc HAVING SUM(CASE status WHEN '1 ' THEN c*k ELSE -c*k END)> 0
--4.查询出结余总长度大于0的型材的名称
SELECT mc FROM t_kc WHERE type= '型材 ' GROUP BY type,mc HAVING SUM(CASE status WHEN '1 ' THEN c ELSE -c END)> 0
[解决办法]
1. select a.type,sum(a.c*a.k) as rmj,sum(b.c*b.k) as cmj,sum(a.c*a.k) - sum(b.c*b.k)
from
( select * from t_kc where status = '0 ') a,
( select * from t_kc where status = '0 ') b
where a.type = b.type
group by a.type
[解决办法]
create table t_kc--库存表
(
type varchar(50),--材料类别
mc varchar(50),--材料名称
c int, --材料长度
k int, --材料宽度
h int, --材料厚度
xh varchar(50),--材料型号
gg varchar(50),--材料规格
dj int, --材料单价
sl int, --出入库数量
status char(1),-- 出入库状态(1为入库,0为出库)
pch int --批次号
)
--板材入库
insert into t_kc select '板材 ', '钢板 ',6,1,3,null, '6*1*3 ',4000,10, '1 ',1
insert into t_kc select '板材 ', '钢板 ',6,1,5,null, '6*1*5 ',3000,10, '1 ',2
insert into t_kc select '板材 ', '锰板 ',10,2,5,null, '10*2*5 ',3500,10, '1 ',3
--型材入库
insert into t_kc select '型材 ', '槽钢 ',10,null,null, '10# ', '10#*10 ',3800,10, '1 ',4
insert into t_kc select '型材 ', '方管 ',10,null,null,null, '20*40 ',3500,10, '1 ',5
--板材出库
insert into t_kc select '板材 ', '钢板 ',1,1,3,null, '1*1*3 ',4000,8, '0 ',4
insert into t_kc select '板材 ', '钢板 ',1,1,5,null, '1*1*5 ',3000,8, '0 ',5
insert into t_kc select '板材 ', '锰板 ',1,2,5,null, '1*2*5 ',3500,8, '0 ',6
--型材出库
insert into t_kc select '型材 ', '槽钢 ',1,null,null, '10# ', '10#*1 ',3800,8, '0 ',7
insert into t_kc select '型材 ', '方管 ',1,null,null,null, '20*40 ',3500,8, '0 ',8
1、
select type,mc,sum(case when status=1 then c*k end) as 入库总面积,sum(case when status=0 then c*k end) as 出库总面积,
sum(case when status=1 then c*k when status=0 then -c*k else 0 end) as 结余总面积
from t_kc
where type= '板材 '
group by type,mc
2、
select type,mc,sum(case when status=1 then c end) as 入库总长度,sum(case when status=0 then c end) as 出库总长度,
sum(case when status=1 then c else -c end) as 结余总长度
from t_kc
where type= '型材 '
group by type,mc
3、
select * from (
select type,mc,sum(case when status=1 then c*k end) as 入库总面积,sum(case when status=0 then c*k end) as 出库总面积,
sum(case when status=1 then c*k when status=0 then -c*k else 0 end) as 结余总面积
from t_kc
where type= '板材 '
group by type,mc) a
where 结余总面积> 0
4、
select * from (
select type,mc,sum(case when status=1 then c end) as 入库总长度,sum(case when status=0 then c end) as 出库总长度,
sum(case when status=1 then c else -c end) as 结余总长度
from t_kc
where type= '型材 '
group by type,mc) a
where 结余总长度> 0