select a.商品,a.价格 as  错误价格,b.现价格 as 正确价格,a.出库日期
from 商品出库表 a,商品调价表 b
where a.商品=b.商品
and a.出库日期>=b.调价日期
and a.价格<>b.现价格

解决方案 »

  1.   

    出库日期>第一次调价日期但是<第二次调价日期你怎么判断
      

  2.   

    这样应该可以:
    select a.商品,a.价格 as  错误价格,b.现价格 as 正确价格,a.出库日期
    from 商品出库表 a,商品调价表 b
    where a.商品=b.商品
    and a.出库日期>=b.调价日期
    and a.出库日期<isnull((select min(调价日期) from 商品调价表 c where c.商品=a.商品 and c.调价日期>b.调价日期),'9999-12-31')
    and a.价格<>b.现价格
      

  3.   

    这里假设你的调价日期、出库日期都是DATETIME型的!
      

  4.   

    保存日期时都是date(datetime)保存进表里的,这样行吗?
      

  5.   

    保存日期时都是date(datetime)保存进表里的,这样行吗?
      

  6.   

    以下语句经过测试:select a.商品,a.价格 as  错误价格,b.现价格 as 正确价格,a.出库日期
    from 商品出库表 a,(select c.*,isnull((select min(调价日期) from 商品调价表 d where d.商品=c.商品 and d.调价日期>c.调价日期),'9999-12-31') as 调价结束日期 from 商品调价表 c) as b
    where a.商品=b.商品
    and a.出库日期>=b.调价日期
    and a.出库日期<b.调价结束日期
    and a.价格<>b.现价格
      

  7.   

    测试过程(改了中文的字段名,加了几行数据):create table #CKB (id int,sp char(10),jg numeric(7,2),chrq datetime)create table #TJB (id int,sp char(10),yjg numeric(7,2),xjg numeric(7,2),tjrq datetime)
    insert #CKB values(1,       'a',     1.1,   '2002-5-10')
    insert #CKB values(2,       'a',     1.1,   '2002-5-11')
    insert #CKB values(3,       'a',     1.6,   '2002-5-20')
    insert #CKB values(4,       'b',     2.1,   '2002-6-01')
    insert #CKB values(5,       'b',     2.8,   '2002-6-09')
    insert #CKB values(6,       'a',     1.8,   '2002-6-2')
    insert #CKB values(7,       'a',     1.6,   '2002-6-4')
    insert #CKB values(8,       'a',     1.8,   '2002-6-8')
    insert #TJB values(1,       'a',     1.1, 1.6,  '2002-5-1')
    insert #TJB values(2,       'b',     2.1, 2.8,  '2002-5-28')
    insert #TJB values(3,       'a',     1.6, 1.8,  '2002-6-1')select a.sp,a.jg as  cwjg,c.xjg as zqjg,a.chrq
    from #ckb a,(select b.*,isnull((select min(tjrq) from #tjb c where c.sp=b.sp and c.tjrq>b.tjrq),'9999-12-31') as tjjsrq
    from #tjb b
    ) as c
    where a.sp=c.sp
    and a.chrq>=c.tjrq
    and a.jg<>c.xjg
    and a.chrq<c.tjjsrq结果:
    sp         cwjg        zqjg        chrq                        
    ---------- ----------- ----------- --------------------------- 
    a          1.10        1.60        05 10 2002 12:00AM          
    a          1.10        1.60        05 11 2002 12:00AM          
    b          2.10        2.80        06 1 2002 12:00AM           
    a          1.60        1.80        06 4 2002 12:00AM           (4 row(s) affected)