Solved

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

Posted on 2011-03-17
6
375 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
[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
  • 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
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Viewers will learn how the fundamental information of how to create a table.

733 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