ID SBA TJA SBB TJB MONTH CITY
-------------------------------------------
0001 1 1 0 1 201301 CITYA
0002 1 0 1 0 201301 CITYA
0003 1 1 0 1 201301 CITYB
0004 1 0 1 0 201301 CITYB
查询条件是 MONTH CITY ,CITY 如果不输就是查询全部CITY
比如我 查询的条件是 201301 ,全部CITY
要得到这样的结果:
相同CITY ,相同月份的 数据,列数值相加
2,1,1,1 201201 CITYA
2,1,1,1 201201 CITYB
select sum(TJA),sum(SBA),sum(TJB),sum(SBB),,,, MONTH,CITY where MONTH={0} order by CITY
这么写是不是不行?
------解决方案--------------------
分析函数.
------解决方案--------------------
-- 用GROUP BY就可以了吧
select sum(TJA),sum(SBA),sum(TJB),sum(SBB),,,, MONTH,CITY
where MONTH={0}
group by month, city
order by month,CITY
------解决方案--------------------
create table tb
(
ID varchar2(4),
SBA number,
TJA number,
SBB number,
TJB number,
MONTH varchar2(6),
CITY varchar2(5)
);
insert into tb(ID,SBA,TJA,SBB,TJB,MONTH,CITY)values('0001',1,1,0,1,'201301','CITYA');
insert into tb(ID,SBA,TJA,SBB,TJB,MONTH,CITY)values('0002',1,0,1,0,'201301','CITYA');
insert into tb(ID,SBA,TJA,SBB,TJB,MONTH,CITY)values('0003',1,1,0,1,'201301','CITYB');
insert into tb(ID,SBA,TJA,SBB,TJB,MONTH,CITY)values('0004',1,0,1,0,'201301','CITYB');
select
sum(SBA) as SBA,
sum(TJA) as TJA,
sum(SBB) as SBB,
sum(TJB) as TJB,
MONTH,
CITY
from
tb
group by
MONTH,
CITY;
/*
SBA TJA SBB TJB MONTH CITY
---------------------- ---------------------- ---------------------- ---------------------- ------ -----
2 1 1 1 201301 CITYA