SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot impunjabians's Avatar
    Join Date
    Dec 2007
    Bed Room
    0 Post(s)
    0 Thread(s)

    Converting Rows to Columns


    I am working on a Human Resource Application using MS SQL Server 2008 Express Edition.

    I have following Tables:

    1. Employees
    2. BudgetCode

    Employee Table:

    EmployeeId(Primary Key, Participates in relation with other tables), EmployeeCode (UNIQUE Key), Name, SSN, Gender)

    BudgetCode Table:

    BudgetCodeId, EmpId ( FK to EmployeeId), BudgetCode, Percentage)

    Relation: This relation is one-to-many relation between Employee Table. So one employee can have many BudgetCodes in BudgetCode Table.

    My desired result is like,

    EmployeeCode, Name, BudgetCode1,BudgetCode2,BudgetCode3...BudgetCodeN


    Employee Table:

    EmployeeId, EmployeeCode, Name, SSN, Gender
    1, CL7653, Paul, WT, M

    BudgetCode Table:

    BudgetCodeId, EmpId, BudgetCode, Percentage
    1, 1, US8765, 50
    1, 2, US8543, 50

    Desired Result:

    EmployeeCode, Name, BudgetCode1, BudgetCode2
    CL7653, Paul, US8765, US8543

    Can any give solution to the problem, i have tried using PIVOT and some other hacks but failed to resolve. I appreciate your response


  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Toronto, Canada
    60 Post(s)
    3 Thread(s)
    Quote Originally Posted by impunjabians View Post
    i have tried using PIVOT and some other hacks but failed to resolve.
    you should really be doing the re-formatting of results in the application layer (asp or .net or whatever you're using) -- it's much more efficient that way

    if you insist on doing it in SQL, please show your PIVOT or other hacks attempts | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts