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 部门
想请问是否可以从cpjc_ls_1中获取'2013-10-27','2013-12-01','2013-12-29'这三个值,
不要手工指定或通过临时表循环赋值变量的方式.
麻烦高手了...........

------解决方案--------------------
用动态SQL实现.
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 部门 ')
------解决方案--------------------
可以,但是得生成动态的语句,才能实现你的需求