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. CrossTab

CrossTab

Scheduled Pinned Locked Moved Database
5 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
    Amit Kumar G
    wrote on last edited by
    #1

    Hello, I have two tables as below Table1 (where id is Pk key) Id Name 1 A 2 B Table2 Id Reasons 1 X 2 Y 1 Z I need output like as below ID NAME REASON1 REASON2 1 A X Z 2 B Y Null Amit

    P F 2 Replies Last reply
    0
    • A Amit Kumar G

      Hello, I have two tables as below Table1 (where id is Pk key) Id Name 1 A 2 B Table2 Id Reasons 1 X 2 Y 1 Z I need output like as below ID NAME REASON1 REASON2 1 A X Z 2 B Y Null Amit

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      If you're using SQL Server 2005, take a look at the Pivot command.

      Deja View - the feeling that you've seen this post before.

      A 1 Reply Last reply
      0
      • P Pete OHanlon

        If you're using SQL Server 2005, take a look at the Pivot command.

        Deja View - the feeling that you've seen this post before.

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

        Any example please....

        1 Reply Last reply
        0
        • A Amit Kumar G

          Hello, I have two tables as below Table1 (where id is Pk key) Id Name 1 A 2 B Table2 Id Reasons 1 X 2 Y 1 Z I need output like as below ID NAME REASON1 REASON2 1 A X Z 2 B Y Null Amit

          F Offline
          F Offline
          fasih1981
          wrote on last edited by
          #4

          see the following example : it may help u (run it in sql query analyser) CREATE TABLE #T1(id int,Name varchar(10)) insert into #T1 (id,Name) values (1,'Haris') insert into #T1 (id,Name) values (2,'Arshad') --select * from #T1 CREATE TABLE #T2(id int,Name varchar(5)) insert into #T2 (id,Name) values (1,'X') insert into #T2 (id,Name) values (2,'Y') insert into #T2 (id,Name) values (1,'Z') insert into #T2 (id,Name) values (1,'X') SELECT Id,Emp,X,Y,Z FROM ( SELECT #T1.Id,#T1.Name AS Emp,#T2.Name FROM #T1 INNER JOIN #T2 ON #T2.Id = #T1.Id ) s PIVOT ( COUNT(Name) FOR Name IN (X,Y,Z) ) p drop table #T1 drop table #T2

          fasih_is_my_signature

          A 1 Reply Last reply
          0
          • F fasih1981

            see the following example : it may help u (run it in sql query analyser) CREATE TABLE #T1(id int,Name varchar(10)) insert into #T1 (id,Name) values (1,'Haris') insert into #T1 (id,Name) values (2,'Arshad') --select * from #T1 CREATE TABLE #T2(id int,Name varchar(5)) insert into #T2 (id,Name) values (1,'X') insert into #T2 (id,Name) values (2,'Y') insert into #T2 (id,Name) values (1,'Z') insert into #T2 (id,Name) values (1,'X') SELECT Id,Emp,X,Y,Z FROM ( SELECT #T1.Id,#T1.Name AS Emp,#T2.Name FROM #T1 INNER JOIN #T2 ON #T2.Id = #T1.Id ) s PIVOT ( COUNT(Name) FOR Name IN (X,Y,Z) ) p drop table #T1 drop table #T2

            fasih_is_my_signature

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

            Thanks but i don't want to count. My rwq is little different and i would really appreciate if you could help me in this.. 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 Thanks Amit

            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