求一个统计方面的sql,问题解决,马上结贴!在线等.....
用户表
create table USERINFO
(
id VARCHAR2(32) not null,
name VARCHAR2(1024),--姓名
phone VARCHAR2(255) --电话
)
电话通知状态表
create table tempdial
(
telno VARCHAR2(32) not null, --电话
insert_date DATE default sysdate not null,--插入时间
send_status NUMBER(1) default 0 not null --0表示通知中,1表示通知成功,2表示通知失败
)
需要查询出姓名,电话,通知中次数,通知成功次数,通知失败次数,通知总次数
------解决方案--------------------select t1.name,
t1.phone,
sum(decode(t2.send_status, 0, 1, 0)) 通知中,
sum(decode(t2.send_status, 1, 1, 0)) 通知成功,
sum(decode(t2.send_status, 2, 1, 0)) 通知失败,
sum(1) 通知总次数
from USERINFO t1, tempdial t2
where t1.phone = t2.telno
group by t1.name, t1.phone
------解决方案--------------------楼主看看这样是否OK?
select t1.name,
t1.phone,
count(select t.send_status from tempdial t where send_status='0') ,
count(select t.send_status from tempdial t where send_status='1') ,
count(select t.send_status from tempdial t where send_status='2') ,
count(select t.send_status from tempdial t )
from userinfo t1,tempdial t2
where t1.phone=t2.telno
------解决方案--------------------select t1.name,
t1.phone,
sum(decode(t2.send_status, 0, 1, 0)) 通知中,
sum(decode(t2.send_status, 1, 1, 0)) 通知成功,
sum(decode(t2.send_status, 2, 1, 0)) 通知失败,
count(1) 通知总次数
from USERINFO t1 left join tempdial t2
on t1.phone = t2.telno
group by t1.name, t1.phone