Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Sql Doubt

Sql Doubt

Scheduled Pinned Locked Moved Database
database
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rajanandal
    wrote on last edited by
    #1

    Hi everyone, my table is 6219 HBO 02 0 6220 HBO1 0201 6219 6221 HEAO 0202 6219 6222 HTS 0203 6219 6231 MEAO 0403 6228 6232 MTS 0404 6228 6234 Midde 05 0 6235 Overi 06 0 6228 MBO 04 0 6236 WO 10 0 6237 Post 07 0 6239 Basi NULL NULL 6448 Athe 05001 6234 but i need like this, 6219 HBO 02 0 6220 HBO1 0201 6219 6221 HEAO 0202 6219 6222 HTS 0203 6219 6228 MBO 04 0 6231 MEAO 0403 6228 6232 MTS 0404 6228 6234 Midde 05 0 6448 Athe 05001 6234 6235 Overi 06 0 6236 WO 10 0 6237 Post 07 0 6239 Basi NULL NULL

    Nothing is Impossible. Keep always Smiling... :)

    L N 2 Replies Last reply
    0
    • R rajanandal

      Hi everyone, my table is 6219 HBO 02 0 6220 HBO1 0201 6219 6221 HEAO 0202 6219 6222 HTS 0203 6219 6231 MEAO 0403 6228 6232 MTS 0404 6228 6234 Midde 05 0 6235 Overi 06 0 6228 MBO 04 0 6236 WO 10 0 6237 Post 07 0 6239 Basi NULL NULL 6448 Athe 05001 6234 but i need like this, 6219 HBO 02 0 6220 HBO1 0201 6219 6221 HEAO 0202 6219 6222 HTS 0203 6219 6228 MBO 04 0 6231 MEAO 0403 6228 6232 MTS 0404 6228 6234 Midde 05 0 6448 Athe 05001 6234 6235 Overi 06 0 6236 WO 10 0 6237 Post 07 0 6239 Basi NULL NULL

      Nothing is Impossible. Keep always Smiling... :)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      And?

      R 1 Reply Last reply
      0
      • L Lost User

        And?

        R Offline
        R Offline
        rajanandal
        wrote on last edited by
        #3

        that is all child records should be displayed next to parent.

        Nothing is Impossible. Keep always Smiling... :)

        D 1 Reply Last reply
        0
        • R rajanandal

          that is all child records should be displayed next to parent.

          Nothing is Impossible. Keep always Smiling... :)

          D Offline
          D Offline
          dxlee
          wrote on last edited by
          #4

          If you tell us that the fourth field is the parent's ID (and the first field is its ID), then we will have an easier time trying to figure out what you want.

          modified on Tuesday, December 15, 2009 10:57 AM

          1 Reply Last reply
          0
          • R rajanandal

            Hi everyone, my table is 6219 HBO 02 0 6220 HBO1 0201 6219 6221 HEAO 0202 6219 6222 HTS 0203 6219 6231 MEAO 0403 6228 6232 MTS 0404 6228 6234 Midde 05 0 6235 Overi 06 0 6228 MBO 04 0 6236 WO 10 0 6237 Post 07 0 6239 Basi NULL NULL 6448 Athe 05001 6234 but i need like this, 6219 HBO 02 0 6220 HBO1 0201 6219 6221 HEAO 0202 6219 6222 HTS 0203 6219 6228 MBO 04 0 6231 MEAO 0403 6228 6232 MTS 0404 6228 6234 Midde 05 0 6448 Athe 05001 6234 6235 Overi 06 0 6236 WO 10 0 6237 Post 07 0 6239 Basi NULL NULL

            Nothing is Impossible. Keep always Smiling... :)

            N Offline
            N Offline
            Niladri_Biswas
            wrote on last edited by
            #5

            Try this. Inputs:

            declare @t table(childid int,name varchar(20),code int,parentid int)
            insert into @t values (6219,'HBO',02,0)
            insert into @t values(6220,'HBO1', 0201,6219)
            insert into @t values(6221,'HEAO',0202,6219)
            insert into @t values(6222,'HTS',0203,6219)
            insert into @t values(6231,'MEAO',0403,6228)
            insert into @t values(6232,'MTS',0404,6228)
            insert into @t values(6234,'Midde',05,0)
            insert into @t values(6235,'Overi',06,0)
            insert into @t values(6228,'MBO',04,0)
            insert into @t values(6236,'WO',10,0)
            insert into @t values(6237,'Post',07,0)
            insert into @t values(6239,'Basi',NULL,NULL)
            insert into @t values(6448,'Athe',05001,6234)
            select * from @t

            Query:

            ;with cte as
            (
            select
            cast(t1.childid as varchar(1000)) [path]
            , t1.childid
            ,t1.name
            ,t1.code
            ,t1.parentid
            ,0 AS [Level] from @t t1 where parentid = 0 or parentid is null

            union all
            select 
            	cast(\[path\] + '/' + cast(t1.childid as varchar(1000)) as varchar(1000)) \[path\]
            	,t1.childid
            	,t1.name
            	,t1.code
            	,t1.parentid 
            	,c.\[Level\]+1 AS \[Level\]from @t t1
            join cte c
            on c.childid = t1.parentid
            

            )
            select childid,name,code,parentid
            from cte order by [path]

            Output:

            childid name code parentid
            6219 HBO 2 0
            6220 HBO1 201 6219
            6221 HEAO 202 6219
            6222 HTS 203 6219
            6228 MBO 4 0
            6231 MEAO 403 6228
            6232 MTS 404 6228
            6234 Midde 5 0
            6448 Athe 5001 6234
            6235 Overi 6 0
            6236 WO 10 0
            6237 Post 7 0
            6239 Basi NULL NULL

            :)

            Niladri Biswas

            R 1 Reply Last reply
            0
            • N Niladri_Biswas

              Try this. Inputs:

              declare @t table(childid int,name varchar(20),code int,parentid int)
              insert into @t values (6219,'HBO',02,0)
              insert into @t values(6220,'HBO1', 0201,6219)
              insert into @t values(6221,'HEAO',0202,6219)
              insert into @t values(6222,'HTS',0203,6219)
              insert into @t values(6231,'MEAO',0403,6228)
              insert into @t values(6232,'MTS',0404,6228)
              insert into @t values(6234,'Midde',05,0)
              insert into @t values(6235,'Overi',06,0)
              insert into @t values(6228,'MBO',04,0)
              insert into @t values(6236,'WO',10,0)
              insert into @t values(6237,'Post',07,0)
              insert into @t values(6239,'Basi',NULL,NULL)
              insert into @t values(6448,'Athe',05001,6234)
              select * from @t

              Query:

              ;with cte as
              (
              select
              cast(t1.childid as varchar(1000)) [path]
              , t1.childid
              ,t1.name
              ,t1.code
              ,t1.parentid
              ,0 AS [Level] from @t t1 where parentid = 0 or parentid is null

              union all
              select 
              	cast(\[path\] + '/' + cast(t1.childid as varchar(1000)) as varchar(1000)) \[path\]
              	,t1.childid
              	,t1.name
              	,t1.code
              	,t1.parentid 
              	,c.\[Level\]+1 AS \[Level\]from @t t1
              join cte c
              on c.childid = t1.parentid
              

              )
              select childid,name,code,parentid
              from cte order by [path]

              Output:

              childid name code parentid
              6219 HBO 2 0
              6220 HBO1 201 6219
              6221 HEAO 202 6219
              6222 HTS 203 6219
              6228 MBO 4 0
              6231 MEAO 403 6228
              6232 MTS 404 6228
              6234 Midde 5 0
              6448 Athe 5001 6234
              6235 Overi 6 0
              6236 WO 10 0
              6237 Post 7 0
              6239 Basi NULL NULL

              :)

              Niladri Biswas

              R Offline
              R Offline
              rajanandal
              wrote on last edited by
              #6

              Thanks for ur timely help...

              Nothing is Impossible. Keep always Smiling... :)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups