在 SQL 服务器中将 sum 与 order by 子句一起使用
Using sum with order by clause in SQL Server
我想查看前 3 个月(即 90 天)工作时间最长的培训师的数据。我创建了一个存储过程来查看前 3 个月的小时总和,现在我只想添加 group by 子句 (group by trainer
)
这是我的存储过程,请给我建议我需要做的更改
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
出现以下错误:
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.
我希望我的查询像
group by trainer
order by trainer desc
子查询中的..group by trainer
是错误原因。子查询将找到每个培训师的总和,但您正在分配给变量。
这是为所有人执行此操作的一种方法 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
尝试类似...
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
我想查看前 3 个月(即 90 天)工作时间最长的培训师的数据。我创建了一个存储过程来查看前 3 个月的小时总和,现在我只想添加 group by 子句 (group by trainer
)
这是我的存储过程,请给我建议我需要做的更改
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
出现以下错误:
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.
我希望我的查询像
group by trainer
order by trainer desc
..group by trainer
是错误原因。子查询将找到每个培训师的总和,但您正在分配给变量。
这是为所有人执行此操作的一种方法 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
尝试类似...
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