--行转列时不能使用固定记录,动态生成create table test_pgd1 --派工单测试表
(
pd_date datetime,--派工日期
kh_name varchar(50),--客户姓名
cx varchar(50),--车型
ht_bh varchar(50),--合同编号
zzbw varchar(50),--制作部位
zzbw_mx varchar(50),--制作部位明细
sj int,--数量
gsde decimal(13,2),--工时定额
sjwgsi datetime,--实际完工时间
zzr varchar(50),--制作人
zzrgsf decimal(13,2),--制作人工时费
gsf decimal(13,2),--合计工时费
xz varchar(50),-- 小组
pgbh varchar(50),--派工单编号
type varchar(50),--职工类别
flags varchar(50),--业务类别
xlnr varchar(50)--修理内容
)
insert into test_pgd1 select '2007-7-18 11:28:00','苏良良','栏板半挂车','HTZZ-52',null,null,1,null,'','00001',5,null,'1组','HTZZ-52','项目负责人','改装/修理','更换备胎'
insert into test_pgd1 select '2007-7-18 11:28:00','苏良良','栏板半挂车','HTZZ-52',null,null,1,null,'','00002',5,null,'1组','HTZZ-52','制作人','改装/修理','制作脚架'
insert into test_pgd1 select '2007-7-18 11:28:00','苏良良','栏板半挂车','HTZZ-52',null,null,1,null,'','00003',5,null,'1组','HTZZ-52','制作人','改装/修理','aaa'
insert into test_pgd1 select '2007-7-18 11:28:00','苏良良','栏板半挂车','HTZZ-52',null,null,1,null,'','00004',10,null,'1组','HTZZ-52','项目负责人','改装/修理','bbb'
insert into test_pgd1 select '2007-7-18 11:28:00','苏良良','栏板半挂车','HTZZ-52',null,null,1,null,'','00005',20,80,'2组','HTZZ-52','制作人','制作车',null
select * from test_pgd1
GO
/*
要得到这样的工时统计表(按照kh_name,cx,ht_bh分组)kh_name         cx            ht_bh      更换备胎(费用)   制作脚架(费用)    aaa(费用)    bbb(费用)    合计    pd_date
苏良良       栏板半挂车       HTZZ-52       5                5               5           10          25     2007-07-18          
*/
drop table test_pgd1

解决方案 »

  1.   

    贴下以前的贴的URL好参考
      

  2.   

    declare @sql varchar(8000)
    set @sql=''
    select @sql=@sql+',sum(case when xlnr='''+xlnr+''' then zzrgsf else 0 end) as ['+xlnr+'(费用)]'
    from test_pgd1 
    group by xlnrexec ('select kh_name,cx,ht_bh'+@sql+',sum(zzrgsf) as 合计,max(pd_date) as pd_date from test_pgd1 group by kh_name,cx,ht_bh')
      

  3.   

    kh_name                                            cx                                                 ht_bh                                              合计                                      pd_date
    -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------------------------------------- -----------------------
    苏良良                                                栏板半挂车                                              HTZZ-52                                            45.00                                   2007-07-18 11:28:00.000(1 row(s) affected)
      

  4.   

    结果不对:苏良良 栏板半挂车 HTZZ-52 45.00 2007-07-18 11:28:00.000----------------------------------------
    应该是:kh_name         cx            ht_bh      更换备胎(费用)   制作脚架(费用)    aaa(费用)    bbb(费用)    合计    pd_date
    苏良良       栏板半挂车       HTZZ-52       5                5               5           10          25     2007-07-18
      

  5.   

    那用  Haiwer(海阔天空)   的方法就行了,把  ,sum(zzrgsf) as 合计  這一段去掉
      

  6.   

    应该是:kh_name         cx            ht_bh      更换备胎(费用)   制作脚架(费用)    aaa(费用)    bbb(费用)    合计    pd_date
    苏良良       栏板半挂车       HTZZ-52       5                5               5           10          25     2007-07-18
    ------------------------------------
    行转列没有转过去。
      

  7.   

    declare @s varchar(8000)
    set @s = ''
    select @s = isnull(@s, '') + ', sum(case xlnr when ''' + xlnr +
    ''' then zzrgsf else 0 end) as [' + xlnr + '(费用)]'
    from test_pgd1 
    group by xlnrset @s = 'select kh_name, cx, ht_bh' + @s + ', max(pd_date) as pd_date' +
    ' from test_pgd1 group by kh_name, cx, ht_bh'exec(@s)
      

  8.   

    ,sum(zzrgsf) as 合计 
    为什么要去掉?
      

  9.   

    --貌似楼主未说清楚,不需要xlnr是空的记录declare @sql varchar(8000)
    set @sql=''
    select @sql=@sql+',sum(case when xlnr='''+xlnr+''' then zzrgsf else 0 end) as ['+xlnr+'(费用)]'
    from test_pgd1 
    where xlnr is not null
    group by xlnrexec ('select kh_name,cx,ht_bh'+@sql+',sum(zzrgsf) as 合计,max(pd_date) as pd_date from test_pgd1 where xlnr is not null group by kh_name,cx,ht_bh')
      

  10.   

    对不起,下午断网了
    declare @sql varchar(8000)
    set @sql=''
    select @sql=@sql+',sum(case when xlnr='''+xlnr+''' then zzrgsf else 0 end) as ['+xlnr+'(费用)]'
    from test_pgd1 
    where xlnr is not null
    group by xlnrexec ('select kh_name,cx,ht_bh'+@sql+',sum(zzrgsf) as 合计,max(pd_date) as pd_date from test_pgd1 where xlnr is not null group by kh_name,cx,ht_bh')
    ---------------------------------------
    要是以pd_date为查询条件的话,应该把where语句写在哪里