sql server - Pivot with datediff -
how remove zeros , replace them null values considering not 0 value?
my working data
id class startdate enddate 1 high 1/1/15 2/1/15 1 low 5/1/15 6/1/15 1 mid 6/1/15 6/10/15 2 mid-low 6/1/15 6/10/15
my query below
select y.id, y.startdate, y.enddate, isnull(y.high,'') high, isnull(y.mid,'') mid, isnull(y.low,'') low, isnull(y.mid-low,'') mid-low, ( select id, class, datediff(day, startdate, enddate) days @test ) x pivot ( max(days) class in (high, low, med) ) y
this returns set value below
id high mid low mid-low 1 30 9 30 0 2 0 0 0 30
having post answer show code. unable reproduce op's results.
this code:
declare @table table ( cola char(1) ,colb int ); insert @table(cola,colb) values ('a',null); select * ( select cola,colb @table )x pivot ( sum(colb) cola in (a,b) --same result if sum() changed max() ) y;
results in:
a b null null
so op doing more example illustrates getting zeros in his/her results.
Comments
Post a Comment