我有这样一个需求,打印出今天的价格和昨天的价格我用如下语句:
select pubdate,Price,
(case when convert(varchar(10),pubdate,126)=convert(varchar(10),dateadd(day,-1,pubdate),126) then price else 0 end) as yestodayPrice from table where TradeDate>'2008-1-1'打印出来每个昨天的价格都为0,实际是有的,请教高人指教,感谢!

解决方案 »

  1.   

    我有这样一个需求,打印出今天的价格和昨天的价格,求出两天之间的价格比 表结构如下 TradeDate    price 
    2008-1-1    200 
    2008-1-2    205 
    2008-1-3    202 我用如下语句: 
    select TradeDate,Price, 
    (case when convert(varchar(10),TradeDate,126)=convert(varchar(10),dateadd(day,-1,TradeDate),126) then price else 0 end) as yestodayPrice 
    from table where TradeDate>'2008-1-1' 用case when 来算昨天的日期,好象这样并不行,请教高人指教,感谢!
      

  2.   


    Select s.* (Select Top 1 PRICE From table  where TradeDate <s. TradeDate   order by TradeDate   desc) as yestodayPrice 
    from table  s where s. TradeDate='2008-1-1' 
      

  3.   


    Select s.* ,(Select Top 1 PRICE From table  where TradeDate <s. TradeDate   order by TradeDate   desc) as yestodayPrice 
    from table  s where s. TradeDate='2008-1-1' 
    --上面的少一逗號