Solved

SQL Statement

Posted on 2014-03-14
4
286 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
[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

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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql query 5 57
Get data from two MySQL tables 6 65
Http Post send by Store Procedure Help 5 30
EF5: Update Model from Database not working 3 80
In this article I will describe the Detach & Attach 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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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