Search code examples
oracleoracle11goracle10goracle-sqldevelopersqlplus

Update with counter with group by


I have a table T1 and which looks like this

DATUM   ID  NAME    ROW_COUNT

2/14/2021   101 ALEX    
2/14/2021   100 SHAUN   
2/14/2021   102 SCOTT   
2/15/2021   100 SHAUN   
2/15/2021   101 ALEX    
2/15/2021   102 SCOTT   
2/16/2021   100 SHAUN   
2/16/2021   101 ALEX    
2/16/2021   102 SCOTT   

which have column datum , ID, Name, Row_Count I want update Counter like this

DATUM   ID  NAME    ROW_COUNT

2/14/2021   101 ALEX    1
2/14/2021   100 SHAUN   1
2/14/2021   102 SCOTT   1
2/15/2021   100 SHAUN   2
2/15/2021   101 ALEX    2
2/15/2021   102 SCOTT   2
2/16/2021   100 SHAUN   3
2/16/2021   101 ALEX    3
2/16/2021   102 SCOTT   3

What I tried like this

UPDATE DAILY_PRODUCTION
SET DOC_ID=DOC_ID+1
GROUP BY DATUM;

But not worked any one knows please.


Solution

  • I suggest not doing this update, but rather using the following select with DENSE_RANK:

    SELECT DATUM, ID, NAME, DENSE_RANK() OVER (ORDER BY DATUM) ROW_COUNT
    FROM DAILY_PRODUCTION
    ORDER BY DATUM, ID;
    

    The reason for not wanting to maintain the ROW_COUNT column is that it might be derived data. As such, you could be forced to run this update multiple times as the data changes.