|
LearningIT
Starting Member
5 Posts |
Posted - 2011-07-04 : 06:23:27
|
| Hi,I am working on a Human Resource Application using MS SQL Server 2008 Express Edition. I have following Tables:1. Employees2. BudgetCodeEmployee 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...BudgetCodeNExample:Employee Table:EmployeeId, EmployeeCode, Name, SSN, Gender1, CL7653, Paul, WT, MBudgetCode Table:BudgetCodeId, EmpId, BudgetCode, Percentage1, 1, US8765, 501, 2, US8543, 50Desired Result:EmployeeCode, Name, BudgetCode1, BudgetCode2CL7653, Paul, US8765, US8543Can any give solution to the problem, i have tried using PIVOT and some other hacks but failed to resolve. I appreciate your response :) Thanks |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2011-07-04 : 07:29:02
|
quote: Originally posted by Lumbago Hm...I suspect you need dynamic pivoting. Madhi has an article about it written some time ago but it still aplies:http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx- LumbagoMy blog-> http://thefirstsql.com/2011/02/07/regular-expressions-advanced-string-matching-and-new-split-function-sql-server-2008-r2/
You can *use* dynamic pivoting..... But that doesn't mean that you should. this is a presentation issue, not a database task.The more sensible thing to do would be to return the results as a simple list (with the employee codes, then budget codes in order)Then iterate over the list in whatever application is calling the database and every time the employeeCode changes drop down to the next row.if the result set is a sane size then storing this would probably involve a dictionary of EmployeeCode to List of BudgetCodes.Then at the end write the dictionary to wherever it needs to go.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|