Advertisement
Guest User

Untitled

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