PDA

View Full Version : [sql] estrazione record con MAX


hello
28-05-2017, 14:53
Ciao,
ho in tabella PROVA questo:

ID SCHEDA STATUS ACTION
1 SCHEDA_01 A 1
2 SCHEDA_01 B 2
3 SCHEDA_01 C 3
4 SCHEDA_01 D 4
5 SCHEDA_02 A 1
6 SCHEDA_02 B 2
7 SCHEDA_02 C 3
8 SCHEDA_02 D 4
9 SCHEDA_02 E 5

come posso fare per estrarre i soli record con MAX(ACTION), cioč quelli con ID=4, 9

sono riuscito per un singolo record:
select * from PROVA where SCHEDA='SCHEDA_01' and ACTION = (select max(ACTION) from PROVA where SCHEDA='SCHEDA_01');

ma non so come fare per estrarre tutti i record insieme

Potete per favore aiutarmi ?

Grazie :help: :help: :help:

vittorio130881
29-05-2017, 08:48
Ciao,
ho in tabella PROVA questo:

ID SCHEDA STATUS ACTION
1 SCHEDA_01 A 1
2 SCHEDA_01 B 2
3 SCHEDA_01 C 3
4 SCHEDA_01 D 4
5 SCHEDA_02 A 1
6 SCHEDA_02 B 2
7 SCHEDA_02 C 3
8 SCHEDA_02 D 4
9 SCHEDA_02 E 5

come posso fare per estrarre i soli record con MAX(ACTION), cioč quelli con ID=4, 9

sono riuscito per un singolo record:
select * from PROVA where SCHEDA='SCHEDA_01' and ACTION = (select max(ACTION) from PROVA where SCHEDA='SCHEDA_01');

ma non so come fare per estrarre tutti i record insieme

Potete per favore aiutarmi ?

Grazie :help: :help: :help:



SELECT *
FROM tabella a where a.action =(select max(action) from tabella b where a.scheda=b.scheda) ;

devi correlare le schede
;)

vittorio130881
29-05-2017, 14:03
Dipende cosa deve estrarre; se non gli serve correlare le schede, ma estrarre tutte quelle con MAX(ACTION), non ha senso fare autojoin, altrimenti č giusto come dici tu :)

altra cosa da dire č che le autojoin sono belle pesanti......se il db č pesante adios........

hello
29-05-2017, 22:35
ciao,
devo filtrare anche in base alla scheda e quindi va bene la soluzione proposta da vittorio130881, il db č oracle.

Grazie mille a entrambi per le risposte :)