?
Solved

selecting a max within a max oracle query.

Posted on 2007-03-23
5
Medium Priority
?
9,522 Views
Last Modified: 2013-12-19
Using oracle 8.
I am using the following query in an oracle function but I want to know if it could be written better. I have a table "my table" which has two sequence numbers "sequence_no" and "seq_no." I want to find the maximum "sequence_no" for a particular key "my_key". But when I find this max sequence_no, I have to find the maximum seq_no within that maximum sequence_no. HOpe I didn't confuse you. Help and Thanks

SELECT decode(my_value,'Y','Fuel')
                  INTO ls_temp
                  FROM  my_table
                  WHERE
                        util_name = 'h$_fuel'
                        AND my_value = 'Y'
                        AND key = my_key
                        AND sequence_no =
                              (SELECT max(sequence_no)
                               FROM my_table
                               WHERE util_name = 'h$_fuel' AND key = my_key
                               )
                        AND seq_no =
                              (SELECT max(seq_no)
                              FROM my_table
                              WHERE util_name = 'h$_fuel' AND key = my_key
                              AND sequence_no =
                                    (SELECT max(sequence_no)
                                          FROM my_table
                                          WHERE util_name = 'h$_fuel' AND key = my_key
                                    )
                              );

0
Comment
Question by:Sanmarie
[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
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
Tayger earned 1600 total points
ID: 18783877
Hello
I dont know whats the idea behind your select statement. From perfomance view you access the same table twice by full table scan. This is not a biggy if the table is small.
The other thing that confuses is me is the way you accessing the data by max(seq). Looks like you want to kinda get the "the latest" record of a data group (?). How about a timestamp instead of a sequence, even tough this wont solve your problem. Maybe there is something wrong in your DB design.

In general and not tested, but how about that way:
SELECT decode(my_value,'Y','Fuel')
                  INTO ls_temp
                  FROM  my_table
                  WHERE util_name = 'h$_fuel'
                        AND my_value = 'Y'
                        AND key = a.my_key  -- key = function input parameter?
                        AND rownum = 1
                        ORDER BY sequence_no DESC, seq_no DESC

Not tested but it should return you the first found my_value (rownum = 1) orderded first by biggest sequence_no, then biggest seq_no. Its also guaranteed you only get 1 record back (if found), instead of error message: too many records.
If this wont help could you let me know whats the main idea of the two sequences in that table and if sequence_no and/or sequence:no can have the same number (sequence) more than once?
Hope I could help
Tayger
0
 
LVL 17

Assisted Solution

by:NicksonKoh
NicksonKoh earned 400 total points
ID: 18790290
Hi,

You can simplify the SQL as follows :

SELECT DECODE (my_value, 'Y', 'Fuel')
  INTO ls_temp
  FROM my_table a,
       (SELECT my_key KEY, MAX (sequence_no)
          FROM my_table
         WHERE util_name = 'h$_fuel' AND KEY = my_key) b
 WHERE (a.KEY = b.KEY AND a.sequence_no = b.sequence_no)
   AND a.util_name = 'h$_fuel'
   AND a.my_value = 'Y'
   AND a.KEY = my_key

Check whether the result is correct because I am a little confused by your my_value = 'Y' condition which I try to just follow as what u did in your original SQL.

cheers
Nickson
0
 

Author Comment

by:Sanmarie
ID: 18792975
Hi Tayger and NicksonKoh,

Sorry  I took so long to get back to you both. Tayger, you are right I access the same table twice. I have two columns, sequence_no and seq_no. I want to get the maximum seq_no in the maximum sequence_no group. Get it? I agree that the database design is bad but unfortunately, that is the design that I have to work with.

I will try both your suggestions and get back to you today.

San
0
 

Author Comment

by:Sanmarie
ID: 18794034
Tayger,
Your query worked perfectly with a slight change. Thank you very much.

NicksonKoh thanks for helping out too but I will go with Tayger's solution.

San
0
 
LVL 2

Expert Comment

by:Tayger
ID: 18794222
Your welcome, glad I coud help.

Regards
Tayger
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
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.
Suggested Courses

752 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