• TryingToLearn (5/3/2013)


    I figured it out....Thank you,

    I would like to return all the data for a single empnum, but this query is returnng data for ALL empnum's. any suggestion would be greatly appreciated.

    select E.empnum, rtrim(C.lname) + ', ' + rtrim(C.fname) as Name,

    rtrim(substring(C.dept,1,3)) as Office,

    C.dept,

    calldate,

    duration_in_decimal,

    io

    from CALLS C INNER JOIN EMP E on C.empnum = E.empnum and C.empnum = 6396 where

    (month(calldate) = case month('2012/11/30') when '1' then '12' else month('2012/11/30') - 1 end and

    year(calldate) = case month('2012/11/30') when '1' then year('2012/11/30') - 1 else year('2012/11/30') end and duration_in_decimal > .0055555

    and (len(phonenum) IN (7,10,11))) or (month(calldate) = case month('2012/11/30') when '1' then '12' else month('2012/11/30') - 1 end and

    year(calldate) = case month('2012/11/30') when '1' then year('2012/11/30') - 1 else year('2012/11/30') end and io = 'I')

    ORDER BY C.empnum;

    You should move that to the where clause.

    So let's look at your where clause a little bit here...

    If you have anything resembling a lot of data this is going to crawl like a snail in frozen molasses. You have all sorts of nonSARGable predicates and tons of implicit conversions.

    I suspect that what you are doing is creating a big long sql string in your application and then executing it? This code could be greatly improved from a performance standpoint.

    Try moving your empnum check to the where clause and see if that helps. Then if you want some help to make this run faster we can take a look.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/