troubleshooting Question

SQL Query bad results

Avatar of carlino70
carlino70Flag for Argentina asked on
Oracle Database
6 Comments1 Solution316 ViewsLast Modified:
Hi experts, I need to solve the results of follows query:
SELECT utchora
           , pointnumber
           , siteid
           , MAX (DECODE (rn, 1, valor_inst)) valor_inst
           , MAX (DECODE (rn, 1, tlq_inst)) tlq_inst
           , AVG (valor_inst) valor_prom
           , case when max(bitand(tlq_inst, 168)) = 0                 
               then                                                    
             max(tlq_inst) keep (dense_rank first order by utctime)  
             else 8
             end as tlq_prom
           , MAX (DECODE (max0, 1, valor_inst)) valor_max
           , MAX (DECODE (max0, 1, tlq_inst)) tlq_max
           , MAX (DECODE (max0, 1, utctime)) utctime_max
           , MAX (DECODE (min0, 1, valor_inst)) valor_min
           , MAX (DECODE (min0, 1, tlq_inst)) tlq_min
           , MAX (DECODE (min0, 1, utctime)) utctime_min
        FROM (
  
        SELECT TRUNC ( utctime, 'HH24')  utchora 
             , utctime 
             , pointnumber
             , siteid
             , valor_inst
             , tlq_inst
             , valor_prom
             , tlq_prom
             , valor_max
             , tlq_max
             , utctime_max
             , valor_min
             , tlq_min
             , utctime_min
             , ROW_NUMBER () OVER (PARTITION BY pointnumber, TRUNC ( utctime, 'HH24') /*+ 1/24*/ ORDER BY utctime /*DESC*/) rn
             , AVG (valor_inst) OVER (PARTITION BY pointnumber ORDER BY utctime DESC) av0
             , ROW_NUMBER () OVER (PARTITION BY pointnumber, TRUNC ( utctime, 'HH24') /*+ 1/24*/ ORDER BY valor_inst DESC) max0
             , ROW_NUMBER () OVER (PARTITION BY pointnumber, TRUNC ( utctime, 'HH24') /*+ 1/24*/ ORDER BY valor_inst) min0
        FROM a_5min_023
       WHERE TRUNC ( utctime, 'HH24') >= to_date('11/08/2014 03:00:00','dd/mm/yyyy hh24:mi:ss') 
       AND TRUNC ( utctime, 'HH24') < to_date('11/08/2014 04:00:00','dd/mm/yyyy hh24:mi:ss') 
       AND POINTNUMBER = 230001
       
       )        
       GROUP BY pointnumber, utchora, siteid ;
Results:
UTCHORA|POINTNUMBER|SITEID|VALOR_INST|TLQ_INST|VALOR_PROM|TLQ_PROM|VALOR_MAX|TLQ_MAX|UTCTIME_MAX|VALOR_MIN|TLQ_MIN|UTCTIME_MIN
11/08/2014 03:00:00 a.m.|230001||0|32|0|8||||0|32|11/08/2014 03:00:00 a.m.
11/08/2014 03:00:00 a.m.|230001|0|||6|8|11|8|11/08/2014 03:55:00 a.m.|||
The problems are:
1 - Returns 2(two) rows with results. I need to get just one row.
2 - I need obtain the average of the segment with:
 AVG (valor_inst) valor_prom 
But never is calculated.
Could you help me to found the solutions?
All the scripts, attached.
Thankyou in advanced
Regards
table-023.sql
inserts.sql
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros