sql server - SQL how to format time containing an aggregate or a subquery -
please help. having little bit of problem here , need figuring out. have query returns multiple rows , when try format time display following message...
msg 130, level 15, state 1, line 3 cannot perform aggregate function on expression containing aggregate or subquery.
here format used when error...
cast(sum(datediff(second, 0, entrytime)) / 3600 varchar(12)) + ':' + right('0' + cast(sum(datediff(second, 0, entrytime)) / 60 % 60 varchar(2)), 2) + ':' + right('0' + cast(sum(datediff(second, 0, entrytime)) % 60 varchar(2)), 2)
which when run in in query correct format like...
project name total time aaa 0:00:19 bbb 0:00:04 ccc 6:00:06 ddd 16:05:52
but, can't seem figure out how format following query display time in manner...
select projname 'project name', convert(varchar(10), sum(case when phone = 0 , research = 0 (datediff(second, 0, entrytime)) end)) [total time no pmre], convert(varchar(10), sum(case when phone = 1 , research = 0 (datediff(second, 0, entrytime)) end)) [total time phone], convert(varchar(10), sum(case when phone = 0 , research = 1 (datediff(second, 0, entrytime)) end)) [total time research], convert(varchar(10), sum(datediff(second, 0, entrytime)) ) 'total time' worktime entrytime not null group projname
and following result...
project name total time no pmre total time phone total time research total time aaa 19 null null 19 bbb 4 null null 4 ccc 10800 7200 3606 21606 ddd 57952 null null 57952
all of correct need in figuring out how format time display hrs:mins:secs... actually, don't need seconds, hours , minutes advice, help, pointing in right direction wonderful. thank you!
try:
with cte (select projname , sum(case when phone = 0 , research = 0 datediff(second, 0, entrytime) end) [total time no pmre], sum(case when phone = 1 , research = 0 datediff(second, 0, entrytime) end) [total time phone], sum(case when phone = 0 , research = 1 datediff(second, 0, entrytime) end) [total time research], sum(datediff(second, 0, entrytime)) [total time] worktime entrytime not null group projname) select projname [project name], cast([total time no pmre] / 3600 varchar(12)) + ':' + right('0' + cast([total time no pmre] / 60 % 60 varchar(2)), 2) + ':' + right('0' + cast([total time no pmre] % 60 varchar(2)), 2) [total time no pmre], cast([total time phone] / 3600 varchar(12)) + ':' + right('0' + cast([total time phone] / 60 % 60 varchar(2)), 2) + ':' + right('0' + cast([total time phone] % 60 varchar(2)), 2) [total time phone], cast([total time research] / 3600 varchar(12)) + ':' + right('0' + cast([total time research] / 60 % 60 varchar(2)), 2) + ':' + right('0' + cast([total time research] % 60 varchar(2)), 2) [total time research], cast([total time] / 3600 varchar(12)) + ':' + right('0' + cast([total time] / 60 % 60 varchar(2)), 2) + ':' + right('0' + cast([total time] % 60 varchar(2)), 2) [total time] cte
Comments
Post a Comment