create table work
(
worker char(4),
worktime decimal,
workmonth decimal,
type char(20)
)create table chanpin
(
type char(20),
cpname char(60)
)insert into work values('0001', 11, '1', 'c01')
insert into work values('0001', 11, '1', 'c02')
insert into work values('0001', 11, '2', 'c02')
insert into work values('0001', 12, '1', 'c02')
insert into work values('0001', 12, '2', 'c02')
insert into work values('0002', 13, '1', 'c02')
insert into work values('0002', 14, '1', 'c03')
insert into work values('0003', 15, '1', 'c03')insert into chanpin values('c01', 'cc0011')
insert into chanpin values('c02', 'cc0022')
insert into chanpin values('c03', 'cc0033')
select a.workerg, a.workmonth, a.ztime, cp1.worktime as cp1, cp2.worktime as cp2, cp3.worktime as cp3  from (
select worker as workerg, workmonth as workmonth, sum(worktime) as ztime
from work
group by worker, workmonth
) a
left join 
(select work.worker, work.workmonth, work.worktime, chanpin.cpname from work, chanpin where work.type = chanpin.type and work.type = 'c01') cp1
on a.workerg = cp1.worker and a.workmonth = cp1.workmonth
left join 
(select work.worker, work.workmonth, work.worktime, chanpin.cpname from work, chanpin where work.type = chanpin.type and work.type = 'c02') cp2
on a.workerg = cp2.worker and a.workmonth = cp2.workmonth
left join 
(select work.worker, work.workmonth, work.worktime, chanpin.cpname from work, chanpin where work.type = chanpin.type and work.type = 'c03') cp3
on a.workerg = cp3.worker and a.workmonth = cp3.workmonth

解决方案 »

  1.   

    结果忘了写
    workerg  workermonth ztime  cp1     cp2       cp3
    0001 1 34 11 12 NULL
    0001 1 34 11 11 NULL
    0002 1 27 NULL 13 14
    0003 1 15 NULL NULL 15
    0001 2 23 NULL 11 NULL
    0001 2 23 NULL 12 NULL
      

  2.   

    谢谢您的答复.我问的问题可能跟您说的还不太一样,产品的记录是从产品表查出来的,然后再作为最后统计表的列,您的查询似乎已经知道产品类型的具体数目了.如果按照您的查询,那产品表的记录添加后整个SQL查询都要改动了:_(
      

  3.   

    --借用楼上的数据
    create table work(worker varchar(4),worktime decimal,workmonth decimal,type varchar(20))
    create table chanpin(type varchar(20),cpname varchar(60))
    insert into work values('0001', 11, '1', 'c01')
    insert into work values('0001', 11, '1', 'c02')
    insert into work values('0001', 11, '2', 'c02')
    insert into work values('0001', 12, '1', 'c02')
    insert into work values('0001', 12, '2', 'c02')
    insert into work values('0002', 13, '1', 'c02')
    insert into work values('0002', 14, '1', 'c03')
    insert into work values('0003', 15, '1', 'c03')insert into chanpin values('c01', 'cc0011')
    insert into chanpin values('c02', 'cc0022')
    insert into chanpin values('c03', 'cc0033')--执行动态交叉表查询
    declare @s varchar(8000),@worker varchar(4)
    set @s = ''
    set @worker = '0001'select 
        @s=@s+',['+cpname+']=sum(case type when '''+rtrim(type)+''' then worktime else 0 end)'
    from 
        chanpin 
    group by
        cpname,typeset @s = 'select workmonth,worktime=sum(worktime)'+@s+' from work where worker='''+@worker+''' group by workmonth'
    exec(@s)
    go--输出结果
    /*
    1 34 11 23 0
    2 23 0 23 0
    */--删除测试数据
    drop table work,chanpin
    go
      

  4.   

    libin_ftsafe的回答应该是正确的,高手就是高手
      

  5.   

    libin_ftsafe,请问您能解释一下吗?我不太明白这个存储过程的意思,我验证了一下,确实是我所需要的结果.
      

  6.   

    libin_ftsafe,能解释一下这个SQL语句的意思吗?因为还要模仿这个句子写其他的查询,所以需要明白其中的精妙之处,请指导,谢谢了.