表a:TYPE   C20    C40    COTH   CNO    CWT
整箱    1       2      4      56     45000
散货    0       0      0      44     6780得到:整箱,则 1xC20,2xC40,4xCOTH
     散货,则  44件,6780公斤

解决方案 »

  1.   


    --这样?
    --> 测试数据: [A]
    if object_id('[A]') is not null drop table [A]
    create table [A] (TYPE varchar(4),C20 int,C40 int,COTH int,CNO int,CWT int)
    insert into [A]
    select '整箱',1,2,4,56,45000 union all
    select '散货',0,0,0,44,6780select type,des=
    case C20 when 0 then ltrim(CNO)+'件,'+ltrim(Cwt)+'公斤'
    else ltrim(c20)+'xC20,'+ltrim(C40)+'xC40,'+ltrim(COTH)+'xCOTH,' end
     from [A]
      

  2.   


    select type ,c20=case type when '整箱' then cast(c20 as varchar)+'*C20' else '0' end,
           c40=case type when '整箱' then cast(c40 as varchar)+'*C20' else '0' end,
     coth=case type when '整箱' then cast(coth as varchar)+'*C20' else '0' end,
     cno=case type when '散货' then cno else '0' end,
     cwt=case type when '散货' then cwt else '0' end
    from tb
                 
    整箱 1*C20 2*C20 4*C20 0 0
    散货 0 0 0 44 6780
    这样?
      

  3.   

    谢谢ls的!其实,最后在winform的Gridview里显示的是这样的形式:货物类型
    1xC20,2xC40,4xCOTH 
    44件,6780公斤
      

  4.   

    ls的,是这个意思:通过查询,把表中的数据显示在gridview的某一个单元格中,想把几个数据根据条件(type)重新组合合并一下,满足条件=整箱,显示:1xC20,2xC40,4xCOTH ,满足条件:散货,显示: 44件,6780公斤
      

  5.   


    --> --> (让你望见影子的墙)生成測試數據
     
    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([TYPE] nvarchar(2),[C20] int,[C40] int,[COTH] int,[CNO] int,[CWT] int)
    Insert tb
    select N'整箱',1,2,4,56,45000 union all
    select N'散货',0,0,0,44,6780
    Go
    Select * from tbselect type,显示=case type when '整箱' then cast(c20 as varchar)+'*C20,' +cast(c40 as varchar)+'*C20,' +cast(coth as varchar)+'*C20' 
       when '散货' then cast(cno as varchar)+'件,'+cast(cwt as varchar) +'公斤'else '0'
                     end
    from tb
    整箱 1*C20,2*C20,4*C20
    散货 44件,6780公斤这样?
      

  6.   


    SELECT 显示= CASE WHEN TYPE='整箱' THEN   
               COL_NAME(OBJECT_ID('你的数据库名称.表a'), 2)+'x'+RTRIM(CAST(C20 AS VARCHAR(10)))+','
               +COL_NAME(OBJECT_ID('你的数据库名称.表a'), 3)+'x'+ RTRIM(CAST(C40 AS VARCHAR(10)))+','
               +COL_NAME(OBJECT_ID('你的数据库名称.表a'), 4)+'x'+ RTRIM(CAST(COTH AS VARCHAR(10)))
                        
                      WHEN TYPE='散货' THEN   RTRIM(CAST(CNO AS VARCHAR(10)))+'件'+','
                            +RTRIM(CAST(CWT AS VARCHAR(10))) +'公斤'
                      ELSE '' END
    FROM 表a
        最后把  ‘显示‘ 这个字段绑定在 Gridview 上。
      

  7.   

    select type,case when type='整箱' then 1*c20+2*c40+4*coth else cno end cno,cwt from a
      

  8.   

    谢谢楼上几位,可以用!另外,如果遇到数字是0的,可以不显示吗?现在的例子是  1*C20,2*C40,4*Coth,如果遇到  1*C20,0*C40,0*Coth 可以显示 1*C20 吗?