How To Use Pivot In Sql Server 2005 Stored Procedure Joining Two Views
Solution 1:
You need to know all of the possible values to PIVOT by. So it is difficult to do this with T-SQL directly unless you use dynamic SQL and this can get hairy pretty quickly. Probably better to pass all of the rows back to the presentation tier or report writer and let it turn them sideways.
Here is a quick PIVOT example if you know all of the UBCategory values in advance. I left out ICCUDays since it seems rather irrelevant unless there are columns that come from that view as part of the result.
USE tempdb;
GO
SET NOCOUNT ON;
GO
-- who on earth is responsible for your naming scheme?CREATETABLE dbo.ICCUEnctrSelectedRevCatsDirCost
(
Account INT,
UBCategory VARCHAR(10),
DirectCost DECIMAL(9,2)
);
INSERT dbo.ICCUEnctrSelectedRevCatsDirCost
SELECT1, 'foo', 5.25UNIONSELECT1, 'bar', 6.25UNIONSELECT1, 'smudge', 8.50UNIONSELECT2, 'foo', 9.25UNIONSELECT2, 'brap', 2.75;
SELECT Account,[foo],[bar],[smudge],[brap] FROM
dbo.ICCUEnctrSelectedRevCatsDirCost
-- WHERE <something>, I assume ???
PIVOT
(
MAX(DirectCost)
FOR UBCategory IN ([foo],[bar],[smudge],[brap])
) AS p;
GO
DROPTABLE dbo.ICCUEnctrSelectedRevCatsDirCost;
To make this more dynamic, you'd have to get the comma separated list of DISTINCT UBCategory values, and build the pivot on the fly. So it might look like this:
USE tempdb;
GO
SET NOCOUNT ON;
GO
-- who on earth is responsible for your naming scheme?CREATETABLE dbo.ICCUEnctrSelectedRevCatsDirCost
(
Account INT,
UBCategory VARCHAR(10),
DirectCost DECIMAL(9,2)
);
INSERT dbo.ICCUEnctrSelectedRevCatsDirCost
SELECT1, 'foo', 5.25UNIONSELECT1, 'bar', 6.25UNIONSELECT1, 'smudge', 8.50UNIONSELECT2, 'foo', 9.25UNIONSELECT2, 'brap', 2.75UNIONSELECT3, 'bingo', 4.00;
DECLARE@sql NVARCHAR(MAX),
@col NVARCHAR(MAX);
SELECT@col=COALESCE(@col, '') + QUOTENAME(UBCategory) +','FROM
(
SELECTDISTINCT UBCategory
FROM dbo.ICCUEnctrSelectedRevCatsDirCost
) AS x;
SET@col=LEFT(@col, LEN(@col)-1);
SET@sql= N'SELECT Account, $col$ FROM
dbo.ICCUEnctrSelectedRevCatsDirCost
-- WHERE <something>, I assume ???
PIVOT
(
MAX(DirectCost)
FOR UBCategory IN ($col$)
) AS p;';
SET@sql= REPLACE(@sql, '$col$', @col);
--EXEC sp_executeSQL @sql;
PRINT @sql;
GO
DROPTABLE dbo.ICCUEnctrSelectedRevCatsDirCost;
Then to "send the data to a new table" you can just make the query an INSERT INTO ... SELECT instead of a straight SELECT. Of course, this seems kind of useless, because in order to write that insert statement, you need to know the order of the columns (which isn't guaranteed with this approach) and you need to have already put in columns for each potential UBCategory value anyway, so this seems very chicken and egg.
Post a Comment for "How To Use Pivot In Sql Server 2005 Stored Procedure Joining Two Views"