Solved

SQL Joins - Date Criteria being ignored

Posted on 2011-09-08
2
223 Views
Last Modified: 2012-05-12
Hi,

I've got a stored procedure that returns data into a form.  This was working well until I was tasked with also adding another table.

I have a main dbAppointments table which has data like this:

appID          fileID          feeusrID         appDate
100             283283      7459               2010-02-06 01:00:00.000
101             285674      7125               2010-05-06 02:00:00.000
102             287444      7459               2010-06-08 02:00:00.000

and I have a secondary table udApptExtraInvite which has data like this:

origAppID    appFeeUsrID
100              7220
101              7459; 7220

I need all appID's and origAppID's that have, for example, 7459 as the dbAppointments.feeusrID or contained within udApptExtraInvite.appFeeUsrID and I need date filtering.  All origAppID's from udApptExtraInvite are viable appID's in dbAppointments.

Note, there are appID's in the dbAppointments table with a feeusrID of 7459 that are not in the udApptExtraInvite table as origAppID.

This is part of the procedure for testing.  My issue is the correct data comes back but when I add the date filter, the script basically is only removing the applicable entries from the udApptExtraInvite table and not filtering anything further.  If I remove "OR AEI.appFeeUsrID like @FEES" the date filtering works correctly.

How do I resolve this?  Any suggestions would be appreciated.

Declare @FEEUSRID as bigint
SET @FEEUSRID = '7459'
Declare @FEES as varchar(50)
set @fees = '%' + convert(varchar, @FEEUSRID) + '%'

SELECT dbo.dbAppointments.*,
dbo.dbuser.usrFullName,
dbo.dbClient.clNo + '/' + dbo.dbFile.fileNo AS clfileno,
dbo.dbFile.fileDesc,
dbo.dbClient.clName
FROM        
dbo.dbAppointments
INNER JOIN dbo.dbFile ON dbo.dbAppointments.fileID = dbo.dbFile.fileID
INNER JOIN dbo.dbUser ON dbo.dbAppointments.feeusrID = dbo.dbUser.usrID
INNER JOIN dbo.dbClient ON dbo.dbFile.clID = dbo.dbClient.clID
LEFT JOIN udapptextrainvite AEI ON dbo.dbAppointments.appID = AEI.origAppID

WHERE
dbo.dbAppointments.feeusrid = coalesce(7459, dbo.dbAppointments.feeusrid)  
OR AEI.appFeeUsrID like @FEES  
AND (dbo.dbappointments.appdate > '2011-09-30 01:00:00.000')
order by appid
0
Comment
Question by:smsstech
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 36502630
your OR and AND mixing in the where clause is the issue
please try
WHERE ( dbo.dbAppointments.feeusrid = coalesce(7459, dbo.dbAppointments.feeusrid)   
     OR AEI.appFeeUsrID like @FEES  
      )
  AND dbo.dbappointments.appdate > '2011-09-30 01:00:00.000'
order by appid

Open in new window

0
 

Author Closing Comment

by:smsstech
ID: 36502663
Sigh.  Thanks my friend.  Perhaps I've been looking at this too long.  I definitely appreciate the help.  :)
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question