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
Main Topics
Browse All Topics





by: TaygerPosted on 2007-03-23 at 17:13:22ID: 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