选插入:
insert tab(报表类型,报表期数,价格)
select '月报',left(报表期数,6),sum(价格)/3 --or avg(价格) ??
from tab where 报表类型='旬报'
group by left(报表期数,6)
删除:
delete from tab where where 报表类型='旬报'
insert tab(报表类型,报表期数,价格)
select '月报',left(报表期数,6),sum(价格)/3 --or avg(价格) ??
from tab where 报表类型='旬报'
group by left(报表期数,6)
删除:
delete from tab where where 报表类型='旬报'
解决方案 »
- 装了SQL Server2000,想再装SQL Server2005要注意什么?
- SQL Server 2005 连接服务器的问题
- sql 2005 XQuery 对 xml 的操作?
- 分页存储过程拼接错误
- 急求 sql server 2005 错误53 17 解决方案
- 帮我看看这个触发器吧~~ 谢谢
- WinXP下有没有办法安装上SQLServer2000的MSDE?
- 这两个语句有什么区别吗?
- 在存储中运行动态的sql为什么变量要改成字符型
- 关于在表中查找记录的问题!!!
- 一个很棘手的问题,表的内联,也有点难度,希望大家可以提点意见!
- MSSQL_NULL_PACKET_DOS是一個什麼咚咚啊?
union
select '月报'dd,substring(报表期数,1,6),sum(价格)/3 from tablename
where 报表类型='旬报' group by substring(报表期数,1,6),dd
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)
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)
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)
select 报表类型='月报',报表期数=left(报表期数,6),价格=avg(价格)
from 表
group by 报表类型,left(报表期数,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 行)
--*/