简体   繁体   English

在SQL Server中将sum与order by子句一起使用

[英]Using sum with order by clause in SQL Server

I want to see data as per trainer who worked for the maximum hours for previous 3 months (ie 90 days). 我想查看每位培训师在过去3个月(即90天)内最长工作时间的数据。 I created a stored procedure to view the sum of hours in the previous 3 months, now I just want to add group by clause ( group by trainer ) 我创建了一个存储过程来查看前三个月的小时总数,现在我只想添加group by子句( group by trainer

Here is my stored procedure, please do suggest me the changes what I need to do 这是我的存储过程,请向我建议我需要做的更改

begin      
declare @h int      
declare @m int      
declare @tm int      
declare @min int      
declare @count int  
declare @d varchar(30)
declare @dt datetime
declare @d1 varchar(30)
declare @dt2 datetime

declare @dt1 datetime

set @dt = (select convert(datetime, (dateadd(day, -90, getdate())), 105))
set @d = (select convert(varchar, @dt, 105))

set @dt1 = (select convert(datetime, getdate(), 105))
set @d1 = (select convert(varchar, @dt1, 105))
set @dt2 = (select convert(datetime, @d1, 105))

set @h = (SELECT SUM(DATEPART(hh, (convert(datetime, hrs, 1))))       
          FROM sonvininsert 
          WHERE date BETWEEN convert(datetime, @d, 105) AND convert(datetime, @d1, 105)      
            AND instructore = 'primary' 
            AND status = '0' 
          GROUP BY trainer)      

set @tm = (SELECT SUM(DATEPART(mi, (convert(datetime, hrs, 1))))       
           FROM sonvininsert 
           WHERE date BETWEEN convert(datetime, @d, 105) AND convert(datetime, @d1, 105)      
             AND instructore = 'primary'   
             AND status = '0' 
           GROUP BY trainer)      

set @m = @tm / 60      
set @min = @tm % 60      
set @h = @h + @m      

select @h as hour
end 

The following error occurs: 发生以下错误:

Msg 512, Level 16, State 1, Line 25 消息512,第16级,状态1,第25行
Subquery returned more than 1 value. 子查询返回的值超过1。 This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。

Msg 512, Level 16, State 1, Line 30 消息512,第16级,状态1,第30行
Subquery returned more than 1 value. 子查询返回的值超过1。 This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。

I want my query to be like 我希望我的查询像

group by trainer 
order by trainer desc

..group by trainer in the sub-query is the reason for error. 子查询中的..group by trainer是错误的原因。 The sub-query will find the sum for each trainer but you are assigning to variable. 子查询将找到每个培训员的总和,但是您要分配给变量。

Here is one way to do this for all trainers 这是为所有trainers一种方法

;WITH cte
     AS (SELECT trainer,
                Sum(Datepart(hh, ( CONVERT(DATETIME, hrs, 1) ))) AS H,
                Sum(Datepart(mi, ( CONVERT(DATETIME, hrs, 1) ))) AS tm
         FROM   sonvininsert
         WHERE  date BETWEEN CONVERT(DATETIME, @d, 105) AND CONVERT(DATETIME, @d1, 105)
                AND instructore = 'primary'
                AND status = '0'
         GROUP  BY trainer)
SELECT trainer,
       m = tm / 60,
       [min] = tm % 60,
       [hour] = h + m
FROM   cte 
ORDER BY trainer desc

Try something like..... 尝试类似.....

begin      
declare @h int ,@m int ,@tm int , @min int , @count int ,@d varchar(30)
       ,@dt datetime , @d1 varchar(30) , @dt2 datetime , @dt1 datetime

select @dt  = convert(datetime,(dateadd(day,-90,getdate())),105)    
select @d   = convert(varchar,@dt,105) 
select @dt1 = convert(datetime,getdate(),105)
select @d1  = convert(varchar,@dt1,105)
select @dt2 = convert(datetime,@d1,105)

select trainer 
     , sum(DATEPART(hh,(convert(datetime,hrs,1))))    
       + sum(DATEPART(mi,(convert(datetime,hrs,1)))) / 60 AS [Hour]
FROM sonvininsert 
where date  between convert(datetime,@d,105) 
                and convert(datetime,@d1,105)      
and instructore='primary' 
and status='0' 
group by trainer     

end 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM