How to convert diagonal rows into a single row?
sql, sql-server-2008
Solution
The aggregation you want as a result seems to be based on the order of the rows in your table. In tables, row order should not matter and it is dangerous to assume that the order will stay the same.
So unless you have not shown us the complete source table yet (with for example a `Section` column in it), it is hard to give you a helpful answer to this question.
Edit:
If you have a `Section` column, you could use the following query to get the result you want:
SELECT Section
,SUM(Engineering) AS Engineering
,SUM(Financials) AS Financials
,SUM(Scope) AS Scope
,SUM(Schedule) AS Schedule
,SUM(Risks) AS Risks
,SUM(People) AS People
FROM YourTable
GROUP BY Section;
Instead of using the `SUM` function to aggregate your data, you could also use `MAX` or another function, depending on the data and what you want to get out of it.
Problem
This is my table. The numbers are coming diagonally. ``` Engineering Financials Scope Schedule Risks People -------------------------------------------------------- 1 NULL NULL NULL NULL NULL NULL 0 NULL NULL NULL NULL NULL NULL 0 NULL NULL NULL NULL NULL NULL 0 NULL NULL NULL NULL NULL NULL 0 NULL 2 NULL NULL NULL NULL NULL NULL 4 NULL NULL NULL NULL NULL NULL 3 NULL NULL NULL NULL NULL NULL 4 NULL NULL NULL NULL NULL NULL 4 NULL NULL NULL NULL NULL NULL 0 0 NULL NULL NULL NULL NULL NULL 0 NULL NULL NULL NULL NULL NULL 0 NULL NULL NULL NULL NULL NULL 0 NULL NULL NULL NULL NULL NULL 0 NULL NULL NULL NULL NULL NULL 0 0 NULL NULL NULL NULL NULL NULL 0 NULL NULL NULL NULL NULL NULL 0 NULL NULL NULL NULL NULL NULL 0 NULL NULL NULL NULL NULL NULL 0 NULL NULL NULL NULL NULL NULL 0 1 NULL NULL NULL NULL NULL NULL 0 NULL NULL NULL NULL NULL NULL 5 NULL NULL NULL NULL NULL NULL 4 NULL NULL NULL NULL NULL NULL 3 NULL NULL NULL NULL NULL NULL 3 ``` I would like those to come in this output: ``` Engineering Financials Scope Schedule Risks People -------------------------------------------------------- 1 0 0 0 0 NULL 2 4 3 4 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 5 4 3 3 ``` Actually I want to convert those diagonal rows into a single row for each section.