Solved

Replace Null Cells in Pivot Table

Posted on 2007-03-19
1
493 Views
Last Modified: 2008-03-06
Need assistance with a pivot table format issue. I have the query below that returns a pivot table I export to Access for a end user. I need to replace the 'null' cells in the "week_end_date" columns with either a blank or 0 before I export to Access.  Here is my SQL:

/*Active items */
select distinct  i.upc,i.description,i.item_size,i.item_uom
into #i
from dim_item i join dim_item_client ic on i.client_key = ic.client_key
where datediff(dd, i.start_date,  '2/24/2007') >= 0
and datediff(dd, i.end_date,  '9/2/2006') <= 0
and ic.client_key = 24391
and i.active = 1
and i.country_id = 1
create clustered index [ix_#i] on #i ([upc] asc) on [primary]

/* Active Locations */
select distinct s.customer_id, s.fulldescription, h.description as holding,
d.description as division, b.description as banner, sb.description as subbanner
into #s
from  vw_store s
join subbanner sb on s.subbanner_key = sb.subbanner_key
join banner b on sb.banner_key = b.banner_key
join division d on b.division_key = d.division_key
join holding h on d.holding_key = h.holding_key
where datediff(dd, s.start_date,  '2/24/2007') >= 0
and datediff(dd, s.end_date,  '9/2/2006') <= 0
and s.active = 1
and s.country_id = 1
and b.channel_key = 80
create clustered index [ix_#s] on #s ([customer_id] asc) on [primary]

/*Week ending dates since September 2006 */
select distinct calendar_key, week_end_date
into #w
from calendar
where week_end_date between '9/2/2006' and '2/24/2007'
create clustered index [ix_#w] on #w ([calendar_key] asc) on [primary]

/*Scans for Active,items at Active, US, Locations*/  
select s.holding, s.division, s.banner, s.subbanner, s.fulldescription, i.upc, i.description, i.item_size, i.item_uom, w.week_end_date,
sum(f.pos_quantity) as scanned
into #f
from sales f
join store ds on f.store_key = ds.store_key
join item di on f.item_key = di.item_key and di.country_id = 1
join #w w on f.calendar_key = w.calendar_key
join #s s on ds.customer_id = s.customer_id
join #i i on di.upc = i.upc
where (pos_quantity > 0 or pos_ext_price > 0)
group by s.holding, s.division, s.banner, s.subbanner, s.fulldescription, i.upc, i.description, i.item_size, i.item_uom, w.week_end_date


/*Pivot Table Output*/
declare @pivot varchar(max), @sql varchar(max)  
set @pivot = ' '      select @pivot = @pivot + '[' + CONVERT(VARCHAR(10), week_end_date, 101) + '],'
                                          from (select distinct week_end_date from #w) C order by week_end_date
set @pivot = left(@pivot, len(@pivot) - 1)
set @sql =
'select * from #f as A
pivot (sum(scanned) for week_end_date in ( ' + @pivot + ' ) ) as B
order by 1,2,3,4,5,6,7,10'
EXEC (@sql)
0
Comment
Question by:kcmoore
1 Comment
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
Comment Utility
use
coalesce( sum(yourcolumnname),0) as yourcolumnname
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now