--前三次是指销售日期最前的?
select 商品编码,销售日期,售价
from 销售明细表 a
where(
select count(*) from 销售明细表
where 商品编码=a.商品编码 and(
销售日期<a.销售日期 
or 
销售日期=a.销售日期 and 销售小票号<=a.销售小票号))<=3

解决方案 »

  1.   

    --如果前三次是指销售日期最新的,则改为:
    select 商品编码,销售日期,售价
    from 销售明细表 a
    where(
    select count(*) from 销售明细表
    where 商品编码=a.商品编码 and(
    销售日期>a.销售日期 
    or 
    销售日期=a.销售日期 and 销售小票号>=a.销售小票号))<=3
      

  2.   

    可以這樣不?
    1:將top 1 商品编码,max(销售日期) 插入到一個臨時表 #t group by 商品编码
    2:將top 1 商品编码,max(销售日期) 插入到一個臨時表 #t where not exists (select * from #t where ....)
    group by 商品编码
    3:同2.
    ?????????ˋ??
      

  3.   

    To:zjcxc(邹建)问一下--前三次是指销售日期最前的?
    select 商品编码,销售日期,售价
    from 销售明细表 a
    where(
    select count(*) from 销售明细表
    where 商品编码=a.商品编码 and(
    销售日期<=a.销售日期  --这样改好像不是可以吗?
     and 销售小票号<=a.销售小票号))<=3
      

  4.   

    还有 to:chenqianlong(443),你的方法好像不行哦。
      

  5.   

    2:select 销售小票号,商品编码,max(销售日期)into #t from table where 销售小票号 not in (select 销售小票号 from #t)