issue with or statement

  • Hi

    It's been awhile since I have used sql and I just can't remember how to get my statement correct:

    So I want to display all records where the State Code Name = Open

    then if the records Sales Stage Code has any one of the codes displayed in the or statement.

    SELECT

    OPPS.statecodeAS [State Code],

    OPPS.statecodenameAS [State Code Name],

    OPPS.salesstagecodeAS [Sales Stage Code],

    OPPS.salesstagecodename AS [Sales Stage Name],

    TBU.businessunitidnameAS [BU Name],

    OPPS.accountidnameAS [Account Name],

    OPPS.parentcontactidnameAS [Contact Name],

    OPPS.descriptionAS [Description],

    OPPS.estimatedvalueAS [Est. Weekly Value],

    OPPS.estimatedclosedateAS [Est. Close Date],

    d.[Days]as [Num Days Open],

    OPPS.owneridnameAS [Owner],

    OPPS.actualclosedateAS [Opp Actual Close Date],

    OPPS.statecodeAS [State Code],

    OPPS.statecodenameAS [Status],

    OPPS.statuscodeAS [Status Code],

    OPPS.statecodenameAS [State Code Name],

    OPPS.actualvalueAS [Actual Value]

    FROM dbo.FilteredOpportunity AS OPPS

    INNER JOIN dbo.FilteredUserSettings AS TBU ON TBU.systemuserid = OPPS.ownerid

    CROSS APPLY(VALUES(DATEDIFF(day, @CurrentDate, OPPS.actualclosedate))) AS d([Days])

    WHERE StateCodeName = 'Open'

    OR SalesStageCode = 1/* DISCOVERY */

    OR SalesStageCode = 121370000 /* PROPOSAL */

    OR SalesStageCode = 121370001 /* SIGN AGREEMENT */

    OR SalesStageCode = 121370002/* INSTALLATION */

  • WHERE StateCodeName = 'Open'

    AND SalesStageCode IN (

    1,/* DISCOVERY */

    121370000, /* PROPOSAL */

    121370001, /* SIGN AGREEMENT */

    121370002/* INSTALLATION */

    )

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • WHERE StateCodeName = 'Open' AND

    (SalesStageCode = 1/* DISCOVERY */

    OR SalesStageCode = 121370000 /* PROPOSAL */

    OR SalesStageCode = 121370001 /* SIGN AGREEMENT */

    OR SalesStageCode = 121370002)/* INSTALLATION */

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • in this case the in and the or will produce the same results...... right?

  • Yup. Completely equivalent.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

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

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