例如表Test,表如下
Visit Section Name No
20090208 1 A 1
20090208 1 A 1
20090208 1 A 1
20090208 1 A 2
20090208 1 A 2
20090208 1 A 3
20090209 2 A 1
20090209 2 A 2
想要的结果是
Visit Section Name No1 No2 No3
20080208 1 A 3 2 1
20090209 2 A 1 1
也就是根据前三个字段分组,Count(No),并根据No的值进行统计然后把具有相同(Visit,Section,Name)的Count(No)合并到一行中,目前No的值只有三个(1,2,3)
要求不能用子查询,并且所用sql语句尽量符合ANSI标准
请问如何解决?谢谢!!!!
13 个解决方案
#1
分析函数 lead()
#2
try this:
WITH test AS (
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,2 no from dual union all
select 20090208 visit,1 section,'A' name,2 no from dual union all
select 20090208 visit,1 section,'A' name,3 no from dual union all
select 20090209 visit,2 section,'A' name,1 no from dual union all
select 20090209 visit,2 section,'A' name,2 no from dual
)
select visit,section,name,
sum(case when no=1 then 1 end) no1,
sum(case when no=2 then 1 end) no2,
sum(case when no=3 then 1 end) no3
from test group by visit,section,name
WITH test AS (
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,2 no from dual union all
select 20090208 visit,1 section,'A' name,2 no from dual union all
select 20090208 visit,1 section,'A' name,3 no from dual union all
select 20090209 visit,2 section,'A' name,1 no from dual union all
select 20090209 visit,2 section,'A' name,2 no from dual
)
select visit,section,name,
sum(case when no=1 then 1 end) no1,
sum(case when no=2 then 1 end) no2,
sum(case when no=3 then 1 end) no3
from test group by visit,section,name
#3
SQL> select visit,section,name,sum(decode(no,'1','1','0'))no1,sum(decode(no,'2','1','0'))no2, sum(decode(no,'3','1','0'))no3 from test group by visit,section,name;
看一下,符不符合你的要求 !
看一下,符不符合你的要求 !
#4
为什么要求和?
#5
估计不行,因为可能存在no4,no5,no6,no7....
#6
刚才没有看清楚题目,只有三个值
所以
所以
select visit,section,name,sum(decode(no,1,1,0))no1,sum(decode(no,2,1,0))no2, sum(decode(no,3,1,0))no3 from test group by visit,section,name;
#7
设计的No字段只有三个值,且要求是不能用子查询,最好也不用case when 语句,且符合ansi标准,
谢谢各位的帮忙,我会去试验一下的,2楼的可以实现这个功能,但有没有更好的方法去掉case,when
谢谢各位的帮忙,我会去试验一下的,2楼的可以实现这个功能,但有没有更好的方法去掉case,when
#8
decode函数是Oracle专属函数吧
#9
#10
select visit, section, name,
sum(decode(no,1,1,0)) n01,
sum(decode(no,2,1,0)) n02,
sum(decode(no,3,1,0)) n03
from test
group by visit, section, name
order by 1,2,3
sum(decode(no,1,1,0)) n01,
sum(decode(no,2,1,0)) n02,
sum(decode(no,3,1,0)) n03
from test
group by visit, section, name
order by 1,2,3
#11
decode是Oracle专有的,sqlserver中可以用CASE WHEN对应
#12
try this:
WITH test AS (
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,2 no from dual union all
select 20090208 visit,1 section,'A' name,2 no from dual union all
select 20090208 visit,1 section,'A' name,3 no from dual union all
select 20090209 visit,2 section,'A' name,1 no from dual union all
select 20090209 visit,2 section,'A' name,2 no from dual
)
select visit,section,name,
(select count(no) from test t1 where t1.visit=t.visit and t1.section=t.section and t1.name=t.name and no=1) no1,
(select count(no) from test t1 where t1.visit=t.visit and t1.section=t.section and t1.name=t.name and no=2) no2,
(select count(no) from test t1 where t1.visit=t.visit and t1.section=t.section and t1.name=t.name and no=3) no3
from test t group by visit,section,name;
#13
case when 就能实现这个功能,
我有个问题也和这个类似,就是在一个case语句中,根据相同的一个条件,统计出不同的字段,由于这个条件的查询效率比较低,所以不能在每个case中都使用,不知道各位有什么好办法没?
我有个问题也和这个类似,就是在一个case语句中,根据相同的一个条件,统计出不同的字段,由于这个条件的查询效率比较低,所以不能在每个case中都使用,不知道各位有什么好办法没?
#1
分析函数 lead()
#2
try this:
WITH test AS (
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,2 no from dual union all
select 20090208 visit,1 section,'A' name,2 no from dual union all
select 20090208 visit,1 section,'A' name,3 no from dual union all
select 20090209 visit,2 section,'A' name,1 no from dual union all
select 20090209 visit,2 section,'A' name,2 no from dual
)
select visit,section,name,
sum(case when no=1 then 1 end) no1,
sum(case when no=2 then 1 end) no2,
sum(case when no=3 then 1 end) no3
from test group by visit,section,name
WITH test AS (
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,2 no from dual union all
select 20090208 visit,1 section,'A' name,2 no from dual union all
select 20090208 visit,1 section,'A' name,3 no from dual union all
select 20090209 visit,2 section,'A' name,1 no from dual union all
select 20090209 visit,2 section,'A' name,2 no from dual
)
select visit,section,name,
sum(case when no=1 then 1 end) no1,
sum(case when no=2 then 1 end) no2,
sum(case when no=3 then 1 end) no3
from test group by visit,section,name
#3
SQL> select visit,section,name,sum(decode(no,'1','1','0'))no1,sum(decode(no,'2','1','0'))no2, sum(decode(no,'3','1','0'))no3 from test group by visit,section,name;
看一下,符不符合你的要求 !
看一下,符不符合你的要求 !
#4
为什么要求和?
#5
估计不行,因为可能存在no4,no5,no6,no7....
#6
刚才没有看清楚题目,只有三个值
所以
所以
select visit,section,name,sum(decode(no,1,1,0))no1,sum(decode(no,2,1,0))no2, sum(decode(no,3,1,0))no3 from test group by visit,section,name;
#7
设计的No字段只有三个值,且要求是不能用子查询,最好也不用case when 语句,且符合ansi标准,
谢谢各位的帮忙,我会去试验一下的,2楼的可以实现这个功能,但有没有更好的方法去掉case,when
谢谢各位的帮忙,我会去试验一下的,2楼的可以实现这个功能,但有没有更好的方法去掉case,when
#8
decode函数是Oracle专属函数吧
#9
#10
select visit, section, name,
sum(decode(no,1,1,0)) n01,
sum(decode(no,2,1,0)) n02,
sum(decode(no,3,1,0)) n03
from test
group by visit, section, name
order by 1,2,3
sum(decode(no,1,1,0)) n01,
sum(decode(no,2,1,0)) n02,
sum(decode(no,3,1,0)) n03
from test
group by visit, section, name
order by 1,2,3
#11
decode是Oracle专有的,sqlserver中可以用CASE WHEN对应
#12
try this:
WITH test AS (
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,1 no from dual union all
select 20090208 visit,1 section,'A' name,2 no from dual union all
select 20090208 visit,1 section,'A' name,2 no from dual union all
select 20090208 visit,1 section,'A' name,3 no from dual union all
select 20090209 visit,2 section,'A' name,1 no from dual union all
select 20090209 visit,2 section,'A' name,2 no from dual
)
select visit,section,name,
(select count(no) from test t1 where t1.visit=t.visit and t1.section=t.section and t1.name=t.name and no=1) no1,
(select count(no) from test t1 where t1.visit=t.visit and t1.section=t.section and t1.name=t.name and no=2) no2,
(select count(no) from test t1 where t1.visit=t.visit and t1.section=t.section and t1.name=t.name and no=3) no3
from test t group by visit,section,name;
#13
case when 就能实现这个功能,
我有个问题也和这个类似,就是在一个case语句中,根据相同的一个条件,统计出不同的字段,由于这个条件的查询效率比较低,所以不能在每个case中都使用,不知道各位有什么好办法没?
我有个问题也和这个类似,就是在一个case语句中,根据相同的一个条件,统计出不同的字段,由于这个条件的查询效率比较低,所以不能在每个case中都使用,不知道各位有什么好办法没?