Solved

SQL Statement

Posted on 2014-03-14
4
285 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
Comment
Question by:conceptdata
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
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 

Open in new window

0
 

Author Comment

by:conceptdata
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

by:
Guy Hengel [angelIII / a3] earned 500 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 

Open in new window

0
 

Author Comment

by:conceptdata
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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