Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Inner join returning all rows. Expand / Collapse
Author
Message
Posted Friday, May 3, 2013 7:14 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, July 29, 2014 7:01 AM
Points: 51, Visits: 303
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;
Post #1449185
Posted Friday, May 3, 2013 7:59 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:14 PM
Points: 13,139, Visits: 11,980
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1449215
Posted Friday, May 3, 2013 10:45 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Tuesday, July 29, 2014 4:39 PM
Points: 4,576, Visits: 8,342
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 ( -- subquery returnig beginning of the curent(?) month. This approach allows us to do the conversion and month calculations just once
SELECT DATEADD(mm, DATEDIFF(mm, 0, CONVERT(DATETIME, '20121130', 112)), 0) MonthStart ) CM
-- selecting the range of dates for calldate - the way allowing to use an index
ON calldate >= DATEADD(mm, -1, MonthStart) AND calldate < MonthStart
INNER JOIN EMP E ON C.empnum = E.empnum
WHERE C.empnum = 6396 -- following Sean's sound advice
AND (
-- now the conditions look pretty straight forward. Please correct it if I made a mistake somewhere.
-- it was not so easy to find the ends in the original version
(duration_in_decimal > .0055555 AND LEN(phonenum) IN (7,10,11))
OR io = 'I'
)
ORDER BY C.empnum;

And one last comment.
I'm pretty sure the table CALLS has a clustered index with 1st column "calldate". Right?
Well, why am I asking? It just cannot be any other way.
Silly me.
Post #1449390
Posted Saturday, May 4, 2013 6:38 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, July 29, 2014 7:01 AM
Points: 51, Visits: 303
Thank you...
Post #1449415
Posted Saturday, May 4, 2013 6:39 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, July 29, 2014 7:01 AM
Points: 51, Visits: 303
Thank you..
Post #1449416
Posted Tuesday, May 7, 2013 2:13 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, July 29, 2014 7:01 AM
Points: 51, Visits: 303
really appreciate your help. This code was written 10 years ago....and i am a little new to coding.

SELECT DATEADD(mm, DATEDIFF(mm, 0, CONVERT(DATETIME, '20121130', 112)), 0) MonthStart ) CM
ON calldate >= DATEADD(mm, -1, MonthStart) AND calldate < MonthStart



Could you pseudo code explain what is happening in this block of code? what i thought it was doing was getting all data for this employee for the month i pass in(20121130). what the CM relates to is confusing me..
Post #1450326
Posted Tuesday, May 7, 2013 2:35 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:14 PM
Points: 13,139, Visits: 11,980
TryingToLearn (5/7/2013)
really appreciate your help. This code was written 10 years ago....and i am a little new to coding.

SELECT DATEADD(mm, DATEDIFF(mm, 0, CONVERT(DATETIME, '20121130', 112)), 0) MonthStart ) CM
ON calldate >= DATEADD(mm, -1, MonthStart) AND calldate < MonthStart



Could you pseudo code explain what is happening in this block of code? what i thought it was doing was getting all data for this employee for the month i pass in(20121130). what the CM relates to is confusing me..


You are missing the very critical piece of that which is the line above it.


INNER JOIN ( -- subquery returnig beginning of the curent(?) month. This approach allows us to do the conversion and month calculations just once


This is joining to the subselect inside the parenthesis. The CM is the alias for the subselect.

I tried to reformat this a little bit so it might be easier to see what is going on here.

INNER JOIN 
( -- subquery returnig beginning of the curent(?) month. This approach allows us to do the conversion and month calculations just once
SELECT DATEADD(mm, DATEDIFF(mm, 0, CONVERT(DATETIME, '20121130', 112)), 0) MonthStart
) CM ON calldate >= DATEADD(mm, -1, MonthStart) AND calldate < MonthStart
-- selecting the range of dates for calldate - the way allowing to use an index

Does this help?


_______________________________________________________________

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1450335
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse