sql - SELECT for recent data only

2
  

I think my title is not a question.

The application I develop inserts data pertaining to the last backup into a table once a month. This table contains the backup instance code, the given insert and the number of bytes.

The problem is that I do not know a command or a way to do a SQL SELECT that only brings the last results of each backup.

Tenho isso
backup   bytes    data
'x'        30  '21/12/14'
'x2'     1200  '14/10/14'
'x2'     3500  '14/12/14'
'x2'     4800  '14/01/15'


Mas preciso somente dos últimos
backup   bytes    data
'x'        30  '21/12/14'
'x2'     4800  '14/01/15'

I'm pretty much in SQL, but any information is already of great value.

    
asked by anonymous 05.02.2015 / 15:55

4 answers

3

I think something like this solves your problem:

SELECT backup, bytes, MAX(data) FROM table_name GROUP BY backup
    
05.02.2015 / 16:01
2

Hi, come on:

Is the 'date' field what type? If it is date you can sort it by decreasing (today is greater than yesterday and yesterday is greater than a month ago) and to limit your search you can use the LIMIT function of SQL, it would look like this: / p>

SELECT * FROM backup ORDER BY data DESC LIMIT 2;
    
05.02.2015 / 16:13
1

I've seen your question again, and the scenario below does not fit what you need ... Luis's answer will solve your problem better.

I do not use Postgresql, but I think it looks something like this:

SELECT * FROM sua_tabela ORDER BY data DESC LIMIT 2;

Change the value of Limit by the amount of results you would like it to display;

05.02.2015 / 15:58
1
select distinct on (backup) *
from t
order by backup, data desc
    
13.02.2015 / 18:56