Hi there,
I'm using sql2005. I've got a table in tabular form and I need to convert it to crosstab format. I've tried to use pivot operator but i can't get the result i want. Because pivot operator needs to aggregate a field but what i need is Dim values as column names and DimCode as values within those columns as it is, no aggregations.
For example : This is tabular table.
And i want the end result as under..
Your help will much appreciated.
There is an excellent article about PIVOT by Peter Larsson.
Follow the link :
Pivot table for Microsoft SQL Server
Thanks
Naras.
|||For quite a few examples of using PIVOT, do a search on this Forum for the keyword 'Pivot'. Amazing how often this question is asked, and how often folks don't seem to bother looking in the archives to find their answer.
|||Thanks Naras,
I'm not looking for help in Pivot functionality of sql2005 as it won't be usefull for me. If you look at the first table, i want the first column Dim to be used for the new columns i.e. Dim1, Dim2, Dim3 will be columns in the crosstab result but then i want DimCode to appear under respective columns without any aggregations. In pivot you've to use aggregate function. Also in crosstab result if look at the record with jrnentry 54222, there are two lines with the same aaGLDistId i.e. i don't want to aggregate values within dim1, dim2 etc.
Thanks
Vivek
|||Vivek,
You are wrong. You can achive it using the PIVOT Operator. See the below query..
Code Snippet
Create Table #data (
[Dim] int ,
[DimCode] int ,
[JrnlEntry] int ,
[GLDistId] int ,
[TrxDate] datetime ,
[GLPostDT] datetime ,
[TrxType] int ,
[AccountIndex] int ,
[DebitAmt] int ,
[CreditAmt] int ,
[Amount] int
);
Set Dateformat DMY
Insert Into #data Values('4','2','54222','1','25/05/2007','25/05/2007','1','44256','617','0','617');
Insert Into #data Values('4','11991','54222','1','25/05/2007','25/05/2007','1','44256','617','0','617');
Insert Into #data Values('5','7','54222','1','25/05/2007','25/05/2007','1','44256','617','0','617');
Insert Into #data Values('5','5','54222','1','25/05/2007','25/05/2007','1','44256','617','0','617');
Insert Into #data Values('4','1','54225','1','31/05/2007','31/05/2007','1','44256','600','0','600');
Insert Into #data Values('5','5','54225','1','31/05/2007','31/05/2007','1','44256','600','0','600');
Insert Into #data Values('4','12821','54225','2','31/05/2007','31/05/2007','1','44256','400','0','400');
Insert Into #data Values('5','4','54225','2','31/05/2007','31/05/2007','1','44256','400','0','400');
Insert Into #data Values('4','3','54227','3','31/05/2007','31/05/2007','1','44256','0','500','-500');
Insert Into #data Values('5','8','54227','3','31/05/2007','31/05/2007','1','44256','0','500','-500');
Select
[JrnlEntry]
,[GLDistId]
,[TrxDate]
,[GLPostDT]
,[TrxType]
,Isnull([1],0) Dim1
,Isnull([2],0) Dim2
,Isnull([3],0) Dim3
,Isnull([4],0) Dim4
,Isnull([5],0) Dim5
,Isnull([6],0) Dim6
,Isnull([7],0) Dim7
,Isnull([8],0) Dim8
,Isnull([9],0) Dim9
,Isnull([10],0) Dim10
,[AccountIndex]
,[DebitAmt]
,[CreditAmt]
,[Amount] From
(
Select
[Dim]
,[DimCode]
,[GLDistId]
,[JrnlEntry]
,[TrxDate]
,[GLPostDT]
,[TrxType]
,[AccountIndex]
,[DebitAmt]
,[CreditAmt]
,[Amount]
,Row_Number() Over (Partition By [Dim],[GLDistId],[JrnlEntry] Order By [Dim],[GLDistId],[JrnlEntry]) RowId
From
#data
) as Data
Pivot
(
Max([DimCode])
For [Dim] in
(
[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]
)
) As PVT
|||Thanks Manivannan, much appreciated. Its returning correct result. I almost spent the whole day on this yesterday with frustration.
Regards,
Vivek
No comments:
Post a Comment