两表
quote_item表  
主键:quote_num,quote_line
字段:qty_ordered  数量
      class_code   分类码
      reason_code  原因码
      
reason表
主键:reason_code  原因码
字段:name求SQL实现:---------------------------------------------
|分类码  |  数量  |A原因|B原因|C原因|D原因|...
---------------------------------------------并按数量倒排序

解决方案 »

  1.   

    select a.class_code,a.qty_ordered,b.name from quote_item a join reason b
    on a.reason_code=b.reason_code
    |A原因|B原因|C原因|D原因|...
    不知道你说的什么? 看看这样能否满足你的要求
      

  2.   

    ---------------------------------------------
    |分类码  |  数量  |A原因|B原因|C原因|D原因|...
    ---------------------------------------------的意思是:按分类码先分组:
    列出每种分类码的合计数量,
    再去找该分类码的所有原因,并列出该分类码每种原因码对应的数量。
      

  3.   

    A原因|B原因|C原因|D原因|...
    是reason表的纪录。quote_item表  的class_code   分类码
    字段可以对应多个原因码。
      

  4.   

    两表关系?quote_item表 VS reason表
    一VS多或者0
    一VS多
    多或者0VS一
    多VS一
      

  5.   

    ---------------------------------------------
    |分类码  |  数量  |A原因|B原因|C原因|D原因|...
    ---------------------------------------------
    |A        |  50     |10   |20   |15  |5    |
    |B        |  10     |2    |4    |3   |1    |
    ---------------------------------------------
    列标题应把原因表中的原因名称都列出来。如果A的C原因累计数量为空,则为零。
      

  6.   

    估计需要:declare @sql varchar(8000)
    set @sql=''
    select @sql=@sql+',sum(case when reason_code='''+reason_code+''' then qty_ordered else 0 end) as ['+name+']'
    from reason
    order by reason_codeexec('select class_code as 分类码,sum(qty_ordered) as 数量'+@sql+' from quote_item group by class_code order by sum(qty_ordered) desc')
      

  7.   

    估计需要:declare @sql varchar(8000)
    set @sql=''
    select @sql=@sql+',sum(case when reason_code='''+reason_code+''' then qty_ordered else 0 end) as ['+name+']'
    from reason
    order by reason_codeexec('select class_code as 分类码,sum(qty_ordered) as 数量'+@sql+' from quote_item group by class_code order by sum(qty_ordered) desc')
    ----------------------------------------------------------
    服务器: 消息 105,级别 15,状态 1,行 1
    字符串 '其它冲压部品              from quote_item group by class_code order by sum(qty_ordered) desc' 之前有未闭合的引号。
    服务器: 消息 170,级别 15,状态 1,行 1
    第 1 行: '其它冲压部品              from quote_item group by class_code order by sum(qty_ordered) desc' 附近有语法错误。
      

  8.   

    你是想纵向转横向,如果列数不确定外加还要排序,建议你在前台使用交叉表,而不是后台直接生成。如果横向列能固定数量,直接生成还多少有点道理。
    楼上的动态SQL就可以,不过还是尽量考虑前台处理吧
      

  9.   

    像类似楼主这类似的问题,用case when 语句可以处理,最好加上动太语句。。
    /*引用*/
    给你一个这方面的语句。
    1.包含两个表------典型行列转换问题例子 
    --建立测试环境
    create table tb1 (id nvarchar(10),type nvarchar(10))
    insert into tb1 select '11','a' union all select '22','b' union all select '33','c'
    create table tb2 (n int,type nvarchar(10),num int)
    insert into tb2 select '1','11','4' union all select '1','11','5' 
    union all select '2','22','8' union all select '3','22','5'
    --查询处理
    DECLARE @SQL VARCHAR(8000)
    SET @SQL='select n '
    SELECT @SQL= @SQL+',sum(case when type='+ttt+' then num else 0 end)['+tt+']' from
    (select distinct a.type as tt,isnull(b.type,'0') as ttt from tb2 b right join tb1 a on a.id=b.type) b
    set @sql=@sql+' from tb2 group by n'
    print @sql
    exec(@sql)
    go--删除测试环境
    Drop Table tb1,tb2