Tuesday, 6 August 2013

Query to turn rows into columns

Query to turn rows into columns

In some business applications, raw data comes in, I normalize and process
them, and store them for reporting purpose.
For example, raw data:
Transaction (transaction ID, employee 1 ID, employee 2 ID, employee 3 ID)
is normalized into
Transaction(transaction ID)
TransactionEmployee (transaction ID, employee ID)
When it comes to reporting requirements, employees have to appear in one
row - similar to raw Transaction.
My solution has been to use an application programming language to loop
TransactionEmployee, and construct INSERT statement to put reporting data
into another table - each transaction with 3 employee IDs.
But I would feel more comfortable to do it in SQL.
Is this feasible via SQL?

No comments:

Post a Comment