I have a table in SQL Server where I have a unique SiteNumber for every unique location_id. What I'm trying to do is increment each sitenumber by 1 if the location_id is different.
For example:
SITENUM LOCATION_ID
1234 8801
4567 8802
8910 8803
... and so on
Lets say, I have now insert new values for LOCATION_ID, but my SITENUM remains NULL
NULL 7000
NULL 7001
NULL 7002
1234 8801
4567 8802
etc.
My logic states that sitenum for 7000 should be MAX(select * sitenum from table) but the next sitenum for 7001 should be Max(select * sitenum from table)+1
Right now, the following code updates all sitenums at once to the EXACT same value
UPDATE dbo.[Monthly Hierarchy Table]
SET SITENUM = MAX(SELECT SITENUM FROM [Monthly Hierarchy Table])+1
WHERE SITENUM IS NULL and location_id is not null
But I really want it to do is go line by line, look at the max(sitenum) and increment it based on non-duplicated location_id's. There has to be some way I can compare each location_id to itself and increment the sitenumber wherever it is null and location_id is different
How do I go about doing that? Maybe the way I'm understanding it is not right, but its a pretty simple problem.