473,385 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Select query help

deepuv04
227 Expert 100+
I have a table called "Company"

Expand|Select|Wrap|Line Numbers
  1. Company:
  2.  
  3. Id    Name    ParentId    Level
  4. 1    A        NULL        0
  5. 2    B        1            1
  6. 3    C        1            1
  7. 4    D        2            2
  8. 5    E        2            2
  9. 6    F        3            2
  10. 7    G        3            2
  11.  
  12.  
  13. Here I want to return the list of all chaild companies in XML format.
  14.  
  15. The XML format should be like
  16.  
  17. <Node Id="1" Name="A">
  18.     <Node Id="2" Name="B">
  19.         <Node Id="4" Name="D" />
  20.         <Node Id="5" Name="E" />
  21.     </Node>
  22.     <Node Id="3" Name="C">
  23.         <Node Id="6" Name="F">
  24.         <Node Id="7" Name="G">
  25.     </Node>
  26. </Node>
  27.  
  28.  
How can i do this... any help
Jun 12 '08 #1
2 1303
Delerna
1,134 Expert 1GB
I'm not an expert on XML but this seems to me to be a sorting issue. Sort the records in the correct order and then its simply coverting the fields into a string.

Perhaps something like this will work for you.
Expand|Select|Wrap|Line Numbers
  1. select '<Node Id="' + convert(varchar(6),ID) + '" Name="' + Name +'">'
  2. from
  3. (select top 100 percent a.ID,a.Name,a.ParentID,a.Level,
  4.     case when a.Level=0 then 0 
  5.     else case when a.Level=1 then a.id 
  6.     else case when a.Level=2 then a.ParentID 
  7.     else case when a.Level=3 then b.ParentID 
  8.     end end end end as srt,
  9.  
  10.     case when a.Level<3 then a.ID 
  11.     else case when a.Level=3 then a.ParentID 
  12.     end end as srt2
  13. from  YourTable a
  14. left join YourTable b on b.ID=a.ParentID
  15. left join YourTable c on c.ID=b.ParentID
  16. order by srt,Srt2,a.ID) a
  17.  
This should handle up to 3 levels deep, I think :)
It did for my limited dataset anyhow.

you will need to add extra joins and Srt columns to handle deeper levels

Hope it helps
Jun 13 '08 #2
deepuv04
227 Expert 100+
I'm not an expert on XML but this seems to me to be a sorting issue. Sort the records in the correct order and then its simply coverting the fields into a string.

Perhaps something like this will work for you.
Expand|Select|Wrap|Line Numbers
  1. select '<Node Id="' + convert(varchar(6),ID) + '" Name="' + Name +'">'
  2. from
  3. (select top 100 percent a.ID,a.Name,a.ParentID,a.Level,
  4.     case when a.Level=0 then 0 
  5.     else case when a.Level=1 then a.id 
  6.     else case when a.Level=2 then a.ParentID 
  7.     else case when a.Level=3 then b.ParentID 
  8.     end end end end as srt,
  9.  
  10.     case when a.Level<3 then a.ID 
  11.     else case when a.Level=3 then a.ParentID 
  12.     end end as srt2
  13. from  YourTable a
  14. left join YourTable b on b.ID=a.ParentID
  15. left join YourTable c on c.ID=b.ParentID
  16. order by srt,Srt2,a.ID) a
  17.  
This should handle up to 3 levels deep, I think :)
It did for my limited dataset anyhow.

you will need to add extra joins and Srt columns to handle deeper levels

Hope it helps
Hi,
Thanks for the reply.

Here I came across with another solution. I am using a function (calling recursively) to get all the child companies in a tree order. The code I am using is

Expand|Select|Wrap|Line Numbers
  1.  
  2. create table dbo.company
  3. ( Id smallint,
  4.   Name varchar(5),
  5.   ParentId smallint,
  6.   Level smallint
  7. )
  8. insert into dbo.company
  9. select 1, 'A', null, 0 union all
  10. select 2, 'B', 1, 1 union all
  11. select 3, 'C', 1, 1 union all
  12. select 4, 'D', 2, 2 union all
  13. select 5, 'E', 2, 2 union all
  14. select 6, 'F', 3, 2 union all
  15. select 7, 'G', 3, 2
  16. --select * from dbo.company
  17.  
  18. go
  19.  
  20.  
  21.  
  22. alter function dbo.getSubtree( @Id int, @level int)
  23. returns xml
  24. begin
  25.  
  26.  
  27.  
  28.   return
  29.   ( select
  30.       Id as [@Id],
  31.       Name as [@Name],
  32.       case when ParentId is not null
  33.            then dbo.GetSubtree (id, @level + 1)
  34.       end
  35.     from dbo.company
  36.     where parentId = @Id
  37.     order by Id
  38.     for xml path('Node'), type
  39.   )
  40.  
  41.  
  42.  
  43. end
  44.  
  45.  
  46.  
  47. go
  48.  
  49.  
  50.  
  51. select
  52.   Id as [@Id],
  53.   Name as [@Name],
  54.   dbo.GetSubtree(Id, 1)
  55. from dbo.company
  56. where parentId is null
  57. order by Id
  58. for xml path('Node'), type
  59.  
  60.  
Any suggestions please.

Thanks
Jun 13 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: John Fabiani | last post by:
Hi, I'm a newbie and I'm attempting to learn howto create a select statement. When I use >>> string1='18 Tadlock Place' >>> cursor.execute("SELECT * FROM mytest where address = %s",string1) All...
4
by: jimh | last post by:
I'm not a SQL expert. I want to be able to write a stored procedure that will return 'people who bought this product also bought this...'. I have a user table that links to a transaction table...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
1
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
4
by: Terencetrent | last post by:
I having been using Access '97/2002 for about 4 years now and have never really had the need or the time to learn visual basic. Well, I think the time has finally come. I need help with Visual...
4
by: Polly | last post by:
I had a macro that ran a parameter query and created and opened an Excel file with the system date as part of the file name, but I had to change the file name by hand. So I converted the macro to...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
0
by: djflow | last post by:
Hi! II was wondering if you can help me with SQL query.. Below 7 separated select query works fine(only when they are retrieved separately) But I want to combined them together and so that i...
9
by: P3Eddie | last post by:
Hello all! I don't know if this can even be done, but I'm sure you will either help or suggest another avenue to accomplish the same. My problem may be a simple find duplicates / do something...
45
by: dizzydangler | last post by:
Hi, I'm new to access (2007, running on XP), but I've gotten some great help from this site on my project so far. However, the more I know, the more complex the problems seem to become.... I'm...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.