日期:2014-05-16  浏览次数:20912 次

判断当前日期来决定是否执行sql 求助求助!!
做报表统计 查询条件是年份 如果是往年的记录就显示12个月份的记录 如果是今年的就显示当前月份之前的记录 我真的是没思路了 润乾报表 函数或是sql能解决问题的办法都可以

select distinct(quota.id),01 as month,quota.use_car_plan as use_car_plan,interior.sum_interior as sum_interior,exterior.sum_exterior 
,jiedai.sum_jiedai,huiyi.sum_huiyi,peixun.sum_peixun,gongyong.sum_gongyong,total.total_sum from(
(select quota.dept_id as id ,quota.dept_name,quota.use_car_plan as use_car_plan from dept_quota quota 
 where quota.year=? and quota.dept_id=?)quota
 left join 
 (select work.leader_dept_id as id, sum(work.total_fee) as sum_interior from car_apply_work work where work.leader_dept_id=? and substr
(work.ride_time,1,4)=? 
  and substr(work.ride_time,6,2)='01' and
work.dispatch_way='junei' and work.purpose='chailv' 
and work.state='close'group by work.leader_dept_id) interior on 
quota.id=interior.id 


left join 
(select work.leader_dept_id as id,sum(work.total_fee) as sum_exterior from car_apply_work work where work.leader_dept_id=? and  
work.dispatch_way<> 'junei' and work.purpose='chailv'
and work.state='close' and substr(work.ride_time,6,2)='01' 
and substr(work.ride_time,1,4)=? 
group by work.leader_dept_id) exterior on quota.id=exterior.id

left join 

(select work.leader_dept_id as id,sum(work.total_fee) as sum_jiedai from car_apply_work work where work.leader_dept_id=? and  
work.purpose='jiedai' 
and work.state='close' and substr(work.ride_time,6,2)='01' 
and substr(work.ride_time,1,4)=? group by work.leader_dept_id) jiedai on quota.id=jiedai.id

left join
(select work.leader_dept_id as id,sum(work.total_fee) as sum_huiyi from car_apply_work work where work.leader_dept_id=? and  
work.purpose='huiyi' 
and work.state='close' and substr(work.ride_time,6,2)='01' 
and substr(work.ride_time,1,4)=? group by work.leader_dept_id) huiyi on quota.id=huiyi.id

left join 

(select work.leader_dept_id as id,sum(work.total_fee) as sum_peixun from car_apply_work work where work.leader_dept_id=? and  
work.purpose='peixun' 
and work.state='close' and substr(work.ride_time,6,2)='01' 
and substr(work.ride_time,1,4)=? group by work.leader_dept_id) peixun on 
quota.id=peixun.id
left join 
(select work.leader_dept_id as id,sum(work.total_fee) as total_sum from car_apply_work work where work.leader_dept_id=?  
and work.purpose='chailv'
and work.state='close' and substr(work.ride_time,6,2)<='01' 
and substr(work.ride_time,1,4)=? 
group by work.leader_dept_id) total on quota.id=total.id
left join 

(select work.leader_dept_id as id,sum(work.total_fee) as sum_gongyong from car_apply_work work where work.leader_dept_id=? and  
work.purpose='gongyong' 
and work.state='close' and substr(work.ride_time,6,2)='01' 
and substr(work.ride_time,1,4)=? group by work.leader_dept_id) gongyong on 
quota.id=gongyong.id)

union all

select distinct(quota.id),02 as month,quota.use_car_plan as use_car_plan,interior.sum_interior as sum_interior,exterior.sum_exterior 
,jiedai.sum_jiedai,huiyi.sum_huiyi,peixun.sum_peixun,gongyong.sum_gongyong ,total.total_sum from(
(select quota.dept_id as id ,quota.dept_name,quota.use_car_plan as use_car_plan from dept_quota quota 
 where quota.year=? and quota.dept_id=?)quota
 left join 
 (select work.leader_dept_id as id, sum(work.total_fee) as sum_interior from car_apply_work work where work.leader_dept_id=? and substr
(work.ride_time,1,4)=? 
  and substr(work.ride_time,6,2)='02' and