sum(case goodstype when '产品1'  then am else null end,0 ) as '产品1上午',sum(case goodstype when '产品1' then pm else null end,0) as '产品1下午'),");
我的意思是如果是产品1,那么am就为产品1上午,pm为产品1下午,上面这句是错的,请问我应该怎么写呢谢谢~~~~

解决方案 »

  1.   

    后面为什么有一个0sum(case when goodstype='产品1' then am else 0 end) as 产品1上午,
    sum(case when goodstype='产品1' then pm else 0 end) as 产品1下午, 
      

  2.   

    我的意思是如果是产品1,那么am就为产品1上午,pm为产品1下午,能不能AM,PM合在一起判断,比如是goodstype=产品1,那么AM为产品1上午,PM为产品2下午呢,谢谢
      

  3.   

    "sum(case when goodstype = '"+strth+"' then am else null end) as '" + rgth1 + "am',sum(case when goodstype = '" + strth + "' then pm else null end) as '" + rgth1 + "pm'," 
    提示)附近有错误,奇怪,求大家帮我更正下,谢谢
      

  4.   

    sum(case goodstype when '产品1'  then am else 0 end ) as '产品1上午'
    ,
    sum(case goodstype when '产品1'  then pm else 0 end ) as '产品1下午'
      

  5.   

    SELECT 
    sum(case goodstype when '产品1'  then am else 0 end ) as '产品1上午',
    sum(case goodstype when '产品1' then pm else 0 end) as '产品1下午' 
    FROM TB  
      

  6.   

    select sum(case goodstype when '产品1' then am else 0 end) as 产品1上午,
           sum(case goodstype when '产品1' then pm else 0 end) as 产品1下午
    然后,你需要这样做,as后的别名不能用','是用于值的:
    "sum(case when goodstype = '"+strth+"' then am else null end) as " + rgth1 + "am,sum(case when goodstype = '" + strth + "' then pm else null end) as " + rgth1 + "pm,"
      

  7.   

    另外,else null -> else 0,null在统计的时候,会影响统计结果,即1+null = null,所以这句最终应该这样:
    "sum(case when goodstype = '"+strth+"' then am else 0 end) as " + rgth1 + "am,sum(case when goodstype = '" + strth + "' then pm else 0 end) as " + rgth1 + "pm,"如果你的goodstype的类型为NChar/NVarchar,那么你的 ='"+strth+"' -> =N'"+strth+"',也就是变量的值前加个N。
      

  8.   


    create table tt
    ( [name] varchar(20),[type] varchar(50),[date] varchar(20),[am] int,[pm] int)
    insert into tt select 'A','产品1','2009-04-03',5,6
    union all select 'A','产品2','2009-04-03',4,1
    union all select 'A','产品3','2009-04-03',7,1
    union all select 'B','产品2','2009-04-03',3,1
    union all select 'B','产品1','2009-04-03',1,4
    select * from ttdeclare @sql varchar(5000)
    select @sql=isnull(@sql+',','')+'max(case when type='''+type+''' then am else 0 end) as ['+type+'上午],max(case when type='''+type+''' then pm else 0 end) as ['+type+'下午]'
    from tt group by type
    exec('select [name] 市场名称,'+@sql+',[date] 送货日期 from tt group by [name],[date]')--proc 
    create proc up_GetView
    as
    declare @sql varchar(5000)
    select @sql=isnull(@sql+',','')+'max(case when type='''+type+''' then am else 0 end) as ['+type+'上午],max(case when type='''+type+''' then pm else 0 end) as ['+type+'下午]'
    from tt group by type
    exec('select [name] 市场名称,'+@sql+',[date] 送货日期 from tt group by [name],[date]')--执行存储过程
    exec up_GetView
      

  9.   

    sum(case goodstype when '产品1'  then am else 0 end ) as '产品1上午' 

    sum(case goodstype when '产品1'  then pm else 0 end ) as '产品1下午'支持此解