August 12, 2015 at 8:36 am
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 */
August 12, 2015 at 8:44 am
WHERE StateCodeName = 'Open'
AND SalesStageCode IN (
1,/* DISCOVERY */
121370000, /* PROPOSAL */
121370001, /* SIGN AGREEMENT */
121370002/* INSTALLATION */
)
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
August 12, 2015 at 8:45 am
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
August 12, 2015 at 2:28 pm
in this case the in and the or will produce the same results...... right?
August 12, 2015 at 3:31 pm
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
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply