I am trying to create a new ID for a dataset which will identify and group people together when certain conditions are met, for example a shared address. I'm trying to add an autoincrement column that creates this ID for those people, so I could see person x and y have the same address so they get the same new_id, but person z may not share an address with anyone and so would have the new_id field left blank.
I think my syntax is wrong and am not sure that you can do ALTER TABLE [] ADD [] AS SELECT [] WHERE []
. Can someone tell me where I'm going wrong here or how to do this properly? Sample code below. Apologies if the code formatting is bad, stackoverflow is ignoring my line breaks.
ALTER TABLE people_tab ADD new_id int autoincrement(1,1)
AS SELECT *
FROM people_tab a
INNER JOIN people_tab b
ON LEFT(a.zip,5) = LEFT(b.zip,5) and a.id != b.id
where LEN(a.address) >=10 and and LEN(b.address) >=10