有两张表
表1存储用户的基本信息
"userid" (主键)
"用户姓名"
.
.
.
表2:存储所用用户的购买信息
"userid"
"时间"
"购买金额"现在是想查询某位用户具体的 "姓名", "最近一次消费记录", "平均每月消费金额"该怎样去做这样的一个查询?知道具体的userid

解决方案 »

  1.   

    求:最近一次消费记录", "平均每月消费金额"
    --》》最好写成两个sql。最近一次消费记录是一条,而每月平均消费金额可能是多条记录,写不到一块去。最近一次消费记录", 
    select 姓名,时间,购买金额
    from tb t
    where not exists(select 1 from tb wher 姓名=t.姓名 and 时间>t.时间)平均每月消费金额":
    select 姓名,convert(char(8),时间),avg(金额)
    from tb
    group by 姓名,convert(char(8),时间)
      

  2.   

    declare @userid int
    set @userid=3---这里取你要查询的用户IDCREATE TABLE #a(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [userid] [int]  NULL ,
    [用户姓名][nvarchar](50) NULL
     
    )
    CREATE TABLE #b(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [userid] [int]  NULL ,
    [时间] datetime NULL,
    [购买金额] decimal(12,2) NULL
     
    )
     
    insert into #a
    select 1,'张三' union all
    select 2,'李四' union all
    select 3,'王五' union all
    select 4,'赵六' insert into #b
    select 1,'2012-01-01',10.00 union all
    select 1,'2012-02-01',21.00 union all
    select 1,'2012-03-01',32.00 union all
    select 1,'2012-04-01',43.00 union all
    select 2,'2012-01-01',10.00 union all
    select 2,'2012-02-01',24.00 union all
    select 2,'2012-03-01',35.00 union all
    select 2,'2012-04-01',46.00 union all
    select 3,'2012-01-01',10.00 union all
    select 3,'2012-02-01',25.00 union all
    select 3,'2012-03-01',34.00 union all
    select 3,'2012-04-01',43.00 union all
    select 4,'2012-01-01',10.00 union all
    select 4,'2012-02-01',22.00 union all
    select 4,'2012-03-01',33.00 union all
    select 4,'2012-04-01',44.00  
     
    select top 1 a.用户姓名,b.时间,b.购买金额,(select avg(购买金额) from #b where userid=@userid) 平均消费 
    from #a a  join #b b on a.userid=b.userid 
    where a.userid=@userid
    order by b.时间 desc
     
    drop table #a
    drop table #b-----查询结果用户姓名 时间 购买金额 平均消费
    王五 2012-04-01 00:00:00.000 43.00 28.000000
    (4 行受影响)(16 行受影响)(1 行受影响)
      

  3.   


    -- 最近一次消费记录
    select b.用户姓名,a.时间,a.购买金额
    from 表2 a
    inner join 表1 b on a.userid=b.userid
    inner join
    (select userid,max(时间) '时间' 
     from 表2 where userid=[userid]) c
    on a.userid=c.userid and a.时间=c.时间
    where a.userid=[userid]-- 平均每月消费金额
    select b.用户姓名,
           rtrim(datepart(mm,a.时间))+'月' '月份',
           avg(a.购买金额) '平均消费金额'
    from 表2 a
    inner join 表1 b on a.userid=b.userid
    where a.userid=[userid]
    group by datepart(mm,a.时间)
      

  4.   


    declare @userid int
    declare @avgMoney decimal(12,2)
    set @userid=4---这里取你要查询的用户IDCREATE TABLE #a(
        [ID] [int] IDENTITY(1,1) NOT NULL,
        [userid] [int]  NULL ,
        [用户姓名][nvarchar](50) NULL
         
    )
    CREATE TABLE #b(
        [ID] [int] IDENTITY(1,1) NOT NULL,
        [userid] [int]  NULL ,
        [时间] datetime NULL,
        [购买金额] decimal(12,2) NULL
         
    )
     
    insert into #a
    select 1,'张三' union all
    select 2,'李四' union all
    select 3,'王五' union all
    select 4,'赵六' insert into #b
    select 1,'2012-01-01',10.00 union all
    select 1,'2012-02-01',20.00 union all
    select 1,'2012-03-01',30.00 union all
    select 1,'2012-04-01',40.00 union all
    select 2,'2012-01-01',10.00 union all
    select 2,'2012-02-01',100.00 union all
    select 2,'2012-03-01',200.00 union all
    select 2,'2012-04-01',300.00 union all
    select 3,'2012-01-01',111.00 union all
    select 3,'2012-02-01',222.00 union all
    select 3,'2012-03-01',333.00 union all
    select 3,'2012-04-01',444.00 union all
    select 4,'2012-01-01',1.00 union all
    select 4,'2012-02-01',2.00 union all
    select 4,'2012-03-01',3.00 union all
    select 4,'2012-04-01',4.00  
    select @avgMoney =(select sum(购买金额)/datediff(m,min(时间),max(时间)) from #b where userid=@userid) 
    select top 1 a.用户姓名,b.时间,b.购买金额, @avgMoney  平均每月消费 
    from #a a  join #b b on a.userid=b.userid 
    where a.userid=@userid
    order by b.时间 desc
     
    drop table #a
    drop table #b---查询结果
    (4 行受影响)(16 行受影响)(1 行受影响)----------------------------------------
    用户姓名   时间     购买金额    平均每月消费赵六 2012-04-01 00:00:00.000 4.00 3.33