SQL 报表求助
表结构和需要的结果如下图,求高人指点
------解决方案--------------------1. 需要的结果DPD15,DPD16,DPD17是 固定死的列?
是不是可以理解 为DiaClient.DPD 字段值 只有 15,16,17 ?
2. DiaTask,DiaBatch表好像不会用到哟!
另:希望能给出 表结构的 创建及初始化语句脚本。
------解决方案--------------------我将表简化了下,只写出了total的
create table #A(taskid varchar(2),clientid varchar(2),agent varchar(20))
insert into #A
select 'T1','C1','Agent01' union all select 'T1','C2','Agent02' union all
select 'T1','C3','Agent02' union all select 'T1','C4','Agent01'
create table #B(clientid varchar(10),dpd varchar(5))
insert into #B
select 'C1','15' union all select 'C2','16' union all
select 'C3','15' union all select 'C4','17'
create table #C (Clientid varchar(3),[datetime] varchar(20) )
insert into #C
select 'C1','2013-12-4' union all select 'C2','2013-12-4'
union all select 'C3','2013-12-4' union all select 'C4','2013-12-4'
declare @s varchar(max)
set @s=''
select @s=@s+','+Quotename('DPD'+dpd)+'=sum(case when dpd='+quotename(dpd,'''')+' then 1 else 0 end)' from #B group by dpd
set @s='select [datetime],agent'+@s+' from #A a,#B b,#C c where a.clientid=b.clientid and a.clientid=c.clientid group by [datetime],agent
union all
select ''Total'','''''+@s+' from #A a,#B b,#C c where a.clientid=b.clientid and a.clientid=c.clientid'
exec(@s)
------解决方案--------------------
create table DiaTaskClient
(taskid varchar(10),
clientid varchar(10),
agent varchar(10),
caseresult varchar(10))
create table DiaClient
(clientid varchar(10),
batchid varchar(10),
DPD int)
create table DiaTrial
(taskid varchar(10),
clientid varchar(10),
[datetime] varchar(15))
insert into DiaTaskClient
select 'T1','C1','agent01','成功扣款' union all
select 'T1','C2','agent02','成功扣款' union all
select 'T1','C3','agent02','成功扣款' union all
select 'T1