SQL qyestion

Posted on 2009-12-23
Last Modified: 2012-05-08
I have this SQL:

Select acct, acct_unit1, trans_date, ref, voucher, from_id, ref_type, dom_amount  From ledger
WHERE     (trans_date >= '01/01/2009') AND (trans_date <= '12/31/2009')

Which works fine but I have additional criteria that needs to be filtered withen that date-range

AND ((acct = '60500') AND (acct_unit1 >= '3000') AND (acct_unit1 <= '3110'))  OR ((acct >= '61000') AND (acct_unit1 >= '3000') AND (acct_unit1 <= '3110')) OR ((acct >= '61000') AND (acct_unit1 >= '4000') AND (acct_unit1 <= '4100'))
Question by:thenrich
    LVL 10

    Expert Comment

    where is the problem?

    for now I suggest using between and slightly different formatting for improved legibility
    Select acct, acct_unit1, trans_date, ref, voucher, from_id, ref_type, dom_amount  
    From ledger
    WHERE  trans_date between '01/01/2009' AND trans_date <= '12/31/2009'
    		acct in ('60500', '61000') 
    		and acct_unit1 between '3000' and '3110'
    		acct >= '61000' 
    		and acct_unit1 between '4000' and '4100'

    Open in new window

    LVL 51

    Accepted Solution

    Does this work?

    SELECT acct,
      FROM ledger
     WHERE (trans_date BETWEEN '01/01/2009' AND '12/31/2009')
       AND (((acct =  '60500') AND (acct_unit1 BETWEEN '3000' AND '3110'))
        OR  ((acct >= '61000') AND (acct_unit1 BETWEEN '3000' AND '3110'))
        OR  ((acct >= '61000') AND (acct_unit1 BETWEEN '4000' AND '4100')))

    Open in new window


    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    Join & Write a Comment

    I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    This video discusses moving either the default database or any database to a new volume.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    746 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

    16 Experts available now in Live!

    Get 1:1 Help Now