Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 0.86 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. SELECT
  2.   max(CASE
  3.       WHEN v.division IN (1,3) THEN 'FR'
  4.       WHEN v.division = 2 THEN 'DAR'
  5.       ELSE 'Unknown' END) AS dept,
  6.   v.vehicle AS vehicle,
  7.   sum(fh.fh_tran_gallons) AS diesel,
  8.   sum(fh_tran_fuel_price*fh.fh_tran_gallons) AS cost,
  9.   sum(fh.fh_tran_miles) AS miles,
  10.   sum(CASE WHEN fh_tran_bucket_type = 2 THEN fh.fh_antifr_qty ELSE 0 END) AS oil,
  11.   sum(CASE WHEN fh_tran_bucket_type = 3 THEN fh.fh_antifr_qty ELSE 0 END) AS anti,
  12.   CASE WHEN sum(fh.fh_tran_miles) != 0 AND sum(fh.fh_tran_gallons) != 0 THEN
  13.     round(sum(fh.fh_tran_miles)/sum(fh.fh_tran_gallons),2)
  14.   ELSE 0 END AS mpg,
  15.   SUM(CASE WHEN fh.fh_tran_gallons > 0 THEN 1 ELSE 0 END) AS activity
  16. FROM
  17.   vehfile v
  18. LEFT OUTER JOIN
  19.   flhistfl fh
  20. ON
  21.   fh.fh_tran_vehicle=v.vehicle AND fh.fh_tran_date BETWEEN $start AND $end
  22. WHERE
  23.   v.vehicle IN ($vehicles)
  24. GROUP BY
  25.   v.vehicle
  26. ORDER BY
  27.   dept DESC,v.vehicle