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 queries for multiple columns depends on same column of another table

SQL queries for multiple columns depends on same column of another table

Scheduled Pinned Locked Moved Database
questiondatabasehelp
3 Posts 3 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.
  • A Offline
    A Offline
    AhmedMasum
    wrote on last edited by
    #1

    See the following scenario: Tables for the question:

    Table : T1
    c10 c12


    e1 A
    e2 B
    e3 C

    Table: T2

    c1 c2 c3 c4 c5 C6


    1 P e1 e2 null X
    2 Q e3 null null Y
    3 R e2 e1 null X
    4 S e3 e1 e2 Z

    The c3, c4 and c5 columns of T2 table depends on c12 column of T1 table.

    I want the following output:

    c1 c2 c3 c4 c5


    1 P A B null
    2 Q C null null
    3 R B A null
    4 S C A B

    Can you help me to write an SQL to retrieve the above output from the table T1 and T2? The SQL may be like:

    Select c1, c2, t1.c12 as c3....... from t1, t2 where join condition

    But I am not getting the actual sql in my mind. So please help me regarding this.

    J B 2 Replies Last reply
    0
    • A AhmedMasum

      See the following scenario: Tables for the question:

      Table : T1
      c10 c12


      e1 A
      e2 B
      e3 C

      Table: T2

      c1 c2 c3 c4 c5 C6


      1 P e1 e2 null X
      2 Q e3 null null Y
      3 R e2 e1 null X
      4 S e3 e1 e2 Z

      The c3, c4 and c5 columns of T2 table depends on c12 column of T1 table.

      I want the following output:

      c1 c2 c3 c4 c5


      1 P A B null
      2 Q C null null
      3 R B A null
      4 S C A B

      Can you help me to write an SQL to retrieve the above output from the table T1 and T2? The SQL may be like:

      Select c1, c2, t1.c12 as c3....... from t1, t2 where join condition

      But I am not getting the actual sql in my mind. So please help me regarding this.

      J Offline
      J Offline
      JOAT MON
      wrote on last edited by
      #2

      I think it would be something like this:

      SELECT c1, c2, tbl1.c12 AS c3, tbl2.c12 AS c4, tbl3.c12 AS c5
      FROM
      T2 LEFT OUTER JOIN
      T1 AS tbl1 ON T2.c3 = tbl1.c10 LEFT OUTER JOIN
      T1 AS tbl2 ON T2.c4 = tbl2.c10 LEFT OUTER JOIN
      T1 AS tbl3 ON T2.c5 = tbl3.c10

      Jack of all trades ~ Master of none.

      1 Reply Last reply
      0
      • A AhmedMasum

        See the following scenario: Tables for the question:

        Table : T1
        c10 c12


        e1 A
        e2 B
        e3 C

        Table: T2

        c1 c2 c3 c4 c5 C6


        1 P e1 e2 null X
        2 Q e3 null null Y
        3 R e2 e1 null X
        4 S e3 e1 e2 Z

        The c3, c4 and c5 columns of T2 table depends on c12 column of T1 table.

        I want the following output:

        c1 c2 c3 c4 c5


        1 P A B null
        2 Q C null null
        3 R B A null
        4 S C A B

        Can you help me to write an SQL to retrieve the above output from the table T1 and T2? The SQL may be like:

        Select c1, c2, t1.c12 as c3....... from t1, t2 where join condition

        But I am not getting the actual sql in my mind. So please help me regarding this.

        B Offline
        B Offline
        Blue_Boy
        wrote on last edited by
        #3

        Here it is

        select t2.c1,t2.c2,t1.c12 as c3,t11.c12 as c4,t111.c12 as c5
        from table2 t2
        inner join table1 t1 on t1.c10 = t2.c3
        left join table1 as t11 on t11.c10 = t2.c4
        left join table1 as t111 on t111.c10 = t2.c5


        I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.

        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