Solved

Creating Order Numbering Sequence such as 1.1, 2, 2.1, 2.2 using SQL Server CTE

Posted on 2011-03-17
6
370 Views
Last Modified: 2012-05-11
I am trying to create a numbering sequence (1, 1.1, 1.2, 2, 2.1, 2.2, etc.) using CTE in SQL.  I am having so many issues, please help.  Below is my script:

with rs as(
select id, parentid, id [root], title, projectid,
      1 as lvl, id as sort_key
from folder
where parentid is null

union all

select c.id, c.parentid, p.[root], c.title, c.projectid, p.lvl + 1,
      p.lvl & '.' & p.sort_key as step
from rs  p
inner join folder c on p.id = c.parentid
)
select *
from rs
where projectid = 597
order by [root]
0
Comment
Question by:vnewman29
  • 2
  • 2
  • 2
6 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 35156865
Hi.

Instead of :
p.lvl & '.' & p.sort_key as step

Open in new window


I might try something like:
p.sort_key + '.' + convert(varchar(10), row_number() over(partition by p.id order by c.id)) as step

Open in new window


Hope that helps.

Kevin
0
 

Author Comment

by:vnewman29
ID: 35156913
That didn't work.  I am getting "Conversion failed when converting the varchar value '.' to data type int.
0
 
LVL 18

Expert Comment

by:deighton
ID: 35157405
how does that work exactly?

1 is a base, then it will contain  1.1 and 1.2
what determines the numbers you use, for example how do you know it will be 1.1 and 1.2 (which fields determine this)

could you give us your table structure, some sample data and a quick explanation?
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:vnewman29
ID: 35157593
The attached image shows the data.  The root field is the main group (1,2,3, ect.) and the parentid is the level and then the id.

For example the 1st group from the image should show as this:

Distribution/Instruction Memoranda 1.0
*_/_/2010 Execution Instruction 1.1
*_/_/2010 Distribution Email 1.2
*Fannie Mae Approvals 1.3
*12/22/2010 Approval 1.4
Transaction Documents 2.0
Untitled.jpg
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 35157787
That means your id column is INT.  From your original code, I figured you already had that as VARCHAR.  In order for this to work, you will need to convert the ID column to VARCHAR in the anchor query.

i.e., CONVERT(VARCHAR(10), id) as sort_key
0
 
LVL 18

Expert Comment

by:deighton
ID: 35157899
where does the '2' of Transaction Documents derive from?
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SqlDataBase 7 48
sql select record as one long string 21 24
Loops and updating in SQL Query 9 27
SQL Query assistance 16 25
In this article I will describe the Copy Database Wizard 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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

772 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