Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

using ROW_NUMBER()

Posted on 2014-09-10
2
Medium Priority
?
112 Views
Last Modified: 2014-09-12
Create table CityMaster (CityCode varchar(50),name varchar(5))

insert into CityMaster values ('1.1','AA');
insert into CityMaster values ('2.0','AB');
insert into CityMaster values ('3.3','AA' );
insert into CityMaster values ('9.9.0.1','CA');
insert into CityMaster values ('AA12345','CS');
insert into CityMaster values ('BA12345','CSS');
insert into CityMaster values ('FBA12345','ERD');
insert into CityMaster values ('SBA12345','SRD');
insert into CityMaster values ('CBA12345','DFG');

insert into CityMaster values ('1.1','AA1');
insert into CityMaster values ('2.0','AB1');
insert into CityMaster values ('3.3','AA1' );
insert into CityMaster values ('9.9.0.1','CA1');
insert into CityMaster values ('AA12345','CS1');
insert into CityMaster values ('BA12345','CSS1');
insert into CityMaster values ('FBA12345','ERD1');
insert into CityMaster values ('SBA12345','SRD1');
insert into CityMaster values ('CBA12345','DFG1');


insert into CityMaster values ('1.1','AAS');
insert into CityMaster values ('2.0','ABS');
insert into CityMaster values ('3.3','AAS' );
insert into CityMaster values ('9.9.0.1','CAS');
insert into CityMaster values ('AA12345','CSS');
insert into CityMaster values ('BA12345','CSSS');
insert into CityMaster values ('FBA12345','ERDS');
insert into CityMaster values ('SBA12345','SRDS');
insert into CityMaster values ('CBA12345','DFGS');

Open in new window

Script:

SELECT  CONVERT(VARCHAR, (ROW_NUMBER() OVER(ORDER BY CityCode DESC)))  AS Slno, CityCode,NAME FROM CityMaster

I am generating sequence number using ROW_NUMBER()  function. But I need the sequence number based on citycode
If city code start with numbers (0 to 9)  it should use separate sequence number .
If citycode has following value in first 2 characters  ('AA'  AND  'BA')  it should use separate sequence number
If citycode has following value in first 2 characters  ('FBA'  AND  'SBA' AND 'CBA' )  it should use separate sequence number

I need a  out put like this:

SlNO     CITYCODE         NAME
1              1.1                     AA
2              2.0                    AB
3              3.3                    AA
1         AA12345             CSS
2         BA12345             CSSS
1        FBA12345           ERDS
2        CBA12345           DFGS
0
Comment
Question by:Varshini S
2 Comments
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 2000 total points
ID: 40315787
SELECT  Slno	=	CAST(ROW_NUMBER()
			OVER
			(	PARTITION BY
					CASE	WHEN LEFT(CityCode,2) IN ('AA','BA')		THEN RIGHT(RTRIM(CityCode),LEN(CityCode) - 2) + 'A'
						WHEN LEFT(CityCode,3) IN ('CBA','FBA','SBA')	THEN RIGHT(RTRIM(CityCode),LEN(CityCode) - 3) + 'B'
						ELSE CityCode
					END	
				ORDER BY CityCode DESC
			) as varchar)
,	CityCode
,	NAME
FROM	CityMaster

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40315874
Why use a varchar for SlNO? it will not sort correctly if you get to 10 or more. I suggest you use integer.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Integration Management Part 2
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question