有可能说的别扭些
这样的表结构日期        产品A    产品B2007-1-1     50        60
2007-1-2     100       150求一SQL语句,输出日期      今天的A+B   今天的(A+B)-昨天(A+B)
2007-1-2      250          140
哪位高手帮下忙,谢谢!

解决方案 »

  1.   

    日期        产品A    产品B2007-1-1     50        60
    2007-1-2     100       150select 
        (t.产品A+t.产品B),
        (t.产品A+t.产品B)-isnull((select 产品A+产品B from 表 where datediff(dd,日期,t.日期)=1),0) 
    from 
        表 t
      

  2.   

    select t.prod_a + t.prod_b , t.prod_a + t.prod_b - 
    (select prod_a + prod_b from tmp where datediff(dd , prod_date , t.prod_date) = 1)
    from tmp as t
      

  3.   

    --创建测试数据
    create table T(
    mydate datetime,
       Product_A int,
       Product_B int
    )insert T  select '2007-1-1', 50, 60
    union all select '2007-1-2', 100, 150--建立函数
    create function getMinusAB(@Product_A int, @Product_B int, @CurDate datetime)
    returns int
    as
    begin
    declare @y_Product_A int,
               @y_Product_B int
       select @y_Product_A = isnull(Product_A, 0), @y_Product_B = isnull(Product_B, 0) 
                 from T where mydate = DateAdd(day, -1, @CurDate)   return isnull((@Product_A + @Product_B) - (@y_Product_A + @y_Product_B),0)
    endselect * from T--SQL语句
    select convert(varchar(20), mydate, 23) as [日期], 
           Product_A + Product_B as [今天的A+B], 
           dbo.getMinusAB(Product_A, Product_B, mydate) as [今天的(A+B)-昨天(A+B)] from T--结果
    2007-01-01 110 0
    2007-01-02 250 140