select multiple columns group by 1 column

Asked

Viewed 763 times

1

I have a table with 300 thousand records and within it there are several different fields, I would like to take the line that contains the largest OUTGOING DATE calculated for each CPF_1

what I have:

ID          NUMERO                    DATAEMISSAOAPURADA      COD                     NOMEMUNICIPIORESIDENCIA                            UFRESIDENCIA CPF_1         CPF_2         FONTE               NOME                                                                         DATA                        DATA         
----------- ------------------------- ----------------------- ----------------------- -------------------------------------------------- ------------ ------------- ------------- ------------------- ---------------------------------------------------------------------------- --------------------------- ---------------------------
12345       0000000000000009116092003 2007-11-19 00:00:00     4200051                 Abdon Batista                                      SC           123           321           TB1                 To***********                                                                2012-10-28 00:00:00         NULL
78999       0000000000080697220782003 2007-11-09 00:00:00     4200051                 Abdon Batista                                      SC           123           321           TB1                 To***********                                                                2012-10-28 00:00:00         NULL
12347       C000000000000000000821022 2006-04-12 00:00:00     2302404                 Boa Viagem                                         CE           356           978           TB1                 JO**********************                                                     2011-09-27 00:00:00         NULL
12399       C489895345345350000881240 2007-06-11 00:00:00     2306405                 Itapipoca                                          CE           876           093           TB1                 HA*************************                                                  2009-11-16 00:00:00         NULL

Expected result:

ID          NUMERO                    DATAEMISSAOAPURADA      COD                     NOMEMUNICIPIORESIDENCIA                            UFRESIDENCIA CPF_1         CPF_2         FONTE               NOME                                                                         DATA                        DATA         
----------- ------------------------- ----------------------- ----------------------- -------------------------------------------------- ------------ ------------- ------------- ------------------- ---------------------------------------------------------------------------- --------------------------- ---------------------------
12345       0000000000000009116092003 2007-11-19 00:00:00     4200051                 Abdon Batista                                      SC           123           321           TB1                 To***********                                                                2012-10-28 00:00:00         NULL
12347       C000000000000000000821022 2006-04-12 00:00:00     2302404                 Boa Viagem                                         CE           356           978           TB1                 JO**********************                                                     2011-09-27 00:00:00         NULL
12399       C489895345345350000881240 2007-06-11 00:00:00     2306405                 Itapipoca                                          CE           876           093           TB1                 HA*************************                                                  2009-11-16 00:00:00         NULL
  • https://forum.imasters.com.br/topic/512214-selecionando-item-max/? do=findComment&comment=2030107 see if it helps , subselect with MAX

  • What version of SQL Server?

2 answers

1

The proposed problem can be solved as follows. It is still possible to filter the select that obtains the Max(DATE Issued Cleared)

SELECT
ID          
,NUMERO 
,DATAEMISSAOAPURADA      
,COD                     
,NOMEMUNICIPIORESIDENCIA                            
,UFRESIDENCIA 
,CPF_1         
,CPF_2         
,FONTE               
,NOME
,DATA                        
FROM tabela A
where ID in (select ID from tabela B where DATAEMISSAOAPURADA = (SELECT MAX(DATAEMISSAOAPURADA) from tabela))
  • the problem is that it only returns me the longest date of the table, I would like to get the biggest date for each existing CPF because the table has multiple records for the same CPF

  • In this case you can adapt the select done on this link by performing the queries and ordering by the CPF https://stackoverflow.com/questions/45643187/query-help-matching-stage-and-returning-the-most-recent-one/45643653

0


Evaluate

-- código #1
with MaisRecente as (
SELECT colunas,
       Seq= row_number() over (partition by CPF_1 order by DATAEMISSAOAPURADA desc)
  from tabela
)
SELECT colunas
  from MaisRecente
  where Seq = 1;
  • 1

    José you are the wizard of SQL the second time you saved me, Thank you guy!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.