Solved

How do I add an ascending count to a column of data via tsql as I am selecting it?

Posted on 2011-03-14
7
368 Views
Last Modified: 2012-05-11
How do I add an ascending count to a column of data via tsql as I am selecting it? i.e

Column Data                Want to return

AAA                                 AAA1
BBB                                  BBB2
CCC                                  CCC3
DDD                                  DDD4

Guessing there is a way to concatenate the column data to row_number but haven't been able to work out the exact statement required.
0
Comment
Question by:ams_group
  • 5
  • 2
7 Comments
 
LVL 9

Accepted Solution

by:
mayank_joshi earned 500 total points
ID: 35125873
WITH CTE AS
(
    SELECT Coulmn_Data,   ROW_NUMBER() OVER (ORDER BY Coulmn_Data ) AS 'RowNumber'
    FROM Table1
) 
SELECT Coulmn_Data,Cast( Coulmn_Data as NVARCHAR(100)) +Cast(RowNumber  as NVARCHAR(100) ) as COL1 FROM CTE;

Open in new window

0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35125880
if ' Column Data' is non numeric you may  try:-

WITH CTE AS
(
    SELECT Coulmn_Data,   ROW_NUMBER() OVER (ORDER BY Coulmn_Data ) AS 'RowNumber'
    FROM Table1
) 
SELECT Coulmn_Data,Coulmn_Data +RowNumber as COL1 FROM CTE;

Open in new window

0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35125907
shorter:-

 
SELECT Coulmn_Data, Coulmn_Data+Cast( ROW_NUMBER() OVER (ORDER BY Coulmn_Data ) AS 
NVARCHAR(100)) 'RowNumber'
    FROM Table1

Open in new window

0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Closing Comment

by:ams_group
ID: 35125988
Thanks, that was spot on
0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35125989
I VE TESETED  THIS.IT WORKS FINE:-

SELECT column_data, column_data+Cast( ROW_NUMBER() OVER (ORDER BY column_data ) AS 
NVARCHAR(100)) as Want_To_Return
    FROM testtbl

Open in new window

0
 

Author Comment

by:ams_group
ID: 35127486
Thanks for your great solutions, just a quick question, any idea if it is possible to achieve a similar result in SQL 2000? Guessing identity could be a candiate for use although not sure it can be used to similar effect?
0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35127572
There is no direct equivalent to  rownum or row id in SQL Server 2000. Strictly speaking, in a relational database, rows within a table are not ordered and a row id won't really make sense. But if you need that functionality, consider the following three alternatives:

    * Add an IDENTITY column to your table. See Books Online for more information

    * Use the following query to generate a row number for each row. The following query generates a row number for each row in the authors table of pubs database. For this query to work, the table must have a unique key.

 
    SELECT        (SELECT COUNT(i.au_id) 
                     FROM pubs..authors i 
                     WHERE i.au_id >= o.au_id ) AS RowID, 
                    au_fname + ' ' + au_lname AS 'Author name'
      FROM          pubs..authors o
      ORDER BY      RowID

Open in new window


    * Use a temporary table approach, to store the entire resultset into a temporary table, along with a row id generated by the IDENTITY() function. Creating a temporary table will be costly, especially when you are working with large tables. Go for this approach, if you don't have a unique key in your table. Search for IDENTITY (Function) in SQL Server Books Online.

For more ideas on this topic, click http://support.microsoft.com/support/kb/articles/q186/1/33.asp  to read an informative article from Microsoft Knowledgebase.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
default constraint within a function 3 37
Changing multiple SQL login passwords 9 31
SQL Server Question 5 25
create insert script based on records in a table 4 14
Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

21 Experts available now in Live!

Get 1:1 Help Now