select a.BODY_NO,
a.BODY_NAME,
a.rpt_month,
round(sum(case
when inout_flag = 'IN' then
a.sum1
else
0
end),
2) SUM1,
round(sum(case
when inout_flag = 'OUT' then
a.sum1
else
0
end),
2) +
(select round(sum(sum1 *
(last_day(to_date(b.rpt_month || '01 23:59:59',
'yyyymmdd hh24:mi:ss')) -
to_date(b.rpt_month || '01 00:00:00',
'yyyymmdd hh24:mi:ss'))),
2)
from v_mb_dailynew b
where b.avg_flag = 'Y'
and b.BODY_NO = 'Z5111'
AND B.rpt_month = A.rpt_month) SUM_COST
from v_mb_dailynew a
where a.avg_flag <> 'Y'
and a.BODY_NO = 'Z5111'
and a.rpt_month between '201701' and '201706'
group by a.body_no,
a.body_name,
f_get_empname(a.emp_no),
f_get_mb_countemp(a.body_no, 'MB'),
a.rpt_month
需要查询从'Z5111'到'Z5150'的数据,如何进行优化?
你这个写的是不是有点太