Advertisement
Guest User

Untitled

a guest
May 29th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.08 KB | None | 0 0
  1. use workdb
  2. go
  3. set nocount on
  4. go
  5.  
  6. -- workdb.dbo.pclist MUST EXIST PRIOR TO THIS SCRIPT RUNNING -------------------------------
  7. --select count(*) from workdb.dbo.pcslist
  8. -- exec dbo.refresh_pcslist
  9. -- exec dbo.refreshChartA
  10.  
  11.  
  12. ------------------------ SERVO LOOP DILIGENCE QUERY ----------------------
  13. if object_id('tempdb.dbo.#servo') is not null
  14. drop table #servo
  15. go
  16. create table #servo (
  17. fldMeterType varchar(1) null,
  18. txModeRaw tinyint null,
  19. txModeText varchar(50) null,
  20. txRateMinutes int null,
  21. fsk varchar(5) null,
  22. Slot varchar(3) null,
  23. countOfBuddyRepeatEnabled int null,
  24. meterCount int null,
  25. avgRaw decimal(5,1) null,
  26. avgCommStatsSNR decimal(5,1) null,
  27. avgSNR_Corr decimal(5,1) null,
  28. percentStale24 decimal(5,1) null,
  29. percentStale48 decimal(5,1) null,
  30. countOfStale24 int null,
  31. countOfStale48 int null,
  32. region varchar(25) null,
  33. servoBucket int null,
  34. reportBucket varchar(25) null
  35. )
  36. go
  37.  
  38. ;with
  39. excludedMeters as
  40. (
  41. select distinct fldRepId
  42. from workdb.dbo.chartAMeters
  43. --where section in ('imA','imB','imC','imD','badMetroRIS0','badMetroKWH0','inPF-TMP-96','stale999')
  44. where section in ('notone')
  45. ),
  46. groupedResults as
  47. (
  48. select p.fldMeterType, p.region, p.txMode, p.txRateMinutes, p.using13fsk, p.isPCS,
  49. sum(case when p.rxMode<3 then 1 else 0 end) 'buddyRepeatEnabled',
  50. avg(p.commstatsSNR) 'avgCommStatsSNR', avg(p.avgSNR_Corr) 'avgSNRCorr',
  51. count(*) 'meterCount', avg(p.rawtput) 'avgRaw',
  52. sum(case when p.hoursReadStale>24 then 1 else 0 end) 'countOfStale24',
  53. sum(case when p.hoursReadStale>48 then 1 else 0 end) 'countOfStale48'
  54. from workdb.dbo.pcslist p
  55. left join excludedMeters em on (p.fldRepId=em.fldRepId)
  56. where em.fldRepId is null
  57. group by p.fldMeterType, p.region, p.txMode, p.txRateMinutes, p.using13fsk, p.isPCS
  58. )
  59. insert into #servo (meterCount, fldMeterType, region, txModeRaw, txModeText, txRateMinutes,
  60. fsk, Slot, countOfBuddyRepeatEnabled, avgCommStatsSNR, avgSNR_Corr, avgRaw,
  61. percentStale24, percentStale48, countOfStale24, countOfStale48)
  62. select meterCount, fldMeterType, region, txMode 'txModeRaw',
  63. CASE txMode
  64. WHEN 0 THEN 'Normal Mode'
  65. WHEN 1 THEN 'Message Pass'
  66. WHEN 2 THEN 'Boost Mode'
  67. WHEN 3 THEN 'Normal, 1/2 Baud Rate'
  68. WHEN 4 THEN 'mPass / Normal Mix (1:1)'
  69. WHEN 5 THEN 'mPass / Normal Mix (1:2)'
  70. WHEN 6 THEN 'mPass / Normal Mix (1:3)'
  71. WHEN 7 THEN 'mPass / Normal Mix (1:4)'
  72. WHEN 8 THEN 'mPass / Normal Mix (1:5)'
  73. WHEN 9 THEN 'mPass / Normal Mix (1:8)'
  74. WHEN 10 THEN 'mPass / Normal Mix (1:16)'
  75. WHEN 11 THEN 'Boost / Normal Mix (1:1)'
  76. WHEN 12 THEN 'Boost / Normal Mix (1:2)'
  77. WHEN 13 THEN 'Boost / Normal Mix (1:4)'
  78. WHEN 14 THEN 'Boost / Normal Mix (1:8)'
  79. WHEN 15 THEN 'Boost / Normal Mix (1:16)'
  80. else 'undefined' end 'txModeText',
  81. txRateMinutes,
  82. case when using13fsk=0 then '7fsk' else '13fsk' end 'fsk',
  83. case when isPCS=0 then 'MAS' else 'PCS' end 'Slot',
  84. buddyRepeatEnabled, cast(avgCommStatsSNR as decimal(5,1)) 'avgCommStatsSNR',
  85. cast(avgSNRCorr as decimal(5,1)) 'avgSNR_Corr', cast(avgRaw as decimal(5,1)) 'avgRaw',
  86. cast((cast(countOfStale24 as float)/meterCount)*100 as decimal(5,1)) 'percentStale24',
  87. cast((cast(countOfStale48 as float)/meterCount)*100 as decimal(5,1)) 'percentStale48',
  88. countOfStale24, countOfStale48
  89. from groupedResults
  90. --order by isPCS, fldMeterType, txMode, txRateMinutes
  91.  
  92. --select * from #servo
  93.  
  94. -- assign buckets per Servo Loop Document
  95. update #servo
  96. set servoBucket=1 where txModeRaw=0 and txRateMinutes=720 and fsk='13fsk' and Slot='MAS'
  97.  
  98. update #servo
  99. set servoBucket=2 where txModeRaw=0 and txRateMinutes=360 and fsk='13fsk' and Slot='MAS'
  100.  
  101. update #servo
  102. set servoBucket=3 where txModeRaw=0 and txRateMinutes=240 and fsk='7fsk' and Slot='MAS'
  103.  
  104. update #servo
  105. set servoBucket=4 where txModeRaw=4 and txRateMinutes=720 and fsk='7fsk' and Slot='MAS'
  106.  
  107. update #servo
  108. set servoBucket=5 where txModeRaw=7 and txRateMinutes=240 and fsk='7fsk' and Slot='MAS'
  109.  
  110. update #servo
  111. set servoBucket=6 where txModeRaw=7 and txRateMinutes=120 and fsk='7fsk' and Slot='MAS'
  112.  
  113. update #servo
  114. set servoBucket=7 where txModeRaw=7 and txRateMinutes=60 and fsk='7fsk' and Slot='MAS'
  115.  
  116. update #servo
  117. set servoBucket=50 where Slot='PCS'
  118.  
  119. update #servo
  120. set servoBucket=51 where servoBucket is null
  121.  
  122. update #servo
  123. set reportBucket = case when servoBucket in (1,2) then 'I & II'
  124. when servoBucket = 3 then 'III'
  125. when servoBucket = 4 then 'IV'
  126. when servoBucket = 5 then 'V'
  127. when servoBucket in (6,7) then 'VI & VII'
  128. when servoBucket = 50 then 'PCS'
  129. else 'Non-Compliant' end
  130.  
  131. -- combined report buckets per Britton
  132. declare @reportBuckets table (
  133. reportBucket varchar(25),
  134. fldMeterType varchar(1),
  135. region varchar(10),
  136. reportBucketSort tinyint,
  137. fldMeterTypeSort tinyint,
  138. regionSort tinyint
  139. )
  140.  
  141. insert into @reportBuckets (reportBucketSort, reportBucket, fldMeterTypeSort, fldMeterType, regionSort, region)
  142. select *
  143. from
  144. (
  145. select 1 as reportBucketSort,'I & II' as reportBucket
  146. union all
  147. select 2,'III'
  148. union all
  149. select 3,'IV'
  150. union all
  151. select 4,'V'
  152. union all
  153. select 5,'VI & VII'
  154. union all
  155. select 6,'PCS'
  156. union all
  157. select 7,'Non-Compliant'
  158. ) reportBuckets,
  159. (
  160. select 1 as fldMeterTypeSort,'E' as fldMeterType
  161. union all
  162. select 2,'A'
  163. ) meterTypes,
  164. (
  165. select 1 as regionSort, 'Central' as region
  166. union all
  167. select 3,'Southern'
  168. union all
  169. select 2,'Eastern'
  170. union all
  171. select 4,'Western'
  172. ) regions
  173.  
  174. --select * from @reportBuckets
  175.  
  176.  
  177.  
  178. /*
  179. ;with
  180. perServoBucketStats as
  181. (
  182. select fldMeterType, region, servoBucket, sum(meterCount) 'meterCount',
  183. sum(countOfStale24) 'countOfStale24', sum(countOfStale48) 'countOfStale48',
  184. avg(avgRaw) 'avgRaw', avg(avgCommStatsSNR) 'avgCommStatsSNR',
  185. avg(avgSNR_Corr) 'avgSNR_Corr', sum(countOfBuddyRepeatEnabled) 'countOfBuddyRepeatEnabled'
  186. from #servo
  187. group by fldMeterType, region, servoBucket
  188. )
  189. select s.fldMeterType, region, s.servoBucket, s.meterCount,
  190. cast(cast(countOfStale48 as float)/meterCount*100 as decimal(5,1)) 'percentStale48'
  191. from perServoBucketStats s
  192. */
  193.  
  194. ;with
  195. perReportBucketStats as
  196. (
  197. select fldMeterType, region, reportBucket, sum(meterCount) 'meterCount',
  198. sum(countOfStale24) 'countOfStale24', sum(countOfStale48) 'countOfStale48',
  199. avg(avgRaw) 'avgRaw', avg(avgCommStatsSNR) 'avgCommStatsSNR',
  200. avg(avgSNR_Corr) 'avgSNR_Corr', sum(countOfBuddyRepeatEnabled) 'countOfBuddyRepeatEnabled'
  201. from #servo
  202. group by fldMeterType, region, reportBucket
  203. ),
  204. regionalResults as
  205. (
  206. select s.fldMeterType, region, s.reportBucket, s.meterCount,
  207. cast((meterCount-cast(countOfStale48 as float))/meterCount*100 as decimal(5,1)) 'percentStaleLT48'
  208. from perReportBucketStats s
  209. where region is not null
  210. and region != ''
  211. )
  212. select rb.fldMeterType, rb.region, rb.reportBucket, isnull(rr.meterCount,0) 'meterCount',
  213. case when percentStaleLT48 is null then 'N/A' else cast(percentStaleLT48 as varchar(10)) end 'percentStaleLT48'
  214. from @reportBuckets rb
  215. left join regionalResults rr
  216. on rb.reportBucket=rr.reportBucket and rb.region=rr.region and rb.fldMeterType=rr.fldMeterType
  217. order by rb.fldMeterTypeSort, rb.regionSort, rb.reportBucketSort
  218.  
  219. -- pulled from chartA results
  220. -- see the dbo.refreshChartA procedure for definition of all these categories
  221. select 'MissingSetup' as ExceptionCategory, count(*) 'meterCount'
  222. from workdb.dbo.chartAMeters
  223. where section='missingSetup'
  224. union all
  225. select '999 Hours Stale', count(*)
  226. from workdb.dbo.chartAMeters
  227. where section='stale999'
  228. union all
  229. select 'TamperOrPFandStale96hr', count(*)
  230. from workdb.dbo.chartAMeters
  231. where section='inPF-TMP-96'
  232. union all
  233. select 'ChartA_HWIssues',
  234. (select count(distinct fldRepId)
  235. from workdb.dbo.chartAMeters
  236. where section in ('imA','imB','imC','imD','badMetroRIS0','badMetroKWH0'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement