表结构如下  
客户            商品             价钱        交易日期  
A                aa              11          20060601  
A                bb              22          20060603  
A                cc              33          20060602  
B                dd              44          20060602  
B                ee              55          20060601  
C                ff              66          20060601  
D                gg              77          20060604  
D                hh              88          20060602  
D                jj              99          20060601  
表记录客户的交易历史  描述:客户A  在20060601  里面  买了商品aa价值11  
求:各客户前几次的交易记录  按照  客户升序,日期降序。  
例如求:各客户的前2次交易历史; 
查询的结果: 
客户             商品           价钱      交易日期  
A                aa              11          20060603  
A                bb              22          20060601  
B                dd              44          20060602  
B                ee              55          20060601  
C                ff              66          20060601  
D                gg              77          20060604  
D                hh              88          20060602  
怎么写呀~~不要用游标  
  

解决方案 »

  1.   

    select top 2 * from 表 order by 客户,交易日期 desc
      

  2.   

    declare @t table(客户 varchar(50),商品 varchar(10),价钱 int,交易日期 datetime)
    Insert @t select 'A','aa',11,'20060601'
    Union all select 'A','bb',22,'20060603'
    Union all select 'A','cc',33,'20060602'
    Union all select 'B','dd',44,'20060602'
    Union all select 'B','ee',55,'20060601'
    Union all select 'C','ff',66,'20060601'
    Union all select 'D','gg',77,'20060604'
    Union all select 'D','hh',88,'20060602'
    Union all select 'D','jj',99,'20060601'Select * from @t A where (Select count(*) from @t where 客户=A.客户 and 价钱<A.价钱)<2
    order by 客户 asc,交易日期 desc
      

  3.   

    回楼上~你的方法不行~~你这个是知道表中有多少数据呀~如果很多几千条~~
    我的表其实已经是通过join来的~只是选取其中的关键部分,而且还不知道有多少数据,只是为了好表达我说的意思就随便写了几个数据
      

  4.   

    例如求:各客户的前2次交易历史; 
    ------------------------------------
    LZ给出的要求和结果示例数据不相符。如果如上,则A的类应为:
    A                aa              11          20060601  
    A                cc              33          20060602  这样的才是升序前两个...
      

  5.   

    if exists(select * from sysobjects where name='Tb' and xtype='U') drop table Tb
    Go
    --生成测试用数据,为SELECT初始环境CREATE TABLE TB(客户 varchar(10),商品 varchar(20),价钱 int,交易日期 varchar(20))
    INSERT INTO TB SELECT 'A','aa', 11,'20060601'
    UNION ALL SELECT 'A','bb', 22,'20060603'
    UNION ALL SELECT 'A','cc', 33,'20060602'  
    UNION ALL SELECT 'B','dd', 44,'20060602'
    UNION ALL SELECT 'B','ee', 55,'20060601'  
    UNION ALL SELECT 'C','ff', 66,'20060601'
    UNION ALL SELECT 'D','gg', 77,'20060604' 
    UNION ALL SELECT 'D','hh', 88,'20060602'
    UNION ALL SELECT 'D','jj', 99,'20060601'--select * from Tb
    select ID=identity(int,1,1),* into #Temp from Tb
    GO
    select * from #Temp T 
    where ID in (
      select top 2 id from #temp where 客户=T.客户 order by 交易日期 desc)--此处的排序情况,可根据自己的实际情况来更改,LZ的示例要求中的排序不是很明确GO--删除测试散数据
    DROP TABLE Tb
    DROP TABLE #Temp/* -- 结果 --ID          客户         商品                   价钱          交易日期                 
    ----------- ---------- -------------------- ----------- -------------------- 
    2           A          bb                   22          20060603
    3           A          cc                   33          20060602
    4           B          dd                   44          20060602
    5           B          ee                   55          20060601
    6           C          ff                   66          20060601
    7           D          gg                   77          20060604
    8           D          hh                   88          20060602(所影响的行数为 7 行)*/ -- 完成 --
      

  6.   

    --把上面的语句中的那句,更为:
    select 客户,商品,价钱,交易日期 from #Temp T 
    where ID in (
      select top 2 id from #temp where 客户=T.客户 )
    order by 客户,交易日期 desc--结果好像和你的相符,你自己看看:
    客户         商品                   价钱          交易日期                 
    ---------- -------------------- ----------- -------------------- 
    A          bb                   22          20060603
    A          aa                   11          20060601
    B          dd                   44          20060602
    B          ee                   55          20060601
    C          ff                   66          20060601
    D          gg                   77          20060604
    D          hh                   88          20060602(所影响的行数为 7 行)
      

  7.   

    不过wwh999(印钞机的卖 V2.0...发梦ing)兄弟借用临时表可用性更大一些!
    厉害!!:)