Help with a logic inside WHERE or ORDER BY I Have this query :

;with cte as (
Select tConc.Faturamento
     , tFat.TotalFatura
     , tCod.Sequencial
     , tCodDet.CdCodigoBarras
     , tCodDet.Linha
     , ValorRecebido
  From Sistema.Conciliacao_Faturamento_x_Arrecadacao as tConc
 Inner Join Arquivo.CodigoBarras_Detalhe as tCodDet
    On tConc.CodigoBarras = tCodDet.CdCodigoBarras_Detalhe
 Inner Join Arquivo.CodigoBarras as tCod
    On tCodDet.CdCodigoBarras = tCod.CdCodigoBarras
 Inner Join Arquivo.Faturamento as tFat
    On tConc.Faturamento = tFat.CdFaturamento
 Where Faturamento In(Select Faturamento
                        From Sistema.Conciliacao_Faturamento_x_Arrecadacao
                       Group By Faturamento
                      Having Count(CodigoBarras) &gt; 01))


And the result is this one:

| Faturamento | TotalFatura | Sequencial | CdCodigoBarras | Linha | ValorRecebido |
|-------------|-------------|------------|----------------|-------|---------------|
| 2458874     | 320,90      | 5783       | 141            | 18393 | 320,90        |
| 2458874     | 320,90      | 5783       | 141            | 18394 | 320,90        |
| 3283899     | 840,50      | 5783       | 141            | 27140 | 840,50        |
| 3283899     | 840,50      | 4397       | 142            | 4010  | 840,50        |
| 3901364     | 89,44       | 21442      | 139            | 23595 | 89,44         |


Long story short, if you notice, there are some equal "faturamento".

What I need is a distinct of:

1) If Faturamento is the same, we need the biggest sequencial. Ok with this, I can order by 3.

2) If faturamento and sequencial is the same, I need the biggest cdCodigoBarras. Ok, again, I can do it with Order by 3,4.

3) If everything is the same, but valorRecebidois the same, I need to get the biggest Linha. but if ValorRecebido is different ( and faturamento, sequencia, cdcodigobarras is the same ), I need the biggest ValorRecebido.

Is this something that devs should do in the source code? I really can't think how I can do it with a Query.

I've spend some time tryint to do something with count, but even thinking I'm in the right way, I can past this when vlaorRecebidois the same, get biggest Linha, or if it's different, the biggest vlaorRecebido.
I think you should be able to get what you want with ROW_NUMBER().  Please see the below example.

;with cte as (
Select tConc.Faturamento
     , tFat.TotalFatura
     , tCod.Sequencial
     , tCodDet.CdCodigoBarras
     , tCodDet.Linha
     , ValorRecebido
     , RN = ROW_NUMBER() OVER (PARTITION BY  tConc.Faturamento, tCod.Sequencial, tCodDet.CdCodigoBarras, ValorRecebido
                                ORDER BY tCodDet.Linha DESC, tCodDet.CdCodigoBarras DESC, tCod.Sequencial DESC)
  From Sistema.Conciliacao_Faturamento_x_Arrecadacao as tConc
 Inner Join Arquivo.CodigoBarras_Detalhe as tCodDet
    On tConc.CodigoBarras = tCodDet.CdCodigoBarras_Detalhe
 Inner Join Arquivo.CodigoBarras as tCod
    On tCodDet.CdCodigoBarras = tCod.CdCodigoBarras
 Inner Join Arquivo.Faturamento as tFat
    On tConc.Faturamento = tFat.CdFaturamento
 Where Faturamento In(Select Faturamento
                        From Sistema.Conciliacao_Faturamento_x_Arrecadacao
                       Group By Faturamento
                      Having Count(CodigoBarras) &gt; 01))
)
SELECT * FROM cte WHERE RN = 1


You may want to change or play with the PARTITION BY section as well as the ORDER BY to get what you want.  Basically, the PARTITION BY is what you want to be the business keys.  And I wasn't sure if it was just the Faturamento or Faturamento + Sequencial + ... or not.  Order by is if (given the same partition), what order should they appear in.