34,590
社区成员
发帖
与我相关
我的任务
分享
create procedure [dbo].[tj_tjjb]
@tjh varchar(20),
@bitxb bit,
@tjfs varchar(50),
@dtstart varchar(10),
@dtend varchar(10)
as
/*
declare @tjh varchar(20)
declare @bitxb bit
declare @tjfs varchar(50)
declare @dtstart varchar(10)
declare @dtend varchar(10)
*/
declare @sql varchar(8000)
declare @tj varchar(100)
BEGIN
--select @tjfs = 'B'
create table #tmpjb(zd varchar(500),xb varchar(20),bs varchar(100),sl int)
/*是否性别分组
*/
--select @tj = ''
if @bitxb = 1
begin
select @tj = ' a.xb, '
end
/**/
select @sql = isnull(@sql + ',' , '') + '[' + bs + ']' from tj_tjfs where name = @tjfs group by bs
if @tjh <> ''
begin
insert into #tmpjb (zd,xb,bs,sl)select a.zd,c.xb,d.bs,1 as sl
from tj_grjb a, tj_lsb b, tjry c,tj_tjfs d
where b.tjh = @tjh and d.name = @tjfs and a.lsbid = b.id and b.dah = c.dah and (c.nl between d.nlxx and d.nlsx)
end
else
begin
insert into #tmpjb (zd,xb,bs,sl)select a.zd,c.xb,d.bs,1 as sl
from tj_grjb a, tj_lsb b, tjry c,tj_tjfs d
where (convert(varchar(10),zjrq,120) between @dtstart and @dtend) and d.name = @tjfs and a.lsbid = b.id and b.dah = c.dah and (c.nl between d.nlxx and d.nlsx)
end
exec('select b.zd, '+ @tjh + @sql +' from (select * from #tmpjb) a pivot (count(sl) for bs in (' + @sql + '))b')
drop table #tmpjb
end --这里少个END