Solved

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

Posted on 2011-03-17
6
382 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 60

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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 60

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 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