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. Web Development
  3. ASP.NET
  4. SQL Rows Join...

SQL Rows Join...

Scheduled Pinned Locked Moved ASP.NET
database
5 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.
  • A Offline
    A Offline
    Amit Kumar G
    wrote on last edited by
    #1

    I have one table CREATE TABLE #T1(id int,Name varchar(10)) insert into #T1 (id,Name) values (1,'Haris') insert into #T1 (id,Name) values (2,'Arshad') CREATE TABLE #T2(id int,Reason varchar(5)) insert into #T2 (id,Reason) values (1,'X') insert into #T2 (id,Reason) values (2,'Y') insert into #T2 (id,Reason) values (1,'Z') insert into #T2 (id,Reason) values (1,'X') Now i need output like Id Name Reason1 Reason2 Reason3 ......ReasonN 1 Haris X Z X 2 Arshad Y NULL NULL Amit

    K I 2 Replies Last reply
    0
    • A Amit Kumar G

      I have one table CREATE TABLE #T1(id int,Name varchar(10)) insert into #T1 (id,Name) values (1,'Haris') insert into #T1 (id,Name) values (2,'Arshad') CREATE TABLE #T2(id int,Reason varchar(5)) insert into #T2 (id,Reason) values (1,'X') insert into #T2 (id,Reason) values (2,'Y') insert into #T2 (id,Reason) values (1,'Z') insert into #T2 (id,Reason) values (1,'X') Now i need output like Id Name Reason1 Reason2 Reason3 ......ReasonN 1 Haris X Z X 2 Arshad Y NULL NULL Amit

      K Offline
      K Offline
      Kschuler
      wrote on last edited by
      #2

      I'm not sure that is possible. The best I can think of is to join to make the output like this: Haris X Haris Z Haris X Arshad Y Once you get your data into a DataTable, you can manipulate it so that when it displays in a DataGrid it looks like the output you want. The problem is, there is no way to how many Reasons there are for each Name. So there is no way to make an output table that has an undetermined number of columns.

      A 1 Reply Last reply
      0
      • K Kschuler

        I'm not sure that is possible. The best I can think of is to join to make the output like this: Haris X Haris Z Haris X Arshad Y Once you get your data into a DataTable, you can manipulate it so that when it displays in a DataGrid it looks like the output you want. The problem is, there is no way to how many Reasons there are for each Name. So there is no way to make an output table that has an undetermined number of columns.

        A Offline
        A Offline
        Amit Kumar G
        wrote on last edited by
        #3

        No looking for SQL Solution. I posted here becuase not got much reply from SQL forum....Thanks any way!!

        S 1 Reply Last reply
        0
        • A Amit Kumar G

          No looking for SQL Solution. I posted here becuase not got much reply from SQL forum....Thanks any way!!

          S Offline
          S Offline
          sulabh2020
          wrote on last edited by
          #4

          Hi u have to make use of function returning table with in sql server where you have to pass the id.

          Hello Forum Always be in touch to help about the topic ASP.NET

          1 Reply Last reply
          0
          • A Amit Kumar G

            I have one table CREATE TABLE #T1(id int,Name varchar(10)) insert into #T1 (id,Name) values (1,'Haris') insert into #T1 (id,Name) values (2,'Arshad') CREATE TABLE #T2(id int,Reason varchar(5)) insert into #T2 (id,Reason) values (1,'X') insert into #T2 (id,Reason) values (2,'Y') insert into #T2 (id,Reason) values (1,'Z') insert into #T2 (id,Reason) values (1,'X') Now i need output like Id Name Reason1 Reason2 Reason3 ......ReasonN 1 Haris X Z X 2 Arshad Y NULL NULL Amit

            I Offline
            I Offline
            Imran Khan Pathan
            wrote on last edited by
            #5

            Hi There Do u know about DataRelation? U can do it using datarelation follow step select all rows of table one and bind dataset ie. ds1 select all rows of table two with relation of table one like T1.Id=T2.Id and bind dataset ie. ds2 Now use datarelation ie. DataRelation dr = new DataRelation("new_relation", ds1.Tables[0].Columns[0], ds2.Tables[0].Columns[0]); ds1.Relations.Add(dr); Make sure both ds have same column[0] Best Regard Pathan

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

            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