2009-11-04 13 views
6

So che il titolo non suona molto descrittivo, ma è il meglio che potevo pensare:più valori massimi in una query

Ho questa tabella

 
ID  BDATE  VALUE 
28911 14/4/2009 44820 
28911 17/4/2009 32240 
28911 20/4/2009 30550 
28911 22/4/2009 4422587,5 
28911 23/4/2009 4441659 
28911 24/4/2009 7749594,67 
38537 17/4/2009 58280 
38537 20/4/2009 137240 
38537 22/4/2009 81098692 
38605 14/4/2009 2722368 
38605 20/4/2009 5600 
38605 22/4/2009 1625400 
38605 23/4/2009 6936575 

che in realtà è molto query complessa incapsulata in una vista, ma non è dell'argomento ora.

Mi piacerebbe avere per ogni ID, la riga contenente il BDate più alto. In questo esempio, questo sarebbe il risultato.

 
ID  BDATE  VALUE 
28911 24/4/2009 7749594,67 
38537 22/4/2009 81098692 
38605 23/4/2009 6936575 

Ho già provato

select id, max(bdate), value from myview group by id, value 

ma poi restituisce tutte le righe, perché per ogni valore collumn è diverso. Questa query è progettata in Oracle v10 e sono idonea a utilizzare solo query selezionate e non a creare procedure.

+0

grazie per tutto il risponde ragazzi. – kurast

+0

diablo II - banchine kurast? – CheeseConQueso

+0

Sì, il mio nome è di diablo II. i moli del kurast. La parola Kurast pronunciata suona così impressionante per me, che non potrei sopportare di usarlo. – kurast

risposta

10
select id, bdate, value 
from myview 
where (id, bdate) in 
    (select id, max(bdate) 
    from myview group by id) 
/
+0

Come aggiungerebbe un'altra condizione a quella query, ad esempio "Valore AND> 500"? – kerosene

0
select a.* from myview a, (select id, max(bdate) from myview group by id) b 
where a.id = b.id and a.bdate = b.bdate 
+0

non restituisce il valore collumn, che voglio risolto – kurast

+0

. Naturalmente, non testato. –

0
SELECT id, bdate, value FROM myview 
WHERE (id, bdate) IN (SELECT id, MAX(bdate) FROM myview GROUP BY id) 

(non testata ... non ho Oracle disponibili in questo momento ...)

2

È possibile utilizzare Analytics:

select 
     id, bdate, value 
    from 
     (
     select 
      id, bdate, value, max(bdate) over (partition by id) max_bdate 
     from 
      myview 
    ) 
    where 
     bdate = max_bdate 
2

È possibile utilizzare un INNER JOIN per filtrare solo le righe massime:

select t.* 
from YourTable t 
inner join (
    select id, max(bdate) as maxbdate 
    from YourTable 
    group by id 
) filter 
    on t.id = filter.id 
    and t.bdate = filter.maxbdate 

Questo stampa:

id  bdate  value 
38605 2009-04-23 6936575 
38537 2009-04-22 81098692 
28911 2009-04-24 7749594.67 

Si noti che questo tornerà più righe per un id che ha più valori con lo stesso bdata.

8

è possibile utilizzare il MAX...KEEP(DENSE_RANK FIRST...) costrutto:

SQL> SELECT ID, 
    2   MAX(bdate) bdate, 
    3   MAX(VALUE) KEEP(DENSE_RANK FIRST ORDER BY bdate DESC) VALUE 
    4 FROM DATA 
    5 GROUP BY ID; 

     ID BDATE   VALUE 
---------- ----------- ---------- 
    28911 24/04/2009 7749594,67 
    38537 22/04/2009 81098692 
    38605 23/04/2009  6936575 

Questo sarà il più efficiente metodo di analisi suggerito da Majkel (nessuna auto-join, un singolo passaggio sui dati)