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
Solved

SQL server creating a query

Posted on 2008-10-29
7
284 Views
Last Modified: 2012-05-05
Hi im offering maximum points to who ever can help me
I am creating a database which contains a table structure, this structure table has four different levels within it.

For example level 4 has a parent code that relates to level 3 which level 3 parent code is related to level 2 and the level 2 parent code is related to level 1

i am tryin to create a veiw in sql server that will show all records

so say someone is on level four then it will go to all the levels,, where as a person maybe just relate to level 3 or 2 or 1 but it wont recognise it as the veiw only shows from level four downwards

i have attached a file to show you

as i sed it only shows from level four but i want to show all the levels down the side for referencing

for example if there goin from level 2 then it will show show level 2 and level 1

and if from level 1 it will only show level 1

etc etc

Please help me


untitled.bmp
0
Comment
Question by:LukeConey
  • 4
  • 2
7 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22832417
You don't need 4 tables to store 4 levels.

Below is an example of a single table that uses CTE's to handle your recursive parent/child data.
create table #Levels
 (LevelID        int identity not null
 ,ParentLevelID  int null
 ,code           varchar(32)
 ,sequence_code  varchar(255)
 )
go
 
insert into #Levels (ParentLevelID,code,sequence_code) values(null, '000001','0000')
insert into #Levels (ParentLevelID,code,sequence_code) values(1, '000002','0000')
insert into #Levels (ParentLevelID,code,sequence_code) values(2, '000003','0000')
insert into #Levels (ParentLevelID,code,sequence_code) values(3, '000004','0000')
insert into #Levels (ParentLevelID,code,sequence_code) values(null, '100001','0001')
insert into #Levels (ParentLevelID,code,sequence_code) values(5, '100002','0001')
insert into #Levels (ParentLevelID,code,sequence_code) values(6, '100003','0001')
insert into #Levels (ParentLevelID,code,sequence_code) values(7, '100004','0001')
 
go
select * from #Levels
 
;with AllLevels as
(select levelid,parentlevelid,1 as theLevel from #Levels
where parentLevelID is null
union all
select l.levelid,l.parentlevelid,al.theLevel+1 from #Levels l
join AllLevels al
on l.parentlevelid = al.levelid)
select * from alllevels al
join #Levels l
on al.levelID = l.LevelID
go
drop table #Levels

Open in new window

0
 

Author Comment

by:LukeConey
ID: 22832477
yeah i understand what you are sayin

i already have them as one table but i want to show how they work down twoards level 1

not just all in one column if you understand that

i created veiws so that it would select all levels and show how they work from 4 to 1 but i want to also show if someone just buys a kitchen they are level 1 and i want it to show the 4 levels which will stay at 1 whereas from 4 i will show 4 levels but it will work down to 1

0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 22832617
OK.  I'm still not sure what you are attempting to display, but does this help you display the data in a format closer to what you want?
create table #Levels
 (LevelID        int identity not null
 ,ParentLevelID  int null
 ,code           varchar(32)
 ,sequence_code  varchar(255)
 )
go
 
insert into #Levels (ParentLevelID,code,sequence_code) values(null, '000001','0000')
insert into #Levels (ParentLevelID,code,sequence_code) values(1, '000002','0000')
insert into #Levels (ParentLevelID,code,sequence_code) values(2, '000003','0000')
insert into #Levels (ParentLevelID,code,sequence_code) values(3, '000004','0000')
insert into #Levels (ParentLevelID,code,sequence_code) values(null, '100001','0001')
insert into #Levels (ParentLevelID,code,sequence_code) values(5, '100002','0001')
insert into #Levels (ParentLevelID,code,sequence_code) values(6, '100003','0001')
insert into #Levels (ParentLevelID,code,sequence_code) values(7, '100004','0001')
 
go
select * from #Levels
 
;with AllLevels as
(select levelid as rootLevel,levelid,parentlevelid,1 as theLevel,cast(Code as nvarchar(max)) code,cast(sequence_code as nvarchar(max)) sequence_code from #Levels
where parentLevelID is null
union all
select al.rootLevel,l.levelid,l.parentlevelid,al.theLevel+1,al.code + '/' + l.Code,al.sequence_code + '/' + l.sequence_code from #Levels l
join AllLevels al
on l.parentlevelid = al.levelid)
select * from alllevels al
join #Levels l
on al.levelID = l.LevelID
go
drop table #Levels

Open in new window

0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:LukeConey
ID: 22832947
its not workin
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22832966
The query that I posted isn't working?  It works for me.  I know that's a typical excuse, but I copied out what I posted and ran it, it works.  

What's it doing/not doing?


0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 23395478
This question has been open for 3 months now.  You didn't post anything about what ISN'T working so I think it's fair to assume it IS working.  Can you close this up!

Thanks.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

839 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