Hi,Please see my DDL/CRT/INSERT Below. I am trying to generate a rownumber for each metric_id/institution_id/period_id tuple. The value number column goes from 1-25. Basically, I need all the rows with value_number=1 to have a row number from 1 to n, where n is the total number of rows with value_number =1. Then, I want to get all the rows with value_number=2, and number each row from 1 to n again, etc, until value_number=25. I would need to generalize this to do the same for each unique metric_id/institution_id/period_id tuple. I have no idea how to do this - in my script, I have written a while statement like this: WHILE BEGIN DECLARE @temp_value_num int select @temp_value_num = 1; select --@rownum, temp_metric_id, metric_id, year, term_name, value, unit_id, currency_id, source, section, page, data_comment, @temp_value_num = value_number, risk_department_id, risk_area_id, period_id, institution_id from MTS_DATA_PRELOAD where institution_id = 1 and metric_id = 666 and CAST(term_name + ', ' + year as datetime) = '2009-12-01 00:00:00.000' and value_number = @value_num order by metric_id, institution_id, CAST(term_name + ', ' + year as datetime), value_number if @temp_value_num = @value_num @rownum +=1; else @value_num = @temp_value_num END END[code]As you can see, I have limited this to one specific unique combo of metric_id/institution_id/date.I don't think this is right, since the select returns multiple rows, so I am not sure how I would operate on a row by row basis, and get an increasing row_number until the value_number changes. Any help appreciated![code]USE [smts_temp_mdk]GO/****** Object: Table [dbo].[MTS_DATA_PRELOAD] Script Date: 02/22/2010 17:43:21 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[MTS_DATA_PRELOAD]( [temp_metric_id] [numeric](5, 0) NULL, [metric_id] [numeric](5, 0) NULL, [year] [varchar](4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [term_name] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [temp_institution_short_name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [institution_short_name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [value] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [unit_id] [numeric](2, 0) NULL, [currency_id] [numeric](2, 0) NULL, [source] [varchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [section] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [page] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [data_comment] [varchar](2000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [value_number] [numeric](3, 0) NULL, [risk_department_id] [numeric](3, 0) NULL, [risk_area_id] [numeric](5, 0) NULL, [period_id] [numeric](7, 0) NULL, [institution_id] [numeric](10, 0) NULL) ON [PRIMARY]GOSET ANSI_PADDING OFFINSERT INTO [smts_temp_mdk].[dbo].[MTS_DATA_PRELOAD] ([temp_metric_id] ,[metric_id] ,[year] ,[term_name] ,[temp_institution_short_name] ,[institution_short_name] ,[value] ,[unit_id] ,[currency_id] ,[source] ,[section] ,[page] ,[data_comment] ,[value_number] ,[risk_department_id] ,[risk_area_id] ,[period_id] ,[institution_id]) VALUES (<temp_metric_id, numeric,> ,<metric_id, numeric,> ,<year, varchar(4),> ,<term_name, varchar(30),> ,<temp_institution_short_name, varchar(50),> ,<institution_short_name, varchar(50),> ,<value, varchar(100),> ,<unit_id, numeric,> ,<currency_id, numeric,> ,<source, varchar(200),> ,<section, varchar(100),> ,<page, varchar(10),> ,<data_comment, varchar(2000),> ,<value_number, numeric,> ,<risk_department_id, numeric,> ,<risk_area_id, numeric,> ,<period_id, numeric,> ,<institution_id, numeric,>)