//sql语句 case when then与子查询

select ontime,offtime,OnlineTime,a.exlane,car1count,car1toll,car2count,car2toll,car3count,car3toll,car4count,car4toll,car5count,car5toll from
(
select operator, dutyshift, stadate,exlane, exstation,sum(case when vehicleclass=1 or vehicleclass=11 then trafficsum end) as car1count,
sum(case when vehicleclass=1 or vehicleclass=11 then tollsum end) as car1toll,
sum(case when vehicleclass=2 or vehicleclass=12 then trafficsum end) as car2count,
sum(case when vehicleclass=2 or vehicleclass=12 then tollsum end) as car2toll,
sum(case when vehicleclass=3 or vehicleclass=13 then trafficsum end) as car3count,
sum(case when vehicleclass=3 or vehicleclass=13 then tollsum end) as car3toll,
sum(case when vehicleclass=4 or vehicleclass=14 then trafficsum end) as car4count,
sum(case when vehicleclass=4 or vehicleclass=14 then tollsum end) as car4toll,
sum(case when vehicleclass=5 or vehicleclass=15 then trafficsum end) as car5count,
sum(case when vehicleclass=5 or vehicleclass=15 then tollsum end) as car5toll
from tbltollsta where stadate='2018-03-29' and exstation=10401 and operator=124004
group by stadate,dutyshift,operator,exlane,exstation

) a,
(
select lane, operator,stadate,exstation,[shift],
min(ontime) as ontime ,
max(OffTime) as offtime,
sum(ROUND(CONVERT(float,DATEDIFF(MINUTE,[OnTime],[OffTime]))/60,2)) as OnlineTime
from TblLaneOnLine
where operator=124004 and stadate='2018-03-29' and exstation=10401
group by lane,operator,stadate,exstation,[shift]
) b
where a.operator=b.operator and a.stadate=b.stadate
and a.dutyshift=b.[shift] and a.exstation=b.exstation and a.exlane=b.lane

猜你喜欢

转载自www.cnblogs.com/jdm123/p/8931571.html