select 型号,数量,价格
from (
select 型号,数量,价格
from 基本表
union all
select left(型号,1) as 型号,null as 数量,sum(价格) as 价格
from 基本表
group by left(型号,1)
) as t
order by 型号

解决方案 »

  1.   

    create table mm(型号 varchar(10),数量 int ,价格  int)
    insert mm select  'A1' ,  10 ,  100
    union all select  'A2' ,  20 ,  200
    union all select  'A3' ,  30 ,  300union all select  'B1' ,  10 , 1000
    union all select  'B2' ,  20 , 2000
    union all select  'B3' ,  30 , 3000select 型号,数量,价格 
    from (
    select 型号,数量,价格 from mm
    union all
    select 型号=left(型号,1)  , 数量=null , 价格=sum(价格) from mm
    group by left(型号,1)
    ) #t
    order by 型号
      

  2.   

    select 型号,数量,价格 
    from (
    select 型号,数量,价格 from mm
    union all
    select 型号=left(型号,1)  , 数量=null , 价格=sum(价格) from mm
    group by left(型号,1)
    ) #t
    order by 型号
    同意!
      

  3.   

    select a.型号,a.数量,a.价格
    from (select 型号,数量,价格 from 基本表
    union all select left(型号,1) as 型号,null as 数量,sum(价格) as 价格 from 基本表
    group by left(型号,1)
    )a order by a.型号
      

  4.   

    select * from 
    (select left(型号,1) as 型号,sum(价格) as 价格 from 基本表 group by left(型号,1)) ii 
    union all
    select * from 基本表
      

  5.   

    select * from 
    (select left(型号,1) as 型号,sum(价格) as 价格 from 基本表 group by left(型号,1)) ii 
    union all
    select * from 基本表 order by 型号
      

  6.   

    Select left(型号,1) as 型号,'' as 数量,sum(价格) as 价格 
     from mm group by left(型号,1)
    union all
    select 型号,cast(数量 as char(5)),价格 from mm 
    Order by 型号
      

  7.   

    用UNION 好象产生的结果是这样的:型号 数量 价格
    A         600
    B         6000  〈〈 这里不对
    A1   10   100
    A2   20   200
    A3   30   300
                   〈〈 B要在这里
    B1   10   1000
    B2   20   2000
    B3   30   3000
      

  8.   

    测试如下:
    create table mm(型号 varchar(10),数量 int ,价格  int)
    insert mm select  'A1' ,  10 ,  100
    union all select  'A2' ,  20 ,  200
    union all select  'A3' ,  30 ,  300union all select  'B1' ,  10 , 1000
    union all select  'B2' ,  20 , 2000
    union all select  'B3' ,  30 , 3000select left(型号,1) as 价格 ,'' as 数量,sum(价格) as 价格 from mm group by left(型号,1)
    union
    select 型号,cast(数量 as varchar(5)),价格 from mmdrop table mm结果:
    型号     数量     价格
    A 600
    A1 10 100
    A2 20 200
    A3 30 300
    B 6000
    B1 10 1000
    B2 20 2000
    B3 30 3000
      

  9.   

    to  gaodongsheng(东升) 
    能否不建立临时表?
      

  10.   

    select 型号,价格,数量 
    from table
    union 
    select left(型号,1),null,sum(isnull(数量,0))
    from table
    group by left(型号,1)
    order by 型号
      

  11.   

    先创建mm表:(略)查询:
    select left(型号,1) as 型号 ,'' as 数量,sum(价格) as 价格 from mm group by left(型号,1)
    union
    select 型号,cast(数量 as varchar(5)),价格 from mm
    Order by 型号
      

  12.   

    to  wendyong(小文) select 型号,价格,数量 
    from mm
    union 
    select left(型号,1),null,sum(isnull(数量,0))
    from mm
    group by left(型号,1)
    order by 型号测试结果如下:
    型号     数量     价格
    A NULL 60
    A1 100 10
    A2 200 20
    A3 300 30
    B NULL 60
    B1 1000 10
    B2 2000 20
    B3 3000 30
      

  13.   

    --你可以试试以下的结果:可能对你有用
    create table mm(型号 varchar(10),数量 int ,价格  int)
    insert mm select  'A1' ,  10 ,  100
    union all select  'A2' ,  20 ,  200
    union all select  'A3' ,  30 ,  300union all select  'B1' ,  10 , 1000
    union all select  'B2' ,  20 , 2000
    union all select  'B3' ,  30 , 3000
     select 型号,数量,substring(型号,1,1) as a,价格 into #tttt from mm  select (
             case when Grouping(型号)=1 and Grouping(a)=0 then '合计'
                  when Grouping(a)=1 and Grouping(型号)=1 then '总计' 
                  else 型号 
             end) as 型号,
             --数量,
             sum(价格) as 价格 from #tttt group by a,型号 WITH ROLLUP
     having Grouping(型号)=1 or Grouping(a)=0  drop table #tttt
     drop table mm
      

  14.   

    select 型号=case grouping(型号) when 0 then 型号
    else left(型号,patindex('%[0-9]%',型号+'0')-1) end
    ,数量=sum(数量),价格=sum(价格)
    from 基本表
    group by left(型号,patindex('%[0-9]%',型号+'0')-1)
    ,型号 with rollup
    having grouping(left(型号,patindex('%[0-9]%',型号+'0')-1))=0
    order by 型号
      

  15.   

    --漏了数量的处理,改一下:select 型号=case grouping(型号) when 0 then 型号
    else left(型号,patindex('%[0-9]%',型号+'0')-1) end
    ,数量=case grouping(型号) when 1 then ''
    else cast(sum(数量) as varchar) end
    ,价格=sum(价格)
    from 基本表
    group by left(型号,patindex('%[0-9]%',型号+'0')-1)
    ,型号 with rollup
    having grouping(left(型号,patindex('%[0-9]%',型号+'0')-1))=0
    order by 型号
      

  16.   

    --示例--示例数据
    create table 基本表(型号 varchar(10),数量 int,价格 int)
    insert 基本表 select 'A1',10,100
    union  all    select 'A2',20,200
    union  all    select 'A3',30,300
    union  all    select 'B1',10,1000
    union  all    select 'B2',20,2000
    union  all    select 'B3',30,3000
    go--查询处理
    select 型号=case grouping(型号) when 0 then 型号
    else left(型号,patindex('%[0-9]%',型号+'0')-1) end
    ,数量=case grouping(型号) when 1 then ''
    else cast(sum(数量) as varchar) end
    ,价格=sum(价格)
    from 基本表
    group by left(型号,patindex('%[0-9]%',型号+'0')-1)
    ,型号 with rollup
    having grouping(left(型号,patindex('%[0-9]%',型号+'0')-1))=0
    order by 型号
    go--删除测试
    drop table 基本表/*--测试结果型号        数量     价格     
    ---------- -------- ---------
    A                   600
    A1         10       100
    A2         20       200
    A3         30       300
    B                   6000
    B1         10       1000
    B2         20       2000
    B3         30       3000(所影响的行数为 8 行)
    --*/
      

  17.   

    之所以不直接用left(型号,1),而用 left(型号,patindex('%[0-9]%',型号+'0')-1)
    是考虑到你一型号可能是这样的:AA1
    AA2
    BBB1
    BBB2即总类长度不固定的情况,所以没有写死,而是用了patindex来动态判断
    如果不存在这个问题,型号是固定的,则直接用left(型号,1)的效率会高一些
      

  18.   

    能不能讲一下grouping的具体意义啊
      

  19.   

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[temp1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[temp1]
    gocreate table temp1(型號 varchar(20) not null,數量 int null,價格 int null)
    goinsert into temp1
    select 'A1',10,100   union
    select 'A2',20,200   union
    select 'A3',30,300   union
    select 'B1',10,1000 union
    select 'B2',20,2000 union
    select 'B3',30,3000goselect * from temp1 order by 1
    /*
    型号 数量 价格
    A1   10   100
    A2   20   200
    A3   30   300
    B1   10   1000
    B2   20   2000
    B3   30   3000
    */select 型號,isnull(cast(數量 as varchar(10)),'') as 數量,價格 from
    (
    select left(型號,len(型號)-1) as 型號,NULL as 數量,sum(價格) as 價格 from temp1 group by  left(型號,len(型號)-1)  
    union all
    select * from temp1
    ) a
    order by 1
    /*
    型號     數量      價格
    A 600
    A1 10 100
    A2 20 200
    A3 30 300
    B 6000
    B1 10 1000
    B2 20 2000
    B3 30 3000
    */
      

  20.   

    总结:
    方法1:采用Grouping 聚合函数实现分组,特点:灵活,方便,代码维护性强.select 型号=case grouping(型号) when 0 then 型号
    else left(型号,patindex('%[0-9]%',型号+'0')-1) end
    ,数量=case grouping(型号) when 1 then ''
    else cast(sum(数量) as varchar) end
    ,价格=sum(价格)
    from 基本表
    group by left(型号,patindex('%[0-9]%',型号+'0')-1)
    ,型号 with rollup
    having grouping(left(型号,patindex('%[0-9]%',型号+'0')-1))=0
    order by 型号
    方法2:采用嵌套的方法,语句简单,直观,灵活性不足。如果记录多,最好不要采用。
    select 型號,isnull(cast(數量 as varchar(10)),'') as 數量,價格 from
    (
    select left(型號,len(型號)-1) as 型號,NULL as 數量,sum(價格) as 價格 from temp1 group by  left(型號,len(型號)-1)  
    union all
    select * from temp1
    ) a
    order by 1
      

  21.   

    select 型号=case grouping(型号) when 0 then 型号
    else left(型号,patindex('%[0-9]%',型号+'0')-1) end
    ,数量=case grouping(型号) when 1 then ''
    else cast(sum(数量) as varchar) end
    ,价格=sum(价格)
    from 基本表
    group by left(型号,patindex('%[0-9]%',型号+'0')-1)
    ,型号 with rollup
    having grouping(left(型号,patindex('%[0-9]%',型号+'0')-1))=0
    order by 型号
    这样的语句我能否在DELPHI中调用执行??
      

  22.   

    GROUPING
    是一个聚合函数,它产生一个附加的列,当用 CUBE 或 ROLLUP 运算符添加行时,附加的列输出值为1,当所添加的行不是由 CUBE 或 ROLLUP 产生时,附加列值为0。仅在与包含 CUBE 或 ROLLUP 运算符的 GROUP BY 子句相联系的选择列表中才允许分组。语法
    GROUPING ( column_name )参数
    column_name是 GROUP BY 子句中用于检查 CUBE 或 ROLLUP 空值的列。返回类型
    int注释
    分组用于区分由 CUBE 和 ROLLUP 返回的空值和标准的空值。作为CUBE 或 ROLLUP 操作结果返回的 NULL 是 NULL 的特殊应用。它在结果集内作为列的占位符,意思是"全体"。示例
    下面的示例将 royalty 的数值分组,并聚合 advance 的数值。GROUPING 函数应用于 royalty 列。USE pubs
    SELECT royalty, SUM(advance) 'total advance', 
       GROUPING(royalty) 'grp'
       FROM titles
       GROUP BY royalty WITH ROLLUP结果集在 royalty 下显示两个空值。第一个 NULL 代表从表中这一列得到的空值组。第二个 NULL 在 ROLLUP 操作所添加的汇总行中。汇总行显示的是所有 royalty 组的 advance 合计数值,并且在 grp 列中用 1 标识。下面是结果集:royalty        total advance              grp 
    ---------      ---------------------    ---
    NULL           NULL                     0  
    10             57000.0000               0  
    12             2275.0000                0  
    14             4000.0000                0  
    16             7000.0000                0  
    24             25125.0000               0  
    NULL           95400.0000               1