Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Loop/Row number question

Author  Topic 

ayanafridi
Starting Member

7 Posts

Posted - 2010-02-22 : 17:51:06
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 ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE 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]

GO
SET ANSI_PADDING OFF

INSERT 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,>)

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-02-22 : 18:11:20
Can you provide some sample data and expected output.

Nevertheless..Try this...Add WHERE conditions if appropriate.
select row_number () over(partition by [value_number] order by [metric_id]) as rownum, *
from [dbo].[MTS_DATA_PRELOAD]

Go to Top of Page

ayanafridi
Starting Member

7 Posts

Posted - 2010-02-22 : 18:44:20
quote:
Originally posted by vijayisonly

Can you provide some sample data and expected output.

Nevertheless..Try this...Add WHERE conditions if appropriate.
select row_number () over(partition by [value_number] order by [metric_id]) as rownum, *
from [dbo].[MTS_DATA_PRELOAD]





OK, you are a beautiful person! I tried a modification of this and it worked! I have been messing with this thing for DAYS.
Thank you so much! Here is what ended up working:


select row_number () over(partition by dp.metric_id,dp.institution_id, dp.period_id,dp.[value_number] order by dp.[metric_id]) as rownum from MTS_DATA_PRELOAD


Thanks again!
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-02-22 : 20:29:50
Np. You're welcome.
Go to Top of Page
   

- Advertisement -