SQL除零错误和返回多值问题
select sum1/sum2 from
(select UsrA01.E0122 as id,sum(C411F+C411G+C411E+C411P+C411R) sum1 from UsrA41
right join UsrA01 on UsrA41.A0100=UsrA01.A0100
where YEAR(A41Z0)='2013' and month(A41Z0) = '01' and E0122 like '01%' group by UsrA01.E0122) tbl1
left join
(select UsrA01.E0122 as id,sum(C411F+C411G+C411E+C411P+C411R) sum2 from UsrA41
right join UsrA01 on UsrA41.A0100=UsrA01.A0100
where YEAR(A41Z0)='2013' and month(A41Z0) = '02' and E0122 like '01%' group by UsrA01.E012) tbl2
on tbl1.id=tbl2.id
select
case when
(select sum(C411F+C411G+C411E+C411P+C411R) sum2 from UsrA41
right join UsrA01 on UsrA41.A0100=UsrA01.A0100
where YEAR(A41Z0)='2013' and month(A41Z0) = '02' and E0122 like '01%' group by UsrA01.E0122) =0
then 0
else(
select sum1/sum2 from
(select UsrA01.E0122 as id,sum(C411F+C411G+C411E+C411P+C411R) sum1 from UsrA41
right join UsrA01 on UsrA41.A0100=UsrA01.A0100
where YEAR(A41Z0)='2013' and month(A41Z0) = '01' and E0122 like '01%' group by UsrA01.E0122) tbl1
left join
(select UsrA01.E0122 as id,sum(C411F+C411G+C411E+C411P+C411R) sum2 from UsrA41
right join UsrA01 on UsrA41.A0100=UsrA01.A0100
where YEAR(A41Z0)='2013' and month(A41Z0) = '02' and E0122 like '01%' group by UsrA01.E0122) tbl2
on tbl1.id=tbl2.id) end from UsrA01
SELECT sum1 / CASE WHEN sum2 = 0 THEN 1 ELSE sum2 END --这个地方改一下
FROM ( SELECT UsrA01.E0122 AS id ,
SUM(C411F + C411G + C411E + C411P + C411R) sum1
FROM UsrA41
RIGHT JOIN UsrA01 ON UsrA41.A0100 = UsrA01.A0100
WHERE YEAR(A41Z0) = '2013'
AND MONTH(A41Z0) = '01'
AND E0122 LIKE '01%'
GROUP BY UsrA01.E0122
) tbl1
LEFT JOIN ( SELECT UsrA01.E0122 AS id ,
SUM(C411F + C411G + C411E + C411P + C411R) sum2
FROM UsrA41
RIGHT JOIN UsrA01 ON UsrA41.A0100 = UsrA01.A0100
WHERE YEAR(A41Z0) = '2013'
AND MONTH(A41Z0) = '02'
AND E0122 LIKE '01%'
GROUP BY UsrA01.E012
) tbl2 ON tbl1.id = tbl2.id