I am getting the following error message when I am trying to do replace null
to zero
.
The column name "jan" specified in the PIVOT operator conflicts with the existing column name in the PIVOT argument.
Query below:
select * from(select isnull(jan,0),isnull(feb,0),sum(data) as amount )as p
pivot(
sum(amount) for month in(jan,feb)) as piv
It's like the error says your column names are wrong because they will be read as un-named. Give them their appropriate names:
select isnull(Jan,0), isnull(feb,0) from(select sum(data) as amount, [month]
from yourtable group by [month] )as p
pivot(
sum(amount) for [month] in(jan,feb)) as piv
You could use CTE
to define your null values and then pivot the data something like this:
;WITH t
AS (
SELECT isnull(jan, 0) AS jan
,isnull(feb, 0) AS feb
,sum(data) AS amount
FROM your_table --change this to match your table name
GROUP BY jan,feb
)
SELECT *
FROM (
SELECT t.jan
,t.feb
,t.amount
FROM t
)
pivot(sum(amount) FOR [month] IN (
jan
,feb
)) AS piv
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With