September 1, 2009 at 3:13 am
How can i pivot table without using Pivot function as i have to pivot the string values. Pivot function works only if we have aggregates
Any idea ,sample code will be really helpful
Thanks in advance
Regards,
Motz
September 1, 2009 at 3:30 am
You can still use Min and Max on varchar columns, does that help ?
September 1, 2009 at 3:35 am
No, its not working, it shows null values
September 1, 2009 at 3:38 am
Please post DDL and sample data that shows your issue and i'll take a look
September 1, 2009 at 4:14 am
I have attached Create , Insert Query
expected output , thr can be more criteria and values
ID Criteria1 Value1 Criteria2 Value2
1 First Input ABC Second Input XYZ
2 N Input PQR Output KLM
Regards,
Motz
September 1, 2009 at 5:14 am
Try This
with cteTest(ID,Value,RowN,Coln)
as
(
Select ID,
case when Coln =1 then Criteria else Value end,
dense_rank() over (partition by ID order by Criteria),
Coln
from Test
cross join (Select 1 as Coln union Select 2) as Cart(Coln)
)
,
cteColTest(ID,Value,Coln)
as
(
Select Id,
value,
Coln = row_number() over(partition By id order by Rown , Coln)
from cteTest
)
Select ID,max([1]),max([2]),max([3]),max([4])
from cteColTest
pivot(
max(value)
for coln in([1],[2],[3],[4])
) as pvt
group by id
September 1, 2009 at 7:19 am
Thanks for the query
it works fine when criteria is different for a particluar ID , but doesnt work well if Criteria is same for particluarID
I have attached the inserts script, if u insert that and run the query it will give wrong result for ID 3
I am trying to modify that part, Dave if you know how to achive this please let me know
Regards,
Motz
September 1, 2009 at 7:49 am
Its because of the duplication of criteria screwing over the dense_rank()
this should sort it
with cteTestWithRow(ID,Criteria,Value,Rown)
as
(
Select Id,
Criteria,Value,row_number() over (Partition by id order by Criteria)
from test
)
,cteTest(ID,Value,RowN,Coln)
as
(
Select ID,
case when Coln =1 then Criteria else Value end,
dense_rank() over (partition by ID order by RowN),
Coln
from cteTestWithRow
cross join (Select 1 as Coln union Select 2) as Cart(Coln)
)
,
cteColTest(ID,Value,Coln)
as
(
Select Id,
value,
Coln = row_number() over(partition By id order by Rown , Coln)
from cteTest
)
Select ID,max([1]),max([2]),max([3]),max([4])
from cteColTest
pivot(
max(value)
for coln in([1],[2],[3],[4])
) as pvt
group by id
September 1, 2009 at 7:58 am
Thanks Dave
Now i am trying to create a view out of that but it throws error as below
Msg 4511, Level 16, State 1, Procedure vtest, Line 3
Create View or Function failed because no column name was specified for column 2.
Regards,
Motz
September 1, 2009 at 8:02 am
Specify the column names like this
Select ID,max([1]) as col1 ,max([2]) as col2 ,max([3]) as col3,max([4]) as col4
from cteColTest
September 1, 2009 at 8:10 am
I already added that part, was some other issue, did some minor mistake
Thanks a Lot Dave for your Help
Regards,
Motz
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply