MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 18th, 2013, 10:54 AM
bbrunof bbrunof is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 bbrunof User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 9 m 39 sec
Reputation Power: 0
Query results ignoring multiple WHERE/AND funtions

I am trying to pull from multiple tables, across two databases, with multiple where/and functions but the results are not obeying all the where/and functions. Here is the code:

select a.PRACCT, a.PRSTAT, b.NStat, c.DISPCODE
FROM ODS.dbo.tblZTDPPRI a
inner join ODS.dbo.tblNotes b
ON a.PRACCT=b.Account_Number
inner join LSDM.dbo.tblAscensionReturnFile c
ON a.PRACCT=c.AccountNumber
WHERE b.NSTAT='W'
AND a.PRSTAT<>'B01' AND a.PRSTAT<>'B02' AND a.PRSTAT<>'B03' AND a.PRSTAT<>'B04' AND a.PRSTAT<>'B05' AND a.PRSTAT<>'B06' AND a.PRSTAT<>'B07' AND a.PRSTAT<>'B08' AND a.PRSTAT<>'B09' AND a.PRSTAT<>'B10' AND a.PRSTAT<>'B11' AND a.PRSTAT<>'B12' AND a.PRSTAT<>'B13' AND a.PRSTAT<>'B14' AND a.PRSTAT<>'B15' AND a.PRSTAT<>'B16' AND a.PRSTAT<>'B17' AND a.PRSTAT<>'B18' AND a.PRSTAT<>'B19' AND a.PRSTAT<>'B20' AND a.PRSTAT<>'B33' AND a.PRSTAT<>'B34' AND a.PRSTAT<>'B36' AND a.PRSTAT<>'B37' AND a.PRSTAT<>'B95'
AND c.DISPCODE='30' or c.DISPCODE='2' or c.DISPCODE='88'

The results are in line with the 'DISPCODE' rule, returning only 30, 2, and 88 values. However, the results are returning incorrect values for PSTAT and NStat. For instance, some rows have a PSTAT of 'B95' and/or NStat of 'K' even though, I think, the query should not allow it. Any help is appreciated!

Reply With Quote
  #2  
Old January 18th, 2013, 11:10 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,349 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 7 h 17 m 46 sec
Reputation Power: 390
This is a problem of precedence, use parenthesis to ensure the right evaluation order

Code:
   and (c.DISPCODE = 30
    or  c.DISPCODE = 2
    or  c.DISPCODE = 88)


The above code can be simplified as

Code:
   and c.DISPCODE in (30,2,88)


and likewise for the comparisons for the PRSTAT column
Comments on this post
bbrunof agrees!

Reply With Quote
  #3  
Old January 18th, 2013, 11:27 AM
bbrunof bbrunof is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 bbrunof User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 9 m 39 sec
Reputation Power: 0
Awesome.. worked, thanks.

Is there a reason why the DISPCODE condition required parenthesis to work and the others didnt?

And, I am receiving an error when trying to simplify the PRSTAT conditions. This is what I am trying:

AND a.PRSTAT not in (B01,B02,...)

Error says that B01 is an invalid column name. Simplifying worked with the DISPCODE condition you suggested.

Reply With Quote
  #4  
Old January 18th, 2013, 11:52 AM
bbrunof bbrunof is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 bbrunof User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 9 m 39 sec
Reputation Power: 0
Solved my 2nd question, thanks

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsDatabasesMySQL Help > Query results ignoring multiple WHERE/AND funtions

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap