CREATE PROCEDURE shourutongji_1
@louceng varchar(20),@qiriqi as varchar(10),@zhiriqi as varchar(10)
AS
declare @sql varchar(8000)
set @sql = 'select distinct fk_zwid as 展位编号,(select zw_name from zwzlb where zw_id=fk_zwid) as 展位名称,sum(fk_cuxiaofei) as 促销费,sum(fk_shuakafei) as 刷卡费'
select @sql = @sql + ' ,isnull((select sum(kk_bencikoukuan) from kkb where kk_zwid=fk_zwid and convert(char(10),kk_date,120)=convert(char(10),fk_date,120) and kk_xiangmu=''' + kk_xiangmu + '''),0) [ ' + kk_xiangmu + '] '
from (select distinct kk_xiangmu from kkb) as a
set @sql = @sql + ' from fkb where convert(char(10),fk_date,120)>=''' + @qiriqi + ''' and convert(char(10),fk_date,120)<=''' + @zhiriqi + '''
'
if @louceng is null
set @sql = @sql + ' group by fk_zwid,convert(char(10),fk_date,120) order by fk_zwid'
else
set @sql = @sql + '
and fk_zwid in (select zw_id from zwzlb where zw_lc=''' + @louceng + ''') group by fk_zwid,convert(char(10),fk_date,120) order by fk_zwid'
exec(@sql)
GO