Hierarchial Query Help Needed

  • Create table TableA

    (

    col1 int indentity(1,1),

    col2 varchar(10),

    col3 int

    )

    insert into TableA values('M1',1)

    insert into TableA values('M2',3)

    insert into TableA values('M3',2)

    Table A (here col3 is the display order)

    ---------------------------------------

    col1col2col3

    1M11

    2M23

    3M32

    Create table TableB

    (

    col1 int indentity(1,1),

    pcol1 int,

    col2 varchar(10),

    col3 int

    )

    insert into TableB values(1,'A',1)

    insert into TableB values(1,'B',2)

    insert into TableB values(2,'A1',2)

    insert into TableB values(2,'A2',1)

    Table B (here col3 is the display order and pcol1 is the col1 from Table A)

    -------------------------------------

    Col1 pcol1 col2 col3

    1 1 A 1

    2 1 B 2

    3 2 A1 2

    4 2 A2 1

    Create table TableC

    (

    col1 int indentity(1,1),

    ccol2 int,

    ccol3 int

    )

    insert into TableC values(1,1)

    insert into TableC values(1,2)

    insert into TableC values(2,4)

    insert into TableC values(2,3)

    Table C(here ccol2 is pcol1 from TableB and ccol3 is Col1 from Table B)

    -----------------------------------------------------------------------

    col1 ccol2 ccol3

    1 1 1

    2 1 2

    3 2 4

    4 2 3

    using all the three tables i do require a query that prints in the following format

    final display

    -------------

    M1

    A

    B

    M2

    A2

    A1

  • Hello

    You will find that you will get a much better response if you take the time to post your sample data and desired results in the form of readily consumable SQL statements which people can simply cut and paste straight into SSMS.

    Please see the link in my signature for details of how to do this.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Thanks for posting ddl and data. Can you explain the logic here? All we have is a few tables with a few rows and very cryptic names and values. There must be some sort of relationship between these tables and some kind of business logic you can share.

    I can come up with a query that will produce the results you are looking for without even looking at TableC but I doubt that is what you want.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply