日期:2014-05-16 浏览次数:20798 次
select * from cpjc_ls_1
select 部门,max(case when 盘点日期='2013-10-27' then 实盘数量 else '0' end) as '2013-10-27',
max(case when 盘点日期='2013-12-01' then 实盘数量 else '0' end) as '2013-12-01',
max(case when 盘点日期='2013-12-29' then 实盘数量 else '0' end) as '2013-12-29'
from cpjc_ls_1
group by 部门
declare @tsql varchar(6000)
select @tsql=isnull(@tsql+',','')
+'max(case when 盘点日期='''+pdrq+''' then 实盘数量 else ''0'' end) '''+pdrq+''' '
from (select distinct convert(varchar,盘点日期,23) 'pdrq'
from cpjc_ls_1) t
exec('select 部门,'+@tsql
+' from cpjc_ls_1
group by 部门 ')