You should have mentioned which query you used, but try:
SELECT contrato, MAX(ano) AS 'max_ano' FROM c GROUP BY contrato;
or:
SELECT contrato, MAX(ano) AS 'max_ano' FROM c GROUP BY contrato ORDER BY max_ano DESC LIMIT 1;
Bank created with:
CREATE TABLE "c" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"contrato" INTEGER NOT NULL,
"codigo" INTEGER NOT NULL,
"ano" INTEGER NOT NULL
);
INSERT INTO "c" VALUES(1,13,7800,2015);
INSERT INTO "c" VALUES(2,12,7800,2014);
INSERT INTO "c" VALUES(3,12,7800,2013);
INSERT INTO "c" VALUES(4,12,7801,2016);
INSERT INTO "c" VALUES(5,12,7801,2013);
INSERT INTO "c" VALUES(6,12,5102,2017);
Some tests:
sqlite> SELECT * FROM c;
1|13|7800|2015
2|12|7800|2014
3|12|7800|2013
4|12|7801|2016
5|12|7801|2013
6|12|5102|2017
sqlite> SELECT contrato, MAX(ano) AS 'max_ano' FROM c GROUP BY contrato;
12|2017
13|2015
sqlite> SELECT contrato, MAX(ano) AS 'max_ano' FROM c GROUP BY contrato ORDER BY max_ano DESC LIMIT 1;
12|2017
sqlite>
Could you give me more details than you’d like? It wasn’t very clear.
– Dherik
@Marconi, I changed the question. I was confused, now I think comparing the two examples is easier.
– Filipe Moraes
@Dherik edited the question, I think it’s now clearer.
– Filipe Moraes