一张表t,物品价格表,今日新增了新物品,求今日价格与昨日对比后,显示所有物品的今日价格、与昨日对比
表t
id     wp物品 rq日期    p价格
----------------------------
1      wp1   2010-3-1   10
2      wp1   2010-3-2   15
3      wp2   2010-3-2   20想要显示的结果
id    wp   rq         p    db与昨日对比
--------------------------------------
2     wp1  2010-3-2   15   +5
3     wp2  2010-3-2   20 求sql查询语句:
大侠们帮帮新手!

解决方案 »

  1.   

    select id,wp,rq,p,p-pre_p as db
    from (
    select id ,wp, rq, p,lag(order by rq group by wp) as pre_p
    from t)
    where pre_p is not null
      

  2.   

    SELECT id, wp, rq, p, db
      FROM (SELECT t.* p - lag(p) over(PARTITION BY wp ORDER BY rq) db,
                   row_number() over(PARTITION BY wp ORDER BY rq DESC) rn
              FROM t
             WHERE t.rq >= trunc(SYSDATE) - 1)
     WHERE rn = 1;
      

  3.   


    这个要在orcle10g下才可以运行的。
      

  4.   


    drop table t11;create table t11(
           id number,
           wp varchar(20),
           cr date,
           price number
    );insert into t11
    select 1, 'wp3', to_date('2010-4-6','yyyy-mm-dd'), 10 from dual union all
    select 1, 'wp1', to_date('2010-4-5','yyyy-mm-dd'), 10 from dual union all
    select 1, 'wp2', to_date('2010-4-5','yyyy-mm-dd'), 10 from dual union all
    select 2, 'wp1', to_date('2010-4-6','yyyy-mm-dd'), 15 from dual union all
    select 3, 'wp2', to_date('2010-4-6','yyyy-mm-dd'), 20 from dual;
    commit;
    select * from t11;select t1.*,(t1.price-t2.price) jiagecha 
    from (select t.* from t11 t where t.cr= trunc(sysdate)) t1
    left join (select t3.* from t11 t3 where t3.cr<trunc(sysdate)) t2
    on t1.wp=t2.wp
    --and t1.cr>t2.cr
    --and t1.cr>trunc(sysdate)
    --and t2.cr<sysdate;-- 结果如下--
      2 wp1 2010-4-6 15 5
      3 wp2 2010-4-6 20 10
      1 wp3 2010-4-6 10
      

  5.   

    忘记给你带+号了,呵呵
    select t1.*, decode((t1.price-t2.price),'','', '+'||(t1.price-t2.price))  jiagecha 
    from (select t.* from t11 t where t.cr= trunc(sysdate)) t1
    left join (select t3.* from t11 t3 where t3.cr<trunc(sysdate)) t2
    on t1.wp=t2.wp
      

  6.   


    lag(order by rq group by wp)?
    应该是lag(p)over(partition by wp order by id)
      

  7.   


    SQL> edi
    已写入 file afiedt.buf  1  select id,wp,rq,p-p1 from (select id,wp,rq,p,lag(p) over(partition by wp order by rq ) p1
      2* from t) a
    SQL> /        ID WP    RQ                   P-P1
    ---------- ----- -------------- ----------
             1 wp1   01-3月 -10
             2 wp1   02-3月 -10              5
             3 wp2   02-3月 -10
      

  8.   

    上面的都看了,都是高手啊,非常感谢!但是遇到新问题了,大侠们,再帮忙看看:
    一张表t,物品价格及销量表,日新增了新物品,求当日价格与前一日对比后,显示所有物品的今日价格、价格与前一日对比、销量与前一日对比、截止当日当月的总销量(要求查询任意时间,比较有通用性)
    表t
    id wp物品 rq日期 p价格 xl销量
    ----------------------------
    1 wp1 2010-3-1  10   5
    2 wp1 2010-3-2  15   6
    3 wp2 2010-3-2  20   12
    4 wp1 2010-3-3  17   4
    5 wp2 2010-3-3  19   15
    想要显示的结果:查询2010-3-2的结果:
    id wp rq查询的时间 p pdb xldb zxl当月总销量
    --------------------------------------
    2 wp1 2010-3-2 15 +5 +1 11
    3 wp2 2010-3-2 20  空格 空格 12
    求sql查询语句:
      

  9.   

    select id,wp,rq,p,p-pre_p as pdb,xl, xl-pre_xl as xldb
    from (
    select id ,wp, rq, p,lag(p)over(order by rq group by wp) as pre_p,
    xl,lag(p)over(order by rq group by wp) as pre_xl
    from t)
    where pre_p is not null
    and rq=to_date('2010-3-2')-1
      

  10.   

    假如数据库没有备份昨日的表!
    如何求出 
    一张表t,物品价格及销量表,当日新增了新物品,求当日价格与前一日对比后,显示所有物品的今日价格、价格与前一日对比、销量与前一日对比、截止当日当月的总销量(要求查询任意时间,比较有通用性)一个表求出变化(数据重演,或者找change 时间戳,触发器)
    在系统资源有限的情况下如何实现数据变话的动态分析!