1 | initial version |
To add a counter specifically for one record using SQL/SAS, you can use a combination of sub-queries and window functions.
Example:
SELECT COUNT(*) FROM mytable WHERE id < desired_id;
Example:
SELECT desired_id, ROW_NUMBER() OVER (ORDER BY desired_id) + (SELECT COUNT(*) FROM mytable WHERE id < desired_id) as counter
FROM mytable
WHERE id = desired_id;
This query selects the desired record and adds a counter column that starts from 1 and increments by one for each subsequent record. The ORDER BY
clause determines the order of the counter, in this case, it's ordered by the desired_id
field. The sub-query calculates the count of records before the desired one and adds it to the counter value.