Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# SQL Statement

Posted on 2014-03-14
Medium Priority
293 Views
Last Modified: 2014-03-18
Hi

I need to Select som data in a SQL db something like in the attached file.

My table and fields :

PROJTABLE.NUMBER_
PROJTABLE.PARENTPROJNUMBER
PROJTABLE.NAME
PROJTABLE.Value1
ProjH.jpg
0
Question by:conceptdata
[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
4 Comments

LVL 143

Expert Comment

ID: 39928755
please check out this code
``````;with data as (
select p.NUMBER_, p.PARENTPROJNUMBER, p.NAME, p.Value1
from yourtable p
where p.PARENTPROJNUMBER IS NULL  --- presuming this are the "root parents", or the one you are looking for ...

UNION ALL -- here is the recursive stuff

select p.NUMBER_, p.PARENTPROJNUMBER, p.NAME, p.Value1
from DATA d
JOIN yourtable p
ON p.PARENTPROJNUMBER = d.NUMBER_
AND d.PARENTPROJNUMBER <> d.NUMBER_
)
select * from data
``````
0

Author Comment

ID: 39929029
Hi Guy

Works nice, but is it possible to sort the output like in the attached file.

Like this :

942800 -
942930 - 942800
942840 - 942800
942842 - 942840

and, output the niveua of the hierachy ?
0

LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 2000 total points
ID: 39929089
the output is normally sorted like that.
to add the level, change like this
``````;with data as (
select p.NUMBER_, p.PARENTPROJNUMBER, p.NAME, p.Value1 , 0 as HierarchyLevel
from yourtable p
where p.PARENTPROJNUMBER IS NULL  --- presuming this are the "root parents", or the one you are looking for ...

UNION ALL -- here is the recursive stuff

select p.NUMBER_, p.PARENTPROJNUMBER, p.NAME, p.Value1, d.HierarchyLevel + 1
from DATA d
JOIN yourtable p
ON p.PARENTPROJNUMBER = d.NUMBER_
AND d.PARENTPROJNUMBER <> d.NUMBER_
)
select * from data
``````
0

Author Comment

ID: 39938050
Hi Again Guy - Thanks for the good answer.

I would like to use the statement in report builder 3.0 and get a report like the attached one.
And get it illustrated at diagrams in the hierachy.

Is it something you help me with too. ??

I can create a new question for that . !
0

## Featured Post

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
###### Suggested Courses
Course of the Month5 days, 4 hours left to enroll

#### 670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.