Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 which one is ideal

Author  Topic 

rajani
Constraint Violating Yak Guru

367 Posts

Posted - 2004-05-31 : 17:43:30
Hi friends

I have following query

select taskid,staffid,
[timespent]=(SELECT CASE WHEN sum(datediff(s,wt_starttime,wt_stoptime)) IS NULL THEN '0'
ELSE sum(datediff(s,wt_starttime,isnull(wt_stoptime,getdate()))) END -- total time spent
FROM pt_vCheckworktime where fk_taskid=t.taskid and fk_staffid=t.staffid)

from task t

and have same piece of code i.e in a UDF

((SELECT CASE WHEN sum(datediff(s,wt_starttime,wt_stoptime)) IS NULL THEN '0'
ELSE sum(datediff(s,wt_starttime,isnull(wt_stoptime,getdate()))) END -- total time spent
FROM pt_vCheckworktime where fk_taskid=t.taskid and fk_staffid=t.staffid))


I am wondering which one is better or ideal.

my first query or the following ??

select taskid,staffid,dbo.pt_fntimespent(t.taskid,t.staffid) from task

at the moment both queries taking same amount of time.
Thanks

Cheers

mr_mist
Grunnio

1870 Posts

Posted - 2004-06-01 : 03:12:18
They do the same thing. The function is probably better from a maintainability / useability point of view.

-------
Moo. :)
Go to Top of Page

Billpl
Yak Posting Veteran

71 Posts

Posted - 2004-06-03 : 00:23:52
Functions are great for reuse and maintenance even if they snip a little off performance.

But I'd have to say readability would take the first priority:


SUM(
Case When wt_starttime Is Null
Then '0'
When wt_stoptime Is Null
Then datediff( s, wt_starttime, GetDate() )
Else datediff( s, wt_starttime, wt_stoptime )
End
)

Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2004-06-03 : 01:48:20
Or, if wt_starttime is defined as NOT NULL in the schema, then eliminate the CASE statement and just use the second part: sum(datediff(s,wt_starttime,isnull(wt_stoptime,getdate())))

--------------------------------------------------------------
Find more words of wisdom at [url]http://weblogs.sqlteam.com/markc[/url]
Go to Top of Page

rajani
Constraint Violating Yak Guru

367 Posts

Posted - 2004-06-03 : 16:42:57
Thank u very much

Cheers
Go to Top of Page
   

- Advertisement -