日期:2014-05-17 浏览次数:20491 次
select a.jhid,jhmc,jhbh,sum(case when zt='Y' then 1 else 0 end)*100.0/count(1) bill
from jhb a inner join jhmxb b
on a.jhid=b.jhid
group by a.jhid,jhmc,jhbh
create table jh
(
jhid int,
jhmc varchar(100),
jhbh int
)
go
create table mx
(
mxid int,
mxmc varchar(100),
jhid int,
zt char(1)
)
go
insert into jh values(1,'jh1','2011')
insert into jh values(2,'jh2','2012')
insert into jh values(3,'jh3','2013')
insert into mx values(1,'mx1',1,'N')
insert into mx values(2,'mx2',1,'Y')
insert into mx values(3,'mx3',1,'Y')
insert into mx values(4,'mx4',2,'Y')
insert into mx values(5,'mx4',2,'N')
select jh.*,CAST(ISNULL(cnt,0)*100.0/(select COUNT(1) from mx) as int) as bili
from jh
left join
(
select jhid, COUNT(1) as cnt
from mx
group by jhid
) t
on jh.jhid = t.jhid
/*
jhid jhmc jhbh bili
1 jh1 2011 60
2 jh2 2012 40
3 jh3 2013 0
*/