Yes, you can do this fairly easily in SQL Server 2005 and up - the magic word is PIVOT
.
Here's the query you need:
SELECT
TaskID,
[001], [002], [003]
FROM
YourTable
PIVOT
(
SUM(HOURS)
FOR VarID IN ([001], [002], [003])
)
AS YourPivot
ORDER BY
YourPivot.TaskID
The idea of a PIVOT table is a bit tricky to grok at first - maybe these additional sources of information will help:
The explanation from MSDN might shed some light:
You can use the PIVOT and UNPIVOT
relational operators to change a
table-valued expression into another
table. PIVOT rotates a table-valued
expression by turning the unique
values from one column in the
expression into multiple columns in
the output, and performs aggregations
where they are required on any
remaining column values that are
wanted in the final output. UNPIVOT
performs the opposite operation to
PIVOT by rotating columns of a
table-valued expression into column
values.
So you basically take a variable VarID
and turn its distinct values (001, 002, 003) and an aggregate over those (SUM(Hours)) into columns of a new "pivot" table.
Marc