Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

So I need a little help. It is currently working but I might have over complicated it. So maybe someone has a better idea. Now let me try and explain it.

I am trying this query, and it currently works but I am not sure if it is the best way to do this.

I have a table that reads every minute, but the data on one meter only changes once every 60 minutes. It will then repeat that value for 60 minutes before changing again.

I need to take the sum of these values and add them up for the day. But if I grab it by the hour, I will get duplicates. So what I decided to do was count the number of records, and if is over 30 minutes use that record.

Currently this is working good and is not a big issue. But I am curious on if there is an easier way to do this.

WITH A AS 
(SELECT DISTINCT 
  CONVERT(CHAR(12), TIME, 101) AS [ DAY ],
  DatePart (HOUR, TIME) AS [ HOUR ],
  [ B100 Pulse LAST HOUR ],
  [ RESET Counter ],
  [ B700 Pulse LAST HOUR ],
  [ B800 Pulse LAST HOUR ],
  [ B1300 Pulse LAST HOUR ],
  [ B900 Pulse LAST HOUR ],
  COUNT([ RESET Counter ]) AS Records 
FROM
  (SELECT 
    TOP (100) PERCENT KPI $.[ B100 Pulse LAST HOUR ],
    Main $.Time,
    KPI $.[ RESET Counter ],
    KPI $.[ B700 Pulse LAST HOUR ],
    KPI $.[ B800 Pulse LAST HOUR ],
    KPI $.[ B900 Pulse LAST HOUR ],
    KPI $.[ B1300 Pulse LAST HOUR ] 
  FROM
    Main $ 
    INNER JOIN KPI $ 
      ON Main $.[LOOP INDEX ] = KPI $.[LOOP INDEX ] 
  ORDER BY Main $.Time DESC) AS t1 
GROUP BY CONVERT(CHAR(12), TIME, 101),
  DatePart (HOUR, TIME),
  [ B100 Pulse LAST HOUR ],
  [ RESET Counter ],
  [ B700 Pulse LAST HOUR ],
  [ B800 Pulse LAST HOUR ],
  [ B1300 Pulse LAST HOUR ],
  [ B900 Pulse LAST HOUR ]) 
SELECT 
  Top 120 [ DAY ],
  COUNT(*) AS [ # of Records], SUM([B100 Pulse Last Hour]) * 100 AS [Building 100 Gal Discharged], SUM([B700 Pulse Last Hour]) * 100 AS [Building 700 Gal Discharged], (SUM([B800 Pulse Last Hour])
  - SUM([ B1300 Pulse LAST HOUR ])) * 100 AS [ Building 800 Gal Discharged ],
  SUM([ B900 Pulse LAST HOUR ]) * 100 AS [ Building 900 Gal Discharged ],
  SUM([ B1300 Pulse LAST HOUR ]) * 100 AS [ Building 1300 Gal Discharged ],
  SUM(
    (
      [ B100 Pulse LAST HOUR ] + [ B700 Pulse LAST HOUR ] + [ B800 Pulse LAST HOUR ] + [ B900 Pulse LAST HOUR ]
    ) * 100
  ) AS [ Daily Total ],
  [ STARTING LEVEL ] AS [ LEVEL IN B1800 AT Midnight ],
  [ Difference ] 
FROM
  (SELECT 
    *,
    ROW_NUMBER () Over (
      PARTITION BY Records 
  ORDER BY Records ASC
  ) AS ROW 
  FROM
    A 
    INNER JOIN B1800_Totals 
      ON A.[ DAY ] = B1800_Totals.[ DATE ]) AS A2 
WHERE Records >= 30 
  AND ([ DAY ] = @date) 
GROUP BY DAY,
  [ STARTING LEVEL ],
  [ Difference ] 
ORDER BY DAY ASC 
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.