现在有一张表t_table
它有这样几个字段:level_1_count,level_2_count,level_3_count,time,等字段。现在要统计分别统计time在2011年5月16日level_1_count的value为1,level_2_count的value为1,level_3_count的value为1当中的记录条数,count(level_1_count),count(level_2_count),count(level_3_count),这个应该怎么写sql啊?还望大虾们指点
它有这样几个字段:level_1_count,level_2_count,level_3_count,time,等字段。现在要统计分别统计time在2011年5月16日level_1_count的value为1,level_2_count的value为1,level_3_count的value为1当中的记录条数,count(level_1_count),count(level_2_count),count(level_3_count),这个应该怎么写sql啊?还望大虾们指点
from t_table
where trunc(time) = to_date('20110516','yyyymmdd')
and (level_1_count=1 and level_2_count=1 and level_3_count = 1)
group by time
with t as(
select '1' level1 ,'1' level2 ,'1' level3 ,'20110516' times from dual
union all
select '1' level1 ,'1' level2 ,'1' level3 ,'20110516' times from dual
union all
select '1' level1 ,'2' level2 ,'1' level3 ,'20110516' times from dual
union all
select '1' level1 ,'1' level2 ,'1' level3 ,'20110516' times from dual
union all
select '1' level1 ,'1' level2 ,'2' level3 ,'20110518' times from dual
)select t.times,count(level1),count(level2),count(level3)
from t
where t.times = '20110516'
and t.level1 = '1'
and t.level2 = '1'
and t.level3 = '1'
group by t.times
这样的??
不是这个意思,我意思是满足level_1_count=1的,count(level_1_count),level_2_count=1的,count(level_2_count),level_3_count=1的,count(level_3_count)的分别统计。楼上这样写的话就是满足同时满足那三个条件的
那就用case吧。
sum(case
when level_1_count=1 then
1
else
0
end case) level_1_count_tol
其他如此类推