
numbering my records based on a grouping
Does this table have a primary key? If not, then adding an incrementing row
count just seems likely to add to your problems. Every table should have a
key. Create and populate a new table like this:
CREATE TABLE NewTable (name VARCHAR(10), type CHAR(1), cnt INTEGER NOT NULL
CHECK (cnt>0), PRIMARY KEY (name,type))
INSERT INTO NewTable (name, type, cnt)
SELECT name, type, COUNT(*)
FROM SomeTable
GROUP BY name, type
If you do have a key then you can create the Cntr column either with an
UPDATE:
UPDATE SomeTable
SET cntr =
(SELECT COUNT(*)
FROM SomeTable AS T
WHERE name = SomeTable.name
AND keycol <= SomeTable.keycol)
Or in a SELECT statement:
SELECT name, type,
(SELECT COUNT(*)
FROM SomeTable AS T
WHERE name = SomeTable.name
AND keycol <= SomeTable.keycol)
FROM SomeTable
--
David Portas
SQL Server MVP
--