Advanced SQL Statement

Posted on 2011-10-07
Last Modified: 2012-05-12
I am having trouble trying to create the correct SQL Statement to pull the required records from the table in the image below.
Filters TableThere are lots of records in the table, but I want to pull the MainId from the table when a couple of conditions are met.
I need something like:
Select MainID From MainFilters Where (FilterId=4 AND FilterValues='9') AND (FilterId=9 AND FilterValues='43')
but only that doesn't appear to work? is it because both conditions are in the same record?

Question by:harris9999
    LVL 82

    Expert Comment

    by:Dave Baldwin
    It needs to be:

    Select MainID From MainFilters Where (FilterId=4 AND FilterValues='9') OR (FilterId=9 AND FilterValues='43')

    OR will select either condition.  Without OR, you can't match two different values in the same column and row.  You can't have both '4' and '9' as FilterId values at the same time.
    LVL 3

    Author Comment

    I need both conditions met.  
    Is that not possible in any way?
    LVL 142

    Accepted Solution

    then you need to do something like
    Select f.MainID 
    From MainFilters f
    Where (f.FilterId=4 AND f.FilterValues='9')
     AND EXISTS( SELECT NULL FROM MainFilters o WHERE (o.FilterId=9 AND o.FilterValues='43' AND o.MainID = f.MainID ) )

    Open in new window

    LVL 3

    Author Comment

    That looks like it works alright.
    And can that be extended if 3 or more conditions needed met?
    FilterId=4 AND FilterId=9
    FilterId=9 AND FilterId=43
    FilterId=5 AND FilterId=26
    FilterId=3 AND FilterId=3
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    for every other condition you add another AND EXISTS() condition ...
    LVL 3

    Author Comment

    Thanks for your help!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now