Solved

Select statement with and/or conditions

Posted on 2013-05-17
2
253 Views
Last Modified: 2013-05-17
I have a select statement that contains multiple (and) conditions as well as an (or) condition.
The or portion doesn't seem to work.  I was wondering if I could get another set of eyes to look at this to see what I'm doing wrong.
I basically have data in this file with the original company as one of the fields and I'm passing a parameter for the company number but I have one exception.
I want all the records that contain that company number to be selected or (if the company number in the record is not equal to the company parameter passed and the type is 'INT'

select      tfgld106.*, tfgld008.*
            from       tfgld106, tfgld008
            where      tfgld106._index1 = {:ttyp.f, :docn.f}
            and     tfgld106.fyer = :year.f      
            and      tfgld106.leac inrange :leac.f and :leac.t
            and      tfgld106.fprd inrange :prod.f and :prod.t
            and      tfgld106.catg <> tfgld.catg.opening.bal
            and      tfgld106.dele <> tcyesno.yes
            and      tfgld106.leac refers to tfgld008
            and      tfgld008.subl = 0
            and      (tfgld106.ocmp = :company.number or
                         tfgld106.ocmp <> :company.number and tfgld106.otyp = "INT")


My records would look something like this:
otyp        Leac     ocmp      fprd
AB          00005    670          4
NON       00010    670          4
INT         00015    683          4

My company parameter would be equal to 670
I would want the select statement to pull all of the records above
0
Comment
Question by:mmthomasmalone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39176149
Probably need another set of parenthesis to set the order of execution

            and      (tfgld106.ocmp = :company.number or
                         (tfgld106.ocmp <> :company.number and tfgld106.otyp = "INT"))
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39176254
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

763 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