I have a pivot query and it is returning null values in the output. I’ve been reading out MS documentation and I don’t get why I’m getting multiple rows in the pivoted output
The inner query is as follows:
<code>select r.Casino, wf.WebsiteValue, dsf.Name, wf.DataStoreFieldId
from Review r
inner join WebsiteField wf on wf.ReviewId = r.Id
inner join DataStoreField dsf on dsf.Id = wf.DataStoreFieldId
where dsf.Id in (42, 43, 163)
and WebsiteValue like '%USD%'
and Casino = 'Tusk Casino'
</code>
<code>select r.Casino, wf.WebsiteValue, dsf.Name, wf.DataStoreFieldId
from Review r
inner join WebsiteField wf on wf.ReviewId = r.Id
inner join DataStoreField dsf on dsf.Id = wf.DataStoreFieldId
where dsf.Id in (42, 43, 163)
and WebsiteValue like '%USD%'
and Casino = 'Tusk Casino'
</code>
select r.Casino, wf.WebsiteValue, dsf.Name, wf.DataStoreFieldId
from Review r
inner join WebsiteField wf on wf.ReviewId = r.Id
inner join DataStoreField dsf on dsf.Id = wf.DataStoreFieldId
where dsf.Id in (42, 43, 163)
and WebsiteValue like '%USD%'
and Casino = 'Tusk Casino'
and returns the following:
The pivot query is:
<code>select
Casino as 'Casino',
[42] as 'Withdrawal per Week',
[43] as 'Withdrawal per Month',
[163] as 'Withdrawal Per Transaction'
from
(select
r.Casino, wf.WebsiteValue, dsf.Name as FieldName, wf.DataStoreFieldId
from
Review r
inner join
WebsiteField wf on wf.ReviewId = r.Id
inner join
DataStoreField dsf on dsf.Id = wf.DataStoreFieldId
where
dsf.Id in (42, 43, 163)
and Casino = 'Tusk Casino'
and WebsiteValue like '%USD%') as source
pivot
(MAX(WebsiteValue)
FOR DataStoreFieldId IN ([42], [43], [163])
) AS PivotTable
where
Casino = 'Tusk Casino'
order by
Casino
</code>
<code>select
Casino as 'Casino',
[42] as 'Withdrawal per Week',
[43] as 'Withdrawal per Month',
[163] as 'Withdrawal Per Transaction'
from
(select
r.Casino, wf.WebsiteValue, dsf.Name as FieldName, wf.DataStoreFieldId
from
Review r
inner join
WebsiteField wf on wf.ReviewId = r.Id
inner join
DataStoreField dsf on dsf.Id = wf.DataStoreFieldId
where
dsf.Id in (42, 43, 163)
and Casino = 'Tusk Casino'
and WebsiteValue like '%USD%') as source
pivot
(MAX(WebsiteValue)
FOR DataStoreFieldId IN ([42], [43], [163])
) AS PivotTable
where
Casino = 'Tusk Casino'
order by
Casino
</code>
select
Casino as 'Casino',
[42] as 'Withdrawal per Week',
[43] as 'Withdrawal per Month',
[163] as 'Withdrawal Per Transaction'
from
(select
r.Casino, wf.WebsiteValue, dsf.Name as FieldName, wf.DataStoreFieldId
from
Review r
inner join
WebsiteField wf on wf.ReviewId = r.Id
inner join
DataStoreField dsf on dsf.Id = wf.DataStoreFieldId
where
dsf.Id in (42, 43, 163)
and Casino = 'Tusk Casino'
and WebsiteValue like '%USD%') as source
pivot
(MAX(WebsiteValue)
FOR DataStoreFieldId IN ([42], [43], [163])
) AS PivotTable
where
Casino = 'Tusk Casino'
order by
Casino
And this returns the following output:
How do I get rid of the NULLs and return the pivoted output as one row?