Click here to Skip to main content
15,922,419 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
date	18 	20	50	909 --headers
2015-03-30 	20	153	92	41
2015-03-31 	20	153	93	41
2015-02-19 	571	160	92  NULL
2015-04-06 	52	155	95	42
2015-04-07 	80	155	95	42


out put should be like this
date	18 	20	50	909 --headers
2015-03-30 	20	153	92	41
2015-03-31 	20	153	93	41
2015-02-19 	571	160	92  NULL
2015-04-06 	52	155	95	42
2015-04-07 	80	155	95	42
Posted
Updated 24-Aug-15 23:27pm
v3
Comments
Wendelius 25-Aug-15 4:47am    
What is the query you have?
Member 11844168 25-Aug-15 4:58am    
declare @fromDate varchar(20)
declare @ToDate varchar(20)
declare @ymd varchar(2)
declare @num smallint
set @ymd='m'
set @num=10

if @ymd<>''
BEGIN
set @ToDate= convert (varchar(20),(select MAX(navdate) from mutual.dbo.latnav),106)
if @ymd ='y'
set @fromdate= convert (varchar(20),DATEADD(year,-@num,@ToDate),106)
else if @ymd='m'
set @fromdate= convert (varchar(20),DATEADD(MONTH,-@num,@ToDate),106)
else if @ymd='d'
set @fromDate= convert (varchar(20),DATEADD(day,-@num,@ToDate),106)
END

begin

exec(
'select *
from
(
select s.code,p.date,p.rs from Price as p
inner join SCH as s
on s.CODE=p.CODE
where s.CODE in (18,20,50,909)
and p.date BETWEEN ' +' '''+@fromdate+''' '+ ' and '+' '''+@todate+''' '+ '


) dataTable
pivot
(
sum(rs)
for code in ( [18],[20],[50],[909] )
) as pivotTable
order by 2'


)


end

Hi,

As i understand you are doing a select * in the pivot table.
so,

Can you select like .. select * [your pivot table] t where [t.requiredcolumn] is not null.

something like,

SQL
select * from
 ( dataTable1
pivot 
datatable2)

 as pivotTable where reqcol is not null

order by 2



Hope this helps.


Happy coding!
 
Share this answer
 
v3
Comments
Andy Lanng 25-Aug-15 5:17am    
psst. Use square brackets or they'll be inserted as markup :P
manognya kota 25-Aug-15 5:54am    
i have used <> to indicate generic way. Thanks for updating :)
Member 11844168 25-Aug-15 5:18am    
this will not work i have tried it
Andy Lanng 25-Aug-15 5:29am    
have you tried it this way:
pivot
(
sum(rs)
for code in ( [18],[20],[50],[909] )
) as pivotTable
where [909] is not null
order by 2'
Member 11844168 25-Aug-15 6:03am    
thank you :-)
A source query should looks like this:
SQL
select s.code,p.date,p.rs from Price as p
inner join SCH as s
on s.CODE=p.CODE
where s.CODE in (18,20,50,909) AND NOT p.rs IS NULL
and p.date BETWEEN ' +' '''+@fromdate+''' '+ ' and '+' '''+@todate+''' '+ '


[EDIT]
If sourcce table does not contain NULL values, you have to filter pivot table:

SQL
SELECT pt.<Fields>
FROM (
    -- source query
) AS st
PIVOT (...) AS pt
WHERE NOT pt.[909] IS NULL


Another way is to replace NULLs with default values via using COALESCE function[^]:

SQL
SELECT COALESCE(pt.[18], 0) AS [18] -- and so on
 
Share this answer
 
v3
Comments
Member 11844168 25-Aug-15 5:52am    
i did the same thing..this will not work because dataTable does't have null value...
Member 11844168 25-Aug-15 5:54am    
null value is introduce while pivoting the table
Maciej Los 25-Aug-15 6:05am    
OK. So, you have to use COALESCE function to replace NULLs with zero (for example).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900