I have an SQL table like that without row_number column,
acct_id part_id sub_model row_number
111 201301 A 1
111 201302 A 2
111 201303 B 1
111 201304 B 2
111 201305 C 1
111 201306 C 2
111 201307 C 3
111 201308 A 1
111 201309 A 2
111 201310 A 3
111 201311 A 4
222 201301 B 1
222 201302 B 2
222 201303 C 1
222 201304 C 2
and so on. I want to add row_number column whenever a model has changed for the Acct_ID, row number will start from 1 and count till there is a change in model. Row_numbers of different acct_IDs shouldn't effect each other. So row_number should count and start again for a specific ID. If next acct_ID starts with same model with last row of previous acct_ID, it should notice and start from 1 again. This is easy to do in excel, at least for me. But data is over a million row. So I have to handle it with SQL and no idea how to do it.
答案 0 :(得分:1)
This would be my suggestion (sql fiddle):
SELECT T1.*,
T2.row_number
FROM Table1 AS T1
JOIN (
SELECT acct_id,
part_id,
sub_model,
COUNT(*) AS row_number
FROM Table1
GROUP BY acct_id, sub_model) AS T2 ON T2.acct_id = T1.acct_id AND T2.sub_model = T1.sub_model
ORDER BY T1.acct_id, T1.part_id, T1.sub_model
I'm summing up the amount of times on sub_model has been used on a acct_id. I have no idea whether this information is actually what you are looking for, if not i think Mike Brant is correct in saying this is better done in the view layer in php or whatever language you are using.