Maximum SQL data

1

I have the following table T_PGTOS_ADESAO :

Adesão  | Tipo Adesão | Data Vencimento Boleto | Data Pgto Boleto 
12247   | Cartão      |    20-03-2018          | 03-03-2018
38497   | Boleto      |    15-05-2018          | 10-05-2018
48289   | Cartão      |    20-05-2018          | 02-01-1900 
89497   | Saque Compl.|    23-04-2018          | 02-01-1900
90849   | Boleto      |    15-05-2018          | 07-05-2018

I would like to create a column with the maximum date of the Ticket Date (which in the given example is the date 10-05-2018) for all lines, like this:

Adesão  | Tipo Adesão | Data Vencimento Boleto |Data Pgto Boleto| DT_UTLIMO_PGTO
12247   | Cartão      |    20-03-2018          | 03-03-2018     | 10-05-2018
38497   | Boleto      |    15-05-2018          | 10-05-2018     | 10-05-2018
48289   | Cartão      |    20-05-2018          | 02-01-1900     | 10-05-2018
89497   | Saque Compl.|    23-04-2018          | 02-01-1900     | 10-05-2018
90849   | Boleto      |    15-05-2018          | 07-05-2018     | 10-05-2018

however when I make the query

SELECT 
T_PGTOS_ADESAO.*,
MAX(Data Pgto Boleto) as DT_UTLIMO_PGTO
from T_PGTOS_ADESAO
GROUP BY 
Adesão
,[Tipo Adesão]
,[Data Vencimento Boleto]
,[Data Pgto Boleto] 

It returns the value of the Due Date Ticket for each line.

Could you please help me?

    
asked by anonymous 14.05.2018 / 22:52

1 answer

1

Aggregating functions such as MAX() are applied in the context of GROUP BY , and since you are grouping by all fields, MAX () returns the largest date of each record.

Try with a subselect:

SELECT 
  T_PGTOS_ADESAO.*,
  (select MAX(Data Pgto Boleto) from T_PGTOS_ADESAO) as DT_UTLIMO_PGTO
from T_PGTOS_ADESAO;

Thus, MAX () is applied to the implicit group context of all records in the table.

    
14.05.2018 / 23:03