Click to See Complete Forum and Search --> : performance turning of sql query


kvwarun
December 4th, 2007, 04:39 AM
hi friends,

i just want to know which is the best query to retrieve the value of smallest month specified in the table given below

SQL>SELECT * FROM TBL_PROVISION

Month Description Value
----------------------------------------------------
1 Pipe 2500.00
2 Pipe 2400.00
5 Pipe 2400.00
6 Pipe 1800.00
9 Pipe 1400.00

3 Valve 800.00
7 Valve 740.00
8 Valve 700.00
9 Valve 600.00

7 Cable 6000.00
8 Cable 4000.00


the output of query is

Month Description Value
----------------------------------------------------
1 Pipe 2500.00
3 Valve 800.00
7 Cable 6000.00

First Query

SELECT a.Month, a.Description, a.Value
FROM TBL_PROVISION a
WHERE Month IN (SELECT MIN(Month) FROM TBL_PROVISION b
WHERE b.Description = a.Description
)

Second Query

SELECT b.Month, b.Description, a.Value
FROM TBL_PROVISION a RIGHT OUTER JOIN
(
SELECT Description,MIN(Month)
FROM TBL_PROVISION GROUP BY Description
)
ON a.Description = b.Description and
a.Month = b.Month


or can we retrieve the same in a single query without a join or sub-query ?

olivthill
December 5th, 2007, 07:53 AM
I have seen your first query in a few programs written by coworkers, so it seems it is a usual solution.
I don't know if it can be improved.