sql server - How to return multiple rows in sql from same table -
pretty new here , use help. have table
timeid(pk), dateentry(date), entrytiem(time), projid(int), projname(varchar), phone(bit), research(bit), notes(varchar), projactive(bit), timedatestamp(date).
i'm trying return multiple rows of data each projname not having luck. here i've come far...
select projname 'project name', (select 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) worktime phone = 0 , research = 0 , entrytime not null ) 'total time no pmre', (select 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) worktime phone = 1 , research = 0 , entrytime not null ) 'total time phone', (select 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) worktime phone = 0 , research = 1 , entrytime not null ) 'total time research', (select 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) worktime entrytime not null ) 'totaltime' worktime entrytime not null group projname
which returns following...
project name total time no pmre total time phone total time research total time aaaa 19:06:15 2:00:00 1:00:06 22:06:21 bbbb 19:06:15 2:00:00 1:00:06 22:06:21 cccc 19:06:15 2:00:00 1:00:06 22:06:21 dddd 19:06:15 2:00:00 1:00:06 22:06:21
which wrong. amount in each column total condition of projname added instead of separated out individually.
this output should be...
project name total time no pmre total time phone total time research total time aaaa 00:00:19 0:00:00 0:00:00 00:00:19 bbbb 00:00:04 0:00:00 0:00:00 00:00:04 cccc 03:00:00 2:00:00 1:00:06 06:06:06 dddd 16:05:52 0:00:00 0:00:00 16:05:52
does have suggestions or point me in right direction? thanks!
i think looking conditional sum:
select projname 'project name', sum(case when (phone = 0 , research = 0 , entrytime not null) datediff(second, 0, entrytime) else null end) 'total time no pmre', sum(case when (phone = 1 , research = 0 , entrytime not null) datediff(second, 0, entrytime) else null end) 'total time phone', sum(case when (phone = 0 , research = 1 , entrytime not null) datediff(second, 0, entrytime) else null end) 'total time research', sum(datediff(second, 0, entrytime)) 'totaltime' worktime entrytime not null group projname
add formatting code result of sum , fine
edit
answering comment format, take format "template":
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)
and replace every occurence of sum(datediff(second, 0, entrytime)
sum(case when (phone = 0 , research = 0 , entrytime not null) datediff(second, 0, entrytime) else null end)
or, can add formats in query wraps source query, this:
select 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) ... -- other columns ( select projname 'project name', sum(case when (phone = 0 , research = 0 , entrytime not null) datediff(second, 0, entrytime) else null end) 'total time no pmre', sum(case when (phone = 1 , research = 0 , entrytime not null) datediff(second, 0, entrytime) else null end) 'total time phone', sum(case when (phone = 0 , research = 1 , entrytime not null) datediff(second, 0, entrytime) else null end) 'total time research', sum(datediff(second, 0, entrytime)) 'totaltime' worktime entrytime not null group projname ) ds
hope helps you
Comments
Post a Comment