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. showing column data as header

showing column data as header

Scheduled Pinned Locked Moved Database
databasecomhelpcareer
6 Posts 5 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
    Ramkumar_S
    wrote on last edited by
    #1

    Hi All, I have two table like this Table1 ------- empcode basic salary ------- -------------- 160 3000 170 4000 Table 2 --------- Amount Additions empcode ------ ---------- ------- 180 Bonus 160 25 Transport 160 so i want the output like this empcode basic Salary Bonus Transport ------- ------------ ----- ---------- 160 3000 180 25 179 4000 175 45 Please help me with this ..I am new to DB

    Ramkumar ("When you build bridges you can keep crossing them. ") http://ramkumarishere.blogspot.com

    P S J M 4 Replies Last reply
    0
    • R Ramkumar_S

      Hi All, I have two table like this Table1 ------- empcode basic salary ------- -------------- 160 3000 170 4000 Table 2 --------- Amount Additions empcode ------ ---------- ------- 180 Bonus 160 25 Transport 160 so i want the output like this empcode basic Salary Bonus Transport ------- ------------ ----- ---------- 160 3000 180 25 179 4000 175 45 Please help me with this ..I am new to DB

      Ramkumar ("When you build bridges you can keep crossing them. ") http://ramkumarishere.blogspot.com

      P Online
      P Online
      PIEBALDconsult
      wrote on last edited by
      #2

      Looks like you need a join.

      R 1 Reply Last reply
      0
      • P PIEBALDconsult

        Looks like you need a join.

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

        Thanks My Table 2 is dynamic value ..so header should change based on Addition column Table 2 --------- Amount Additions empcode ------ ---------- ------- 180 Bonus 160 25 Transport 160

        Ramkumar ("When you build bridges you can keep crossing them. ") http://ramkumarishere.blogspot.com

        1 Reply Last reply
        0
        • R Ramkumar_S

          Hi All, I have two table like this Table1 ------- empcode basic salary ------- -------------- 160 3000 170 4000 Table 2 --------- Amount Additions empcode ------ ---------- ------- 180 Bonus 160 25 Transport 160 so i want the output like this empcode basic Salary Bonus Transport ------- ------------ ----- ---------- 160 3000 180 25 179 4000 175 45 Please help me with this ..I am new to DB

          Ramkumar ("When you build bridges you can keep crossing them. ") http://ramkumarishere.blogspot.com

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

          Assuming you are using SQL Server. I would suggest that you read up on pivot queries. MSDN: Pivot Query[^]

          Nagy Vilmos wrote:

          And eat bacon. Bacon's real important for 'puters.

          1 Reply Last reply
          0
          • R Ramkumar_S

            Hi All, I have two table like this Table1 ------- empcode basic salary ------- -------------- 160 3000 170 4000 Table 2 --------- Amount Additions empcode ------ ---------- ------- 180 Bonus 160 25 Transport 160 so i want the output like this empcode basic Salary Bonus Transport ------- ------------ ----- ---------- 160 3000 180 25 179 4000 175 45 Please help me with this ..I am new to DB

            Ramkumar ("When you build bridges you can keep crossing them. ") http://ramkumarishere.blogspot.com

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            Assuming SQL Server, You can achieve this using the following PIVOT

            select empcode, basicSalary, ISNULL(Bonus,0) as Bonus, ISNULL(Transport,0) as Transport
            from
            (SELECT s.empcode, s.basicSalary, a.description, a.amount
            FROM salary s
            LEFT JOIN additions a
            ON s.empcode=a.empcode ) AS SalaryWithAdditions
            PIVOT(
            SUM(amount)
            FOR description IN ([Bonus],[Transport])
            ) AS PivotTable

            Output with your test data:

            empcode basicSalary Bonus Transport
            160 3000 180 25
            170 4000 0 0

            1 Reply Last reply
            0
            • R Ramkumar_S

              Hi All, I have two table like this Table1 ------- empcode basic salary ------- -------------- 160 3000 170 4000 Table 2 --------- Amount Additions empcode ------ ---------- ------- 180 Bonus 160 25 Transport 160 so i want the output like this empcode basic Salary Bonus Transport ------- ------------ ----- ---------- 160 3000 180 25 179 4000 175 45 Please help me with this ..I am new to DB

              Ramkumar ("When you build bridges you can keep crossing them. ") http://ramkumarishere.blogspot.com

              M Offline
              M Offline
              mhwasim
              wrote on last edited by
              #6

              Check the following query... SELECT T1.empcode,T1.[basic Salary],B.Bonus,T.Transport FROM Table1 T1 INNER JOIN (SELECT Amount As Bonuus, empcode FROM Table2 WHERE Additions = 'Bonus') B ON B.empcode = T1.empcode INNER JOIN (SELECT Amount AS Transport, empcode FROM Table2 WHERE Additions = 'Transport') T ON T.empcode = T1.empcode Adjust the inner join to left join if required... Thanks

              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