统计每日盈利最多的信息

--用户投注表历史表
create table t_Userinfo_his (
   rid            int              identity,
   userid               int                  not null, --用户ID
   nick                 varchar(50)          not null, --用户昵称
   termid               int                  not null, --比赛ID
   nums                 int                  not null, --投注数字
   instone              bigint               not null, --投入金额
   outstone             bigint               not null, --返还金额
   addtime              datetime             not null, --投注时间   
)
go



--功能:统计每日盈利最多的用户
--作者:测试人员
--创建日期:2011-04-12
create procedure p_daystar
@date    datetime --从哪天开始统计
as
set nocount on
set transaction isolation level read uncommitted
set xact_abort on            
    --创建一个表变量,存储用户ID,用户当天总盈利,日期
    declare @tb table(userid int,outstone bigint,addtime char(10))
    insert @tb select userid,sum(outstone) as outstone,convert(char(10),addtime,20) as addtime from t_userinfo_his with(nolock)     
        where convert(char(10),addtime,20) >= @date
        group by userid ,convert(char(10),addtime,20)    
    --select * from @tb
    select addtime,max(outstone) as outstone ,userid=(select userid from @tb where addtime=a.addtime and outstone=max(a.outstone)) from @tb as a        
    group by addtime
go
原文地址:https://www.cnblogs.com/mxw09/p/2014319.html