Solved

using ROW_NUMBER()

Posted on 2014-09-10
2
96 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 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 48

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now