例如:查出所有用户(同一个用户有购买多次)的最后一次的购买记录.
有没有多点,这种综合型的查询例子,谢谢!

解决方案 »

  1.   


    --表tb 客户 customer 日期 date
    select *
    from tb t
    where not exists (select 1 from tb where customer = t.customer and date > t.dat)
      

  2.   

    select * from tb a where not exists(select 1 from tb where id=a.id and dt>a.id)
      

  3.   


    --如果tb表有主键,1楼的可能会出现重复记录
    --表tb 客户 customer 日期 date
    select *
    from tb t
    where id = (select top 1 id from tb where customer = t.customer order by date desc)
      

  4.   


    --sql2005;with ach as
    (
        select *,rid = row_number() over (partition by customer order by date desc)
        from tb
    )select *
    from ach
    where rid = 1
      

  5.   

    感谢AcHerat前辈,但我是求经典题集,不是解这道题的题集,^_^
      

  6.   

    经典题集!楼主看看这个吧!不敢再粘代码了。http://topic.csdn.net/u/20080304/12/485bf91c-7988-46f0-9943-288b1c9ef5f0.html
      

  7.   

    楼主是要的什么呢?
    create table tb(客户编号 int,货品编号 int,购买日期 datetime,购买数量 int)
    insert into tb select 1,23,'2011-03-24',10
    insert into tb select 2,21,'2011-03-25',15
    insert into tb select 3,10,'2011-03-24',12
    insert into tb select 1,22,'2011-03-27',18
    insert into tb select 1,25,'2011-04-10',6
    insert into tb select 2,15,'2011-04-05',12
    insert into tb select 1,25,'2011-05-20',4
    insert into tb select 3,27,'2011-05-21',5
    go
    select * from tb a where not exists(select 1 from tb where 客户编号=a.客户编号 and 购买日期>a.购买日期)
    /*
    客户编号        货品编号        购买日期                    购买数量
    ----------- ----------- ----------------------- -----------
    2           15          2011-04-05 00:00:00.000 12
    1           25          2011-05-20 00:00:00.000 4
    3           27          2011-05-21 00:00:00.000 5(3 行受影响)*/
    go
    drop table tb
      

  8.   


    select a.* from tb a,(select customer,max(date) date from tb group by customer) b where a.customer=b.customer and a.date=b.date
      

  9.   

    MS-SQL Server这块的气氛不错,有时在java里问问题,老是沉沉的
      

  10.   

    select * from tb a where 购买日期=(SELECT MAX(购买日期) FROM TB WHERE  客户编号=a.客户编号) ORDER BY 客户编号