发表于: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的取值是好多??