473,405 Members | 2,262 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,405 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 1304
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...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.