第一排序width,第二排序字母这个不太明白

解决方案 »

  1.   

    而且,按width排序好像有些问题吧?例如,第一和第二条记录均有a,按width排序的时候,a应该使用那个width?
      

  2.   

    --看错了,是下面这个意思么?
    --测试数据
    create table [table](number int,s1 char(1),s2 char(1),s3 char(1),s4 char(1),s5 char(1),quantity int,width int)
    insert [table]
    select 1,'a','b','s','o' ,'a' ,100,1200 union all
    select 2,'8','c','c','a' ,'5' ,200,1500 union all
    select 3,'9','5','9',null,null,150,1200 union all
    select 4,'b','s','b','5' ,'1' ,150,1600
    go--统计
    select width,s1=case 
    when s1 between '0' and '9' then '[0-9]'
    when s1 between 'a' and 'z' then '[a-z]'
    else '[other]' end,
    quantity=sum(quantity)
    from(
    select s1,quantity,width from [table]
    union all
    select s2,quantity,width from [table]
    union all
    select s3,quantity,width from [table]
    union all
    select s4,quantity,width from [table]
    union all
    select s5,quantity,width from [table]
    )a group by width,case 
    when s1 between '0' and '9' then '[0-9]'
    when s1 between 'a' and 'z' then '[a-z]'
    else '[other]' end
    order by width,s1
    go--删除测试
    drop table [table]/*--结果width       s1      quantity    
    ----------- ------- ----------- 
    1200        [0-9]   450
    1200        [a-z]   500
    1200        [other] 300
    1500        [0-9]   400
    1500        [a-z]   600
    1600        [0-9]   300
    1600        [a-z]   450(所影响的行数为 7 行)
    --*/
      

  3.   

    不是啊 老大 !
    我要的结果是:以b 为例width       s1      quantity    1200        1        .....
    ....        ...      .....  
    1200        b        100
    ....        ...      .....
    1200        z        .....
    ....        ...      .....
    1600        1        .....
    ....        ...      .....
    1600        b        300
    ....        ...      ....
      

  4.   

    就是说先以width 列分类且排序,又在每个width中 按字母分类且排序
      

  5.   

    --测试数据
    create table [table](number int,s1 char(1),s2 char(1),s3 char(1),s4 char(1),s5 char(1),quantity int,width int)
    insert [table]
    select 1,'a','b','s','o' ,'a' ,100,1200 union all
    select 2,'8','c','c','a' ,'5' ,200,1500 union all
    select 3,'9','5','9',null,null,150,1200 union all
    select 4,'b','s','b','5' ,'1' ,150,1600
    go--统计
    select width,s1,
    quantity=sum(quantity)
    from(
    select s1,quantity,width from [table]
    union all
    select s2,quantity,width from [table]
    union all
    select s3,quantity,width from [table]
    union all
    select s4,quantity,width from [table]
    union all
    select s5,quantity,width from [table]
    )a group by width,s1
    order by width,s1
    go--删除测试
    drop table [table]/*--结果width       s1   quantity    
    ----------- ---- ----------- 
    1200        NULL 300
    1200        5    150
    1200        9    300
    1200        a    200
    1200        b    100
    1200        o    100
    1200        s    100
    1500        5    200
    1500        8    200
    1500        a    200
    1500        c    400
    1600        1    150
    1600        5    150
    1600        b    300
    1600        s    150(所影响的行数为 15 行)
    --*/