?
Solved

Finding corresponding column of a row that has a maximum value

Posted on 2009-05-12
6
Medium Priority
?
705 Views
Last Modified: 2012-05-06
I have 2 tables: a daily table and an hourly table. The daily table has the following columns:
avg_30day float;
daily_metric;
max_hour_value float;
max_hour date;
timestamp; /* timestamp is always = trunc(timestamp) */

while the hourly table has:
metric_value float;
timestamp date; /* timestamp is always = trunc(timestamp, 'hh') */

I'm trying to create an update statement that will update the daily table as follows:
avg_30day = average of previous 30 days' daily_metric
max_hour_value = maximum metric_value in hour table for that day
max_hour = corresponding timestamp of the row that has maximum metric_value

The problem with my statement below is that I can't find the correct SQL that will return the corresponding timestamp from the daily table that has the maximum metric_value for that day. I get the ORA-00937: not a single-group group function. If I remove the busy_hour column and h1.timestamp, it works.
update daily d2
set (avg_30day, busy_hour_value, busy_hour) =
(select avg(d1.daily_metric), max(h1.metric_value), h1.timestamp
from daily d1 left outer join hourly h1 on d1.id = h1.id and d1.timestamp = trunc (h1.timestamp)
where d1.id = d2.id and d1.timestamp > (d2.timestamp - 29) and d1.timestamp <= d2.timestamp)

Open in new window

0
Comment
Question by:jdymahal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Expert Comment

by:bull_rider
ID: 24363940
Just run the select statement if it retrieves the correct values. I am suspecting the group by clause not being included in the select statement. It should contain group by h1.timestamp as avg() and max() are group functions.

Hope this helps. Let me know if you have any more questions.
0
 
LVL 20

Accepted Solution

by:
gatorvip earned 375 total points
ID: 24367357
You can accomplish this via analytic functions, see below for an example that you should be able to adapt to your 30 day requirement. If this is not enough, post some sample values with desired result.

What happens if you happen to have two different timestamps that have the same max daily value for your metric? If you have a specific criteria (for example, the first one throughout the day) then add this requirement to 'order by metric_value'
select avg(d1.daily_metric) over() avg_30_days,
   h1.metric_value,
   h1.ts
from daily d1
  left outer join (select h.*, row_number() over(partition by trunc(h.ts) order by metric_value desc) max_hour from hourly h) h1
    on d1.ts=trunc(h1.ts)
where h1.max_hour=1           

Open in new window

0
 
LVL 11

Expert Comment

by:Andytw
ID: 24367821
Try the statement below.  It uses the FIRST function which allows you to get a value from the first row of a sorted group, where the value is not what you're sorting on.  See ora docs for more info ...

Oracle® Database SQL Reference
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions056.htm#i1000901


update daily d2
set (avg_30day, max_hour_value, max_hour) =
( select avg(d1.daily_metric), 
         max(h1.metric_value),
	 max(h1.date_time) keep (dense_rank first order by h1.metric_value desc)
  from daily d1 left outer join hourly h1 on (d1.id = h1.id and d1.date_time = trunc (h1.date_time) )
  where d1.id = d2.id 
    and d1.date_time > (d2.date_time - 29) 
    and d1.date_time <= d2.date_time)

Open in new window

0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 1

Author Closing Comment

by:jdymahal
ID: 31580475
Thank you! I've never heard of analytical functions before (hence my slow response since I spent the last day studying it), but it was exactly what i needed to solve my problem. I had to split the solution into two update statements for performance reasons. here's my final solution:

update daily d2 set (avg_30day) =
(select avg(d1.daily_metric) over (partition by id)
from daily d1
where d1.id = d2.id and d1.timestamp > (d2.timestamp - 29) and d1.timestamp <= d2.timestamp)

the second update is similar to what you specified in your solution.
0
 
LVL 20

Expert Comment

by:gatorvip
ID: 24384062
Have you tried Andytw's solution? I believe it might actually help solve your problem moreso than my answer did. I would suggest splitting the points.
0
 
LVL 1

Author Comment

by:jdymahal
ID: 24386686
Yes, I did, but using row_number() and then picking out the row that has row_number() = 1 in the outer select as in your solution was better. Andytw's solution using "keep first" did find the correct timestamp, but it returned an "ora-01427: single-row subquery returns more than one row". In other words, I would have needed to add a row_number() column in the inner select and pick out where row_number() = 1 in an outer select to make it work in the update... making the "keep first" clause superfluous.

I hope I explained myself clear here.. Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question