Solved

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

Posted on 2011-03-17
6
366 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how to extract a number from a MS SQL server string. 39 64
Report Builder 9 30
Azure SQL DB? 3 16
Test a query 23 17
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

757 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

23 Experts available now in Live!

Get 1:1 Help Now