我试图从总分钟计数中得到小时和分钟。我从其他网站获得了这段代码,但第一部分似乎不起作用:
cast(TimeToAdd/60 as varchar) + ' hours ' +
cast(TimeToAdd%60 as varchar) + ' minutes' as [Converted]
TimeToAdd价值75分钟,答案应该是1小时15分钟,但我得到的只是0小时15分钟。
这是完整的SQL:
select pu.ProjectID, c.ClientName, p.ProjectTitle, sum(TimeToAdd) as TotalTime,
cast(TimeToAdd/60 as varchar) + ' hours ' +
cast(TimeToAdd%60 as varchar) + ' minutes' as [Converted]
from dbo.ProjectUsers pu
left join dbo.Projects p
on pu.ProjectID = p.ProjectID
left join dbo.ProjectTime pt
on p.ProjectID = pt.ProjectID
inner join dbo.Clients c
on p.ClientID = c.ClientID
where pu.StaffID = 3
group by pu.ProjectID, c.ClientName, p.ProjectTitle, pt.TimeToAdd
有什么建议吗?
答案 0 :(得分:2)
使用子选择,以便您可以按名称引用SUMmed列:
select
*,
cast(TotalTime/60 as varchar) + ' hours ' +
cast(TotalTime%60 as varchar) + ' minutes' as [Converted]
from (
select
pu.ProjectID,
c.ClientName,
p.ProjectTitle,
sum(TimeToAdd) as TotalTime
from dbo.ProjectUsers pu
left join dbo.Projects p on pu.ProjectID = p.ProjectID
left join dbo.ProjectTime pt on p.ProjectID = pt.ProjectID
inner join dbo.Clients c on p.ClientID = c.ClientID
where pu.StaffID = 3
group by
pu.ProjectID,
c.ClientName,
p.ProjectTitle
) s
答案 1 :(得分:0)
这对我有用,如下所示:
DECLARE @TimeToAdd INT
SET @TimeToAdd = 75
DECLARE @Hours nvarchar(50)
DECLARE @Minutes nvarchar(50)
SELECT @Hours = CAST(@TimeToAdd/60 as nvarchar) + ' hours'
SELECT @Minutes = CAST(@TimeToAdd%60 as nvarchar) + ' minutes'
PRINT @Hours
PRINT @Minutes
输出:
1 hours
15 minutes
答案 2 :(得分:0)
select pu.ProjectID, c.ClientName, p.ProjectTitle, sum(TimeToAdd) as TotalTime,
cast(FLOOR(sum(TimeToAdd)/60.0) as varchar) + ' hours ' +
cast(sum(TimeToAdd)%60 as varchar) + ' minutes' as [Converted]
from dbo.ProjectUsers pu
left join dbo.Projects p
on pu.ProjectID = p.ProjectID
left join dbo.ProjectTime pt
on p.ProjectID = pt.ProjectID
inner join dbo.Clients c
on p.ClientID = c.ClientID
where pu.StaffID = 3
group by pu.ProjectID, c.ClientName, p.ProjectTitle // please remove (I guess), pt.TimeToAdd