Issue with logical operators

  • Hello all,

    I am having issue with this query

    use ERCOT

    SELECT

    sp.station_code

    ,sp.load_zone

    ,ccs.LOAD_TYPE_DESC

    ,tdspx.esiid

    ,ccs.esiid

    ,tdspx.address

    ,ccs.flow_end_date

    ,ccs.flow_START_DATE

    ,ccs.account_number

    FROM esg.dbo.clean_cust_account_lcd_cancel_switch ccs

    left outer join dbo.all_tdsp_extracts tdspx on ccs.esiid = tdspx.esiid

    left outer join (select distinct station_code, load_zone from dbo.Settlement_Points) sp on sp.station_code = tdspx.station_code

    where flow_START_DATE is not null and flow_START_DATE < '2012-01-03'

    and flow_end_date is null or flow_end_date > '2012-01-03'

    I am not getting the correct number of records. Is there any mistake in the where clause?

    Please assist.

    Thanks

  • where flow_START_DATE is not null and flow_START_DATE < '2012-01-03'

    and flow_end_date is null or flow_end_date > '2012-01-03'

    I'd suggest adding some parens to the where - change to :

    where

    (flow_START_DATE is not null and flow_START_DATE < '2012-01-03')

    and (flow_end_date is null or flow_end_date > '2012-01-03')

    [font="Courier New"]Looking for a Deadlock Victim Support Group..[/font]
  • I tried this. But not working

  • Use brackets to group your "and" and "or" conditions correctly. Right now it's going to return everything with the end date greater than the date specified regardless of the start since there is an "or" with no brackets. Also, if a date is less than X, it already cannot be null so you might as well remove that condition.

    └> bt



    Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • If adding the parenthesis as suggested is still not providing the accurate number of records, please post sample data and structures so we can recreate and test.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply