发表于:2008-07-30 18:03:06 楼主
查看每年产品的执行价格:
如果此产品在本年中没有调价,那么就沿用上年最近的价格
如果此产品在本年中有几次调价,那么就沿用最近的价格
如果此产品在本年中没有,那么就显示为新产品。
有例:
1-1 5 2005-08-05
1-1 6 2006-08-08
1-2 6 2005-08-09
1-2 7 2005-11-1
1-3 8 2007-01-01
查看各个产品2006年执行价格:
得出: 1-1 6 2006-08-08
1-2 7 2005-11-1
1-3 0 2007-01-01(新产品)
如果日期为2005
1-1 5 2005-08-05
1-2 7 2005-11-1
1-3 0 2007-01-01
同理2007
1-1 6 2006-08-08
1-2 7 2005-11-1
1-3 8 2007-01-01 求此语句应该怎么写? 答:
set nocount on;
declare @T table([Col1] nvarchar(3),[Col2] int,[Col3] Datetime)
Insert @T
select N'1-1',5,'2005-08-05' union all
select N'1-1',6,'2006-08-08' union all
select N'1-2',6,'2005-08-09' union all
select N'1-2',7,'2005-11-1' union all
select N'1-3',8,'2007-01-01'
declare @i int
set @i=2006
Select
[Col1],[Col2]=case when year([Col3])>@i then 0 else [Col2] end,
[Col3]=case when year([Col3])>@i then convert(varchar(10),[Col3],120)+'(新品)' else convert(varchar(10),[Col3],120) end
from
@T a
where
[Col3]=(select top 1 [Col3] from @T where [Col1]=a.[Col1] and year([Col3])<=@i order by [Col3] desc )
or
@i<(select year(min([Col3])) from @T where [Col1]=a.[Col1])
and not exists(select 1 from @T where [Col1]=a.[Col1] and [Col1]>a.[Col1])Col1 Col2 Col3
---- ----------- ----------------
1-1 6 2006-08-08
1-2 7 2005-11-01
1-3 0 2007-01-01(新品)请问此语言怎么解释》? [Col3]=(select top 1 [Col3] from @T where [Col1]=a.[Col1] and year([Col3]) <=@i order by [Col3] desc ) 这个a.col1的取值是好多??
查看每年产品的执行价格:
如果此产品在本年中没有调价,那么就沿用上年最近的价格
如果此产品在本年中有几次调价,那么就沿用最近的价格
如果此产品在本年中没有,那么就显示为新产品。
有例:
1-1 5 2005-08-05
1-1 6 2006-08-08
1-2 6 2005-08-09
1-2 7 2005-11-1
1-3 8 2007-01-01
查看各个产品2006年执行价格:
得出: 1-1 6 2006-08-08
1-2 7 2005-11-1
1-3 0 2007-01-01(新产品)
如果日期为2005
1-1 5 2005-08-05
1-2 7 2005-11-1
1-3 0 2007-01-01
同理2007
1-1 6 2006-08-08
1-2 7 2005-11-1
1-3 8 2007-01-01 求此语句应该怎么写? 答:
set nocount on;
declare @T table([Col1] nvarchar(3),[Col2] int,[Col3] Datetime)
Insert @T
select N'1-1',5,'2005-08-05' union all
select N'1-1',6,'2006-08-08' union all
select N'1-2',6,'2005-08-09' union all
select N'1-2',7,'2005-11-1' union all
select N'1-3',8,'2007-01-01'
declare @i int
set @i=2006
Select
[Col1],[Col2]=case when year([Col3])>@i then 0 else [Col2] end,
[Col3]=case when year([Col3])>@i then convert(varchar(10),[Col3],120)+'(新品)' else convert(varchar(10),[Col3],120) end
from
@T a
where
[Col3]=(select top 1 [Col3] from @T where [Col1]=a.[Col1] and year([Col3])<=@i order by [Col3] desc )
or
@i<(select year(min([Col3])) from @T where [Col1]=a.[Col1])
and not exists(select 1 from @T where [Col1]=a.[Col1] and [Col1]>a.[Col1])Col1 Col2 Col3
---- ----------- ----------------
1-1 6 2006-08-08
1-2 7 2005-11-01
1-3 0 2007-01-01(新品)请问此语言怎么解释》? [Col3]=(select top 1 [Col3] from @T where [Col1]=a.[Col1] and year([Col3]) <=@i order by [Col3] desc ) 这个a.col1的取值是好多??
解决方案 »
- 用SQLSERVER2000写好的程序,要用MSDE发布给用户,装了MSDE后,不知道如何将数据库实例导入,请高手指点? 谢谢!
- [救命啊]mssql 连接 mysql 问题
- 《****问一下建表的问题*****》建好表后服务器重起后,表丢失,问题解决马上给分 !
- SQL能否在Windows XP的操作系统上安装?
- 存储过程问题!大哥帮忙
- 连上库后,什么SQL语句可知道库里有什么信息,包括表(请最快答复)
- [分享帖]再次散分分享:如何屏蔽成功备份产生的ERRORLOG信息
- 有这样一个查询:select top 6 id,title from [mytable] where id in (34,279,34,44,45,33,46,279,45,46),怎样使结果按条件先后排序?
- TTable组件如何实现自动LOGIN数据库?
- 交叉查询
- 要重做系统,如何完全的备份数据库实例?
- sql2000 英文版 查询含有中文问题(在线等)
declare @T table (图号 varchar(3),价格 numeric(2,1),时间 datetime,产品名称 varchar(4))
这个是定义表变量,其他的都是语句都很简单 答2:
create table #t(图号 varchar(3),价格 numeric(2,1),时间 datetime,产品名称 varchar(4))
这个是临时表