根据最近时间查单价.有点难度 不用游标
2008-12-31 21:41
951 查看
CREATE TABLE tb(日期 datetime, 料号 NVARCHAR(30), 单价 int)
INSERT tb SELECT '2008/5/1','AAA',2
UNION ALL SELECT '2008/6/6','BBB',22
UNION ALL SELECT '2008/6/6','CCC ',12
UNION ALL SELECT '2008/7/3','BBB',21
UNION ALL SELECT '2008/7/9','AAA ',1
--select * from tb
--select distinct 料号 FROM TB
--select count (distinct 料号) from tb
--select TOP 1 日期 ,单价 from tb where 料号 ='AAA' order by 日期 DESC
create procedure sp_查询最近单价
WITH ENCRYPTION
AS
begin tran--启动事务
declare @ERROR int
set @ERROR=0
begin
declare @temp table
(
[日期] datetime,
[料号] NVARCHAR(30),
[单价] int
)
declare @tempdb料号 table
(
[id] int identity(1,1),
[料号] NVARCHAR(30)
)
declare @改后temp table
(
[日期] datetime,
[料号] NVARCHAR(30),
[单价] int
)
insert into @temp select * from tb
-- SELECT * FROM @tempdb
SET @ERROR =@ERROR +@@ERROR
IF (@ERROR <>0) GOTO EXT
insert into @tempdb料号 select DISTINCT 料号 from tb
-- SELECT * FROM @tempdb料号
SET @ERROR =@ERROR +@@ERROR
IF (@ERROR <>0) GOTO EXT
declare @temp日期 datetime,@temp料号 varchar(30),@temp单价 INT,@I int
set @i=1
select distinct 料号 FROM @tempdb料号 where id=@i
WHILE @@rowcount<>0
begin
select TOP 1 @temp日期=日期 ,@temp料号=料号,@temp单价=单价 from @temp where 料号 =(select distinct 料号 FROM @tempdb料号 where id=@i )
order by 日期 DESC
SET @ERROR =@ERROR +@@ERROR
IF (@ERROR <>0) GOTO EXT
INSERT INTO @改后temp VALUES(@temp日期,@temp料号,@temp单价)
SET @ERROR =@ERROR +@@ERROR
IF (@ERROR <>0) GOTO EXT
set @i=@i+1
select distinct 料号 FROM @tempdb料号 where id=@i
end
select * from @改后temp
end
--异常出口
EXT:
--判断执行状态
IF (@ERROR =0)
BEGIN
COMMIT
END
ELSE
ROLLBACK
exec sp_查询最近单价
结果:
2008-07-09 00:00:00.000 AAA 1
2008-07-03 00:00:00.000 BBB 21
2008-06-06 00:00:00.000 CCC 12
INSERT tb SELECT '2008/5/1','AAA',2
UNION ALL SELECT '2008/6/6','BBB',22
UNION ALL SELECT '2008/6/6','CCC ',12
UNION ALL SELECT '2008/7/3','BBB',21
UNION ALL SELECT '2008/7/9','AAA ',1
--select * from tb
--select distinct 料号 FROM TB
--select count (distinct 料号) from tb
--select TOP 1 日期 ,单价 from tb where 料号 ='AAA' order by 日期 DESC
create procedure sp_查询最近单价
WITH ENCRYPTION
AS
begin tran--启动事务
declare @ERROR int
set @ERROR=0
begin
declare @temp table
(
[日期] datetime,
[料号] NVARCHAR(30),
[单价] int
)
declare @tempdb料号 table
(
[id] int identity(1,1),
[料号] NVARCHAR(30)
)
declare @改后temp table
(
[日期] datetime,
[料号] NVARCHAR(30),
[单价] int
)
insert into @temp select * from tb
-- SELECT * FROM @tempdb
SET @ERROR =@ERROR +@@ERROR
IF (@ERROR <>0) GOTO EXT
insert into @tempdb料号 select DISTINCT 料号 from tb
-- SELECT * FROM @tempdb料号
SET @ERROR =@ERROR +@@ERROR
IF (@ERROR <>0) GOTO EXT
declare @temp日期 datetime,@temp料号 varchar(30),@temp单价 INT,@I int
set @i=1
select distinct 料号 FROM @tempdb料号 where id=@i
WHILE @@rowcount<>0
begin
select TOP 1 @temp日期=日期 ,@temp料号=料号,@temp单价=单价 from @temp where 料号 =(select distinct 料号 FROM @tempdb料号 where id=@i )
order by 日期 DESC
SET @ERROR =@ERROR +@@ERROR
IF (@ERROR <>0) GOTO EXT
INSERT INTO @改后temp VALUES(@temp日期,@temp料号,@temp单价)
SET @ERROR =@ERROR +@@ERROR
IF (@ERROR <>0) GOTO EXT
set @i=@i+1
select distinct 料号 FROM @tempdb料号 where id=@i
end
select * from @改后temp
end
--异常出口
EXT:
--判断执行状态
IF (@ERROR =0)
BEGIN
COMMIT
END
ELSE
ROLLBACK
exec sp_查询最近单价
结果:
2008-07-09 00:00:00.000 AAA 1
2008-07-03 00:00:00.000 BBB 21
2008-06-06 00:00:00.000 CCC 12
相关文章推荐
- 根据最近时间求单价....SQL
- 最近估计太多时间放在blog上了,功夫开始恢复锻炼,项目有点事做,还要准备面试。
- Javaweb项目不用重启加载properties文件的方法(根据时间戳来判断)
- 邮票分你一半 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 小珂最近收集了些邮票,他想把其中的一些给他的好朋友小明。每张邮票上都有分值,他们想把这些邮票分成两
- 素数距离问题 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度。如果左右有等距离长度素数
- 最近有点时间-赶紧学习,好久没有写东西了
- 今天刚弄的sql查询的小问题,根据时间查询最近的条记录
- 最近有点时间-赶紧学习,好久没有写东西了
- 根据ajax从服务器获取数据的时间和ID,根据最近的时间,显示一个记录重复ID的数字
- 多条相同数据中,选根据选择最近时间一条记录
- TriggerUtils 根据cron表达式计算最近几次的执行时间
- 根据cron表达式计算最近几次的执行时间
- K-Backup技术文章:根据文件最近修改时间备份文件
- 根据多个时间取距当前最近的
- Magento ver. 1.5.1.0—根据系统当天时间,Order导出最近俩天的ERP需求数据SQL语句
- 个人想总结的东西太多了,最近正好有点时间
- Android 根据指定某日时间,获取指定星期【原创】
- 获取文件最近的修改时间
- HDU1598 并查集(有点难度哦)//感觉又有点像和宽度优先搜索有点结合
- C#获取指定月指定周的日期范围 根据当前时间获取本月开始日期和结束日期