下面是我的sql语句:
select
case when t.cityname='武汉' then count(*) else null end "武汉",
case when t.cityname='孝感' then count(*) else null end "孝感"
from hb10000.td_nsert_temp t
group by t.cityname;输出结果如下:
武汉 孝感
1
2
3
4
5
6
7
8
9 37840
10
11
12
13 496
14 但是我只想要有数据的部分,就是能不能出现如下的结果:
武汉 孝感
1 37840 496
select
case when t.cityname='武汉' then count(*) else null end "武汉",
case when t.cityname='孝感' then count(*) else null end "孝感"
from hb10000.td_nsert_temp t
group by t.cityname;输出结果如下:
武汉 孝感
1
2
3
4
5
6
7
8
9 37840
10
11
12
13 496
14 但是我只想要有数据的部分,就是能不能出现如下的结果:
武汉 孝感
1 37840 496
select cityname,count(1)
from hb10000.td_nsert_temp
where cityname in('武汉','孝感')
group by cityname如果要放在一条记录上,就在这个结果的基础上行转列
(select count(*) from hb10000.td_nsert_temp where cityname = '孝感') 孝感 from dual;
你这里的空记录主要是有其他的city导致的。试试
select sum(decode(cityname,'武汉' 1, 0)) "武汉", sum(decode(cityname,'孝感' 1, 0)) "孝感" from hb10000.td_nsert_temp t
这个表中有3个字段,如下
create table td_nsert_temp(seq number(10),cityname varchar(3),amount number(10));
同样有多个city,如:
seq cityname amount
1 武汉 2
2 武汉 3
3 武汉 4
4 孝感 2
5 孝感 2
6 黄冈 2
1 黄冈 5
1 黄冈 7
要想出现如下结果:
武汉 孝感 黄冈
3 2 3
9 4 14
即第一行统计的是各个city出现的次数
而第二行统计的是对应的city中amount之和请问下这个可以怎么做呢?thanks!!!
sum(decode(cityname,'黄岗' 1, 0)) "黄岗"
from td_nsert_temp t
union all
select sum(decode(cityname,'武汉' amount, 0)) "武汉", sum(decode(cityname,'孝感' amount, 0)) "孝感"
sum(decode(cityname,'黄岗' amount, 0)) "黄岗"
from td_nsert_temp t
sum(decode(t.cityname,'武汉',1,0)) "武汉"
sum(decode(t.cityname,'孝感',1,0)) "孝感"
from hb10000.td_nsert_temp t
group by t.cityname;
用case或者decode都能实现看到家乡的名字 顶一下