Solved

using ROW_NUMBER()

Posted on 2014-09-10
2
110 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 500 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

615 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