选插入:
insert tab(报表类型,报表期数,价格) 
select '月报',left(报表期数,6),sum(价格)/3 --or avg(价格) ??
from tab where 报表类型='旬报'
group by left(报表期数,6)
删除:
delete from tab where where 报表类型='旬报'

解决方案 »

  1.   

    select * from tablename where 报表类型='月报'
    union
    select '月报'dd,substring(报表期数,1,6),sum(价格)/3 from tablename
     where 报表类型='旬报' group by substring(报表期数,1,6),dd
      

  2.   


    declare @ table(报表类型 nvarchar(10),报表期数 nvarchar(20),价格 decimal(5,2))
    insert into @ select 
     '月报'    , '200403'         ,   10.00  union all select 
        '月报'  ,   '200404'     ,       12.00  union all select 
        '旬报'  ,   '200405上'  ,        13.00  union all select 
        '旬报'  ,   '200405中' ,         11.00  union all select 
        '旬报'  ,   '200405下',          15.00  union all select 
        '月报'  ,   '200406' ,            9.00select 报表类型='月报',
    substring(报表期数,1,6) as  报表期数  ,
    sum(价格) As 价格
    from @
    group by substring(报表期数,1,6)--结果
    报表类型 报表期数   价格                                       
    ---- ------ ---------------------------------------- 
    月报   200403 10.00
    月报   200404 12.00
    月报   200405 39.00
    月报   200406 9.00(4 row(s) affected)
      

  3.   

    刚才忘了除3了
    declare @ table(报表类型 nvarchar(10),报表期数 nvarchar(20),价格 decimal(5,2))
    insert into @ select 
     '月报'    , '200403'         ,   10.00  union all select 
        '月报'  ,   '200404'     ,       12.00  union all select 
        '旬报'  ,   '200405上'  ,        13.00  union all select 
        '旬报'  ,   '200405中' ,         11.00  union all select 
        '旬报'  ,   '200405下',          15.00  union all select 
        '月报'  ,   '200406' ,            9.00
    select 报表类型='月报',
    substring(报表期数,1,6) as  报表期数  ,
    sum(价格)/3 As 价格
    into #tmp
    from @
    where 报表类型='旬报'
    group by substring(报表期数,1,6)
     
    select 报表类型, 报表期数,价格 from #tmp a union all
    select * from @   where 报表类型='月报'
    order by 报表期数
    drop table #tmp
    --结果报表类型       报表期数                 价格                                       
    ---------- -------------------- ---------------------------------------- 
    月报         200403               10.000000
    月报         200404               12.000000
    月报         200405               13.000000
    月报         200406               9.000000(4 row(s) affected)
      

  4.   

    declare @ table(报表类型 nvarchar(10),报表期数 nvarchar(20),价格 decimal(5,2))
    insert into @ select 
     '月报'    , '200403'         ,   10.00  union all select 
        '月报'  ,   '200404'     ,       12.00  union all select 
        '旬报'  ,   '200405上'  ,        13.00  union all select 
        '旬报'  ,   '200405中' ,         11.00  union all select 
        '旬报'  ,   '200405下',          15.00  union all select 
        '月报'  ,   '200406' ,            9.00select * from @ where 报表类型='月报' 
    union
    select 报表类型='月报',substring(报表期数,1,6) as  报表期数, sum(价格)/3 As 价格
    from @
    where 报表类型='旬报'
    group by substring(报表期数,1,6)
      

  5.   

    --转换处理
    select 报表类型='月报',报表期数=left(报表期数,6),价格=avg(价格)
    from 表
    group by 报表类型,left(报表期数,6)
      

  6.   

    --测试--测试数据
    create table 表(报表类型 varchar(10),报表期数 varchar(14),价格 decimal(20,2))
    insert 表 select '月报','200403',10.00
    union all select '月报','200404',12.00
    union all select '旬报','200405上',13.00
    union all select '旬报','200405中',11.00
    union all select '旬报','200405下',15.00
    union all select '月报','200406',9.00
    go--转换处理
    select 报表类型='月报',报表期数=left(报表期数,6),价格=avg(价格)
    from 表
    group by 报表类型,left(报表期数,6)
    go--删除测试
    drop table 表/*--测试结果
    报表类型 报表期数         价格    
    ---- ------------ ---------------
    月报   200403       10.000000
    月报   200404       12.000000
    月报   200405       13.000000
    月报   200406       9.000000(所影响的行数为 4 行)
    --*/