Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.63 KB | None | 0 0
  1. (SELECT CIT.citation_id,PD.*,A.agency_name,AP.*,CAST((CAST(ISNULL(A.grace_period,5) AS FLOAT)/100) AS FLOAT)+1 AS theAmount,
  2. p.permit_num,C.*, O.*,R.service_rep_name,L.*,S.system_name,M.manufacturer_name,
  3. Case when ((AP.gallonsperminute IS not null and totalseconds * (AP.gallonsperminute/60) > AP.dailygallonlimit * CAST((CAST(ISNULL(A.grace_period,5) AS FLOAT)/100) AS FLOAT)+1) or (AP.gallonspercycle IS not null and totalcycles>AP.dailygallonlimit/AP.gallonspercycle * CAST((CAST(ISNULL(A.grace_period,5) AS FLOAT)/100) AS FLOAT)+1)) then 3 else 0 end As violation_type,
  4. Case when (AP.gallonsperminute IS not null and totalseconds * (AP.gallonsperminute/60) > AP.dailygallonlimit * CAST((CAST(ISNULL(A.grace_period,5) AS FLOAT)/100) AS FLOAT)+1) then totalseconds * (AP.gallonsperminute/60) - AP.dailygallonlimit * (1-CAST((CAST(ISNULL(A.grace_period,5) AS FLOAT)/100) AS FLOAT)) else (totalcycles*AP.gallonspercycle) - AP.dailygallonlimit* (1-CAST((CAST(ISNULL(A.grace_period,5) AS FLOAT)/100) AS FLOAT)) end as gallons_over
  5. FROM
  6. PumpDays PD, locations L
  7.       LEFT outer join Owners O ON O.owner_id=L.owner_id,
  8. systems S
  9.       LEFT outer join manufacturers M ON M.manufacturer_id=S.manufacturer_id
  10.       LEFT outer join service_contracts c ON C.system_id=S.system_id
  11.       LEFT outer join service_reps R ON C.service_rep_id=R.service_rep_id,
  12. system_permits sp, permits p
  13.       LEFT outer join citations cit ON cit.permit_num = p.permit_num,
  14. permits AP, Agencies A
  15. WHERE p.permit_num = AP.permit_num and p.active=1 and AP.active=1 and AP.permit_id <> P.permit_id and PD.assoc_type=4 and
  16. A.agency_id = AP.agency_id and S.location_id=L.location_id and L.assoc_type=4 and L.assoc_id = 15008 AND
  17. p.permit_num = PD.permit_num and sp.system_id = s.system_id and p.permit_id = sp.permit_id and
  18. (
  19. (AP.gallonsperminute IS not null and totalseconds * (AP.gallonsperminute/60) > AP.dailygallonlimit * CAST((CAST(ISNULL(A.grace_period,5) AS FLOAT)/100) AS FLOAT)+1) or
  20. (AP.gallonspercycle IS not null and totalcycles>AP.dailygallonlimit/AP.gallonspercycle * CAST((CAST(ISNULL(A.grace_period,5) AS FLOAT)/100) AS FLOAT)+1)))
  21. UNION ALL
  22. (SELECT CIT.citation_id,PD.*,A.agency_name,P.*,0 AS theAmount,p.permit_num,C.*, O.*,R.service_rep_name,L.*,S.system_name,M.manufacturer_name,
  23. Case when ((PD.gallonsperminute IS not null and totalseconds * (PD.gallonsperminute/60) > PD.dailygallonlimit) or (PD.gallonspercycle IS not null and totalcycles>PD.dailygallonlimit/PD.gallonspercycle)) then 4 else 0 end as violation_type,
  24. Case when (PD.gallonsperminute IS not null and totalseconds * (PD.gallonsperminute/60) > PD.dailygallonlimit) then totalseconds * (PD.gallonsperminute/60) - PD.dailygallonlimit else (totalcycles*PD.gallonspercycle) - PD.dailygallonlimit end as gallons_over
  25. FROM PumpDays PD,
  26. locations L
  27.       LEFT outer join Owners O ON O.owner_id=L.owner_id,
  28. systems S
  29.       LEFT outer join manufacturers M ON M.manufacturer_id=S.manufacturer_id
  30.       LEFT outer join service_contracts c ON C.system_id=S.system_id
  31.       LEFT outer join service_reps R ON C.service_rep_id=R.service_rep_id,
  32. system_permits sp, permits p
  33.       LEFT outer join citations cit ON cit.permit_num = p.permit_num,
  34. agencies A
  35. WHERE S.location_id=L.location_id and L.assoc_type=4 and L.assoc_id = 15008 AND PD.assoc_type=3 and  p.permit_num = PD.permit_num and sp.system_id = s.system_id and
  36. p.permit_id = sp.permit_id and p.agency_id = A.agency_id and
  37. (
  38. (PD.gallonsperminute IS not null and totalseconds * (PD.gallonsperminute/60) > PD.dailygallonlimit)
  39. or
  40. (PD.gallonspercycle IS not null and totalcycles>PD.dailygallonlimit/PD.gallonspercycle)
  41. ))
  42. ORDER BY R.service_rep_name ASC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement