Question: Using sum with order by clause in SQL Server

Question

Using sum with order by clause in SQL Server

Answers 2
Added at 2016-12-31 12:12
Tags
Question

I want to see data as per trainer who worked for the maximum hours for previous 3 months (i.e 90 days). 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)

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
Subquery returned more than 1 value. 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
Subquery returned more than 1 value. 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
Answers
nr: #1 dodano: 2016-12-31 12:12

..group by trainer in the sub-query is the reason for error. 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

;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
nr: #2 dodano: 2016-12-31 12:12

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 
Source Show
◀ Wstecz