表table :id,money(消费金额),point(积分),shuliang (购买数量) datime(购买日)
我现在想查询某个id在几月到几月之间的消费情况和总的消费情况。在一条sql中写出来、
如:id,money(消费金额),point(积分),shuliang (购买数量) datime(日期)
    50   200              50          10                    2001-1-1
    50   100              25          5                     2003-2-2
    20   300              75          15                    2001-1-1
我现在要查询id50的2001-12-30到2003-12-30的消费情况要得到的情况是:
id,money,point,shuliang  datime(日期)sum(point) sum(money) sum(shuliang
50   200     50    10        2001-1-1      75        300          15
50   100    25       5        2003-2-2      75        300          15
这个怎么写??????请高手解答

解决方案 »

  1.   

    2001-12-30到2003-12-30怎么会得出这样的结果?应该是2001-1-1到2003-12-30:
    declare @t table(id int,money int,point int,shuliang int, datime datetime)
    insert @t
    select    50,   200,  50,  10,   '2001-1-1' union all
    select    50,   100,  25,  5 ,   '2003-2-2' union all
    select    20,   300,  75,  15,   '2001-1-1'declare @id int,@begin datetime,@end datetime
    set @id = 50
    set @begin = '2001-1-1'
    set @end = '2003-12-30'
    SELECT a.*,b.sum_point,b.sum_money,b.sum_shuliang
    FROM @t AS a INNER JOIN
    (select id,
    sum(point) as sum_point,sum(money) as sum_money,sum(shuliang) as sum_shuliang 
    from @t where id = @id AND datime between @begin and @end group by id) AS b
    ON a.id = b.id WHERE a.id = @id AND a.datime between @begin and @end/*结果
    id   money   point  shuliang  datime    sum_point sum_money  sum_shuliang
    50   200     50     10        2001-1-1  75        300        15
    50   100     25     5         2003-2-2  75        300        15
    */