日期:2014-05-18  浏览次数:20589 次

求教一条SQL语句,能否用一条语句得到结果
表结果
Assess_Tab
Person_No A1 A2 B1 B2 B3 Check_Name_No Business_No
  26001 8 8 8 8 8 26004 1
  26002 8 8 8 8 8 26004 1
  26001 8 8 8 8 8 26005 1
  26002 8 8 8 8 8 26005 1
  26001 8 8 8 8 8 26006 3
  26002 8 8 8 8 8 26006 3
  26001 8 8 8 8 8 26007 3
  26002 8 8 8 8 8 26007 3
  26001 8 8 8 8 8 26008 4
  26002 8 8 8 8 8 26008 4

现要统计出如下Business_No=1时,合计占总分的40% Business_No=3时,占总数的30% Business_No=4 时,占数的30%
Person_No 1 3 4
 26001 (80/200)*0.4 (80/200)*0.3 (40/200)*0.3 
 26002 (80/200)*0.4 (80/200)*0.3 (40/200)*0.3 

请问能用一条语气查询出来吗,还是先把所有的数据查询出来,再在本地筛选求合乘除


------解决方案--------------------
SQL code
create table tb(Person_No varchar(10),A1 int,A2 int,B1 int,B2 int,B3 int,Check_Name_No varchar(10),Business_No int) 
insert into tb values('26001',  8,  8,  8,  8,  8,    '26004',      1)
insert into tb values('26002',  8,  8,  8,  8,  8,    '26004',      1)
insert into tb values('26001',  8,  8,  8,  8,  8,    '26005',      1)
insert into tb values('26002',  8,  8,  8,  8,  8,    '26005',      1)
insert into tb values('26001',  8,  8,  8,  8,  8,    '26006',      3)
insert into tb values('26002',  8,  8,  8,  8,  8,    '26006',      3)
insert into tb values('26001',  8,  8,  8,  8,  8,    '26007',      3)
insert into tb values('26002',  8,  8,  8,  8,  8,    '26007',      3)
insert into tb values('26001',  8,  8,  8,  8,  8,    '26008',      4)
insert into tb values('26002',  8,  8,  8,  8,  8,    '26008',      4)
go

select m.Person_No ,
max(case m.Business_No when 1 then (a1+a2+b1+b2+b3)*0.4/n.person_num else 0 end) '1',
max(case m.Business_No when 3 then (a1+a2+b1+b2+b3)*0.3/n.person_num else 0 end) '3',
max(case m.Business_No when 4 then (a1+a2+b1+b2+b3)*0.3/n.person_num else 0 end) '4'
from tb m,(select Person_No , sum(a1+a2+b1+b2+b3) person_num from tb group by Person_No) n
where m.Person_No = n.Person_No
group by m.Person_No

drop table tb

/*
Person_No  1            3            4           
---------- ------------------------- ------------------------- -------------------------
26001    .080000000000      .060000000000      .060000000000
26002    .080000000000      .060000000000      .060000000000

(所影响的行数为 2 行)
*/