SQL SEQUENTIAL NUMBER WITH GROUP , THE OPPOSITE OF PARTITION BY

daniele franzini 21 Reputation points
2021-10-27T16:30:08.673+00:00

Hi,
I need to create a Progressive Number on different group in a Select on SQL, the number should be progressive with different group , but the same inside the same group.
Here is the code i use now with the result :

select   
ROW_NUMBER()OVER(PARTITION BY  SD.SALEDOCID ORDER BY sd.DOCNO ASC) AS ProgressivoDocumento,  
sd.SaleDocId ,  
cs.custsupp ,  
'CINV' AS TipoDocument,  
sd.DocNo As NumeroDocumento,  
sd.DocumentDate as DataEmissioneDocumento  
from MA_SaleDoc SD   
JOIN MA_PyblsRcvbls PY on sd.DocNo = py.DocNo  
right join MA_CustSupp CS on cs.CustSupp = sd.CustSupp  
join MA_SaleDocSummary sds on sd.SaleDocId = sds.SaleDocId   
join MA_SaleDocPymtSched sdp on sdp.SaleDocId = sd.SaleDocId  
  
where   
( sd.DocumentType = '3407874'or sd.DocumentType = '3407875') and py.Settled = 0  and sd.DocumentDate >='20210101'  and py.DocumentDate >='20210101' AND CS.CustSuppType = '3211264'  

And this is what i find
144284-cattura.png

But i need exactly the opposite like this :
144301-cattura1.png

Can someone help me on how to solve this?
Thank you very much

Developer technologies Transact-SQL
SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2021-10-27T16:50:38.047+00:00

    Try 'dense_rank() over (order by SaleDocId)'.


1 additional answer

Sort by: Most helpful
  1. EchoLiu-MSFT 14,621 Reputation points
    2021-10-28T06:34:15.237+00:00

    Hi @daniele franzini

    Welcome to the microsoft TSQL Q&A forum!

    The DENSE_RANK() function mentioned by Viorel-1 can solve your problem. Please refer to his answer. For more details, please refer to:
    DENSE_RANK (Transact-SQL)

    Regards
    Echo

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.