locked
SQL query -and/ or condition in where clause RRS feed

  • Question

  • Got it solved!!!

    No need for help.

    Best Sverre

    Hi

    Got the following SQL that retrieves non-correct rows. It only includes rows that fullfill this where-statement:

    (trn_group= 'CS' and trn_package_id=0 and report_date='2012-02-29' )

    I would also like to retrieve rows that fullfill the sub-select condition in the where clause. Something goes wrong with my and/or here but I dont know how to re-design the query. 

    I also wonder if I can replace the date 2012-02-29 with a parameter/variable instead ?

    Select * from DBO.sesam
    Where report_date='2012-02-29' And trn_group <>'IRS'
    and (trn_group= 'CS' and trn_package_id=0 and report_date='2012-02-29' )
    or (trn_package_id in(Select trn_package_id
    From dbo.sesam
    Where report_date='2012-02-29' and (portfolio = 'COV-FUND-HA-MM' and currency<>'SEK')
    Group by trn_package_id)
    and report_date=2012-02-29)

    Sverreberre


    Tuesday, March 6, 2012 2:12 PM

Answers

  • Adding some parenthesis and including OR conditions will get the results you are looking for.  It seem like you want everything from a single date with a bunch or OR condtions like my changes below.

    Select * from DBO.sesam
    Where report_date='2012-02-29' And ((trn_group= 'CS' and trn_package_id=0)
    or (trn_package_id in(Select trn_package_id)) or (portfolio = 'COV-FUND-HA-MM' and currency<>'SEK'))
    From dbo.sesam
    Group by trn_package_id


    jdweng


    No does not work. I get an error message. Thanks anyway

    Sverreberre

    • Proposed as answer by Bruce Song Monday, March 12, 2012 7:21 AM
    • Marked as answer by Bruce Song Wednesday, March 14, 2012 6:00 AM
    Tuesday, March 6, 2012 3:37 PM

All replies

  • Adding some parenthesis and including OR conditions will get the results you are looking for.  It seem like you want everything from a single date with a bunch or OR condtions like my changes below.

    Select * from DBO.sesam
    Where report_date='2012-02-29' And ((trn_group= 'CS' and trn_package_id=0)
    or (trn_package_id in(Select trn_package_id)) or (portfolio = 'COV-FUND-HA-MM' and currency<>'SEK'))
    From dbo.sesam
    Group by trn_package_id


    jdweng

    Tuesday, March 6, 2012 2:48 PM
  • Adding some parenthesis and including OR conditions will get the results you are looking for.  It seem like you want everything from a single date with a bunch or OR condtions like my changes below.

    Select * from DBO.sesam
    Where report_date='2012-02-29' And ((trn_group= 'CS' and trn_package_id=0)
    or (trn_package_id in(Select trn_package_id)) or (portfolio = 'COV-FUND-HA-MM' and currency<>'SEK'))
    From dbo.sesam
    Group by trn_package_id


    jdweng


    No does not work. I get an error message. Thanks anyway

    Sverreberre

    • Proposed as answer by Bruce Song Monday, March 12, 2012 7:21 AM
    • Marked as answer by Bruce Song Wednesday, March 14, 2012 6:00 AM
    Tuesday, March 6, 2012 3:37 PM
  • Hi Sverreberre,

    From your post, I am glad to hear that you have resolved your problem. It will be nice if you can share the workaround with us, this may also help the others who have this similar problems. Wish you a nice day.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    Monday, March 12, 2012 7:21 AM