客户号  购买日期                     销售好 金额246 2003-04-29 00:00:00.000 3166 177.00
246 2003-05-16 00:00:00.000 2389 42.00
246 2003-05-08 00:00:00.000 2168 85.00
248 2003-04-30 00:00:00.000 3172 997.00
248 2003-04-17 00:00:00.000 3043 550.00
249 2003-07-23 00:00:00.000 858 315.00
249 2003-07-31 00:00:00.000 907 204.00
250 2003-04-20 00:00:00.000 3070 343.00
250 2003-04-25 00:00:00.000 3126 29.00
253 2003-04-29 00:00:00.000 3162 271.00
254 2003-06-17 00:00:00.000 1513 244.00
254 2003-06-24 00:00:00.000 1676 77.00
254 2003-06-19 00:00:00.000 1624 12.60
写个语句实现 最近一段时间(比如30天)没有销售记录,而且最近一次(注意,最近一次)的销售金额小于一个值(比如200)的客户号
注意:每个客户有很多条销售记录

解决方案 »

  1.   

    insert into #ttt select khh,max(gmrq) as rq from table group by khh
    delete from #ttt where day(getdate()-rq)<30 //剩下的都是大于30天的
    select khh from table where khh in (select khh from #ttt) and je<200这三条可以放在一个语句里面的,不过就是效率比较低。
      

  2.   

    select a.customerno (select * from your_table where day(getdate()-rq)<30) as a 
    where (select top1 from a order by purcharse_date desc)<200i'm in bar, do not know wheather it can work.try yourself.
      

  3.   

    应该有2个表吧一个是客户表custom别告所我你没有这个表,
    一个是销售记录表就是你列的表了名字叫s好了select custom.客户号 from custom 
      where 客户号 not in 
     (select 客户号 from s where datediff(day, 购买日期, getdate)<30)
      and 
      客户号 in 
     (select 客户号 from s,(select 客户号,max(购买日期) as "购买日期" from s group by 客户号) ss where s.金额<200 and s.客户号=ss.客户号 and s.购买日期=ss.购买日期)行拉
    不过估计select 客户号,max(购买日期) as "购买日期" from s group by 客户号) ss 得换成视图ss因为from里不能写select
      

  4.   

    CREATE  procedure cust_Lost
    (
    @days int,
    @MinSellPrice numeric(13,4)
    )
    as  
    select custno from cust where custno not in 
     (select distinct custno from sells where datediff(day,buyDate,GetDate()) < @Days) a
     and custno in (select custno from sells where sellPrice<@MinSellPrice)
      

  5.   

    这个问题应该不大,不过我先问你用的是什么数据库?oracel就更加好办了.
      

  6.   

    应该有2个表吧一个是客户表custom别告所我你没有这个表,
    一个是销售记录表就是你列的表了名字叫s好了select custom.客户号 from custom 
      where 客户号 not in 
     (select 客户号 from s where datediff(day, 购买日期, getdate)<30 )
      and exists(select top 1 * from s where s.客户号=custom.客户号 and s.金额<200 order by purcharse_date desc)
      

  7.   

    with query1 do
    .close;
    .sql.clear;
    .sql.add( select 客户号 from a where 客户号 not in (select 客户号 from a  where datediff(day, 购买日期, getdate())<30 ));
    .open;
    with query2 do
    .close;
    .sql.clear;
    .sql.add( select * from a where 客户号=query1.field(0).asstring and 金额<200 order by purcharse_date desc );
    .open 
      

  8.   

    谢谢  liuhelin(鹤林)
    你的方法正确,接分! 
    但这我有点不太名台可以将一下思想吗。
    exists(select top 1 * from s where s.客户号=custom.客户号 and s.金额<200 order by purcharse_date desc没有s.客户号=custom.客户号 ,就不行
      

  9.   

    datediff()是SQL中取日期差的吗?
      

  10.   

    我来告诉你datediff是取日期差的,你可以从sql server中查看帮助