?
Solved

Select statement with and/or conditions

Posted on 2013-05-17
2
Medium Priority
?
265 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 66

Accepted Solution

by:
Jim Horn earned 2000 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 66

Expert Comment

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

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

777 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