SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Please help a newbe


Please help a newbe

Author
Message
skibitskie
skibitskie
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 4
Hello All,
really new to SQL so if my question is stupid or complicated, sorry, but i need to run 2 parameters. We have a vendors that are on hold or active and contract date expiration. I need to pull the report for all on hold vendors, but only those active that contract expiration date is more than 360 days away from today's date. I can run 2 reports, but would like to put together a report that will not have to be altered or corrected in the future. the code i have below works, but the date parameters apply to both, active and on hold, how do i change it so that the date only applies to active and all on hold show up. Hope i didn't confuse anyone and thanks in advance.

Kind Regards

WHERE
d.VendorRelationStatus in (‘Active’,’On Hold’)
AND d.ContractEndDate > getdate() AND d.ContractEndDate >= dateadd(d, +360, getdate())
Jack Corbett
  Jack Corbett
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23892 Visits: 14905
You need to change your WHERE clause to something like this:

WHERE
d.VendorRelationStatus = ’On Hold’
AND d.ContractEndDate > getdate()
OR (d.VendorRelationStatus = 'Active'
AND d.ContractEndDate >= dateadd(d, +360, getdate())
)



This WHERE clause now says, "Give me all On Hold Vendors whose contract end date is after today OR Active Vendors whose Contract End Date is more than 360 days from today."

Another way to write this that may perform better in is to write 2 selects (1 that returns the On Hold vendors and 1 that returns the active vendors) and then use a UNION ALL to return them in one result set. This can perform better than an OR because the OR will often cause an index scan while the UNION ALL can do 2 index seeks. This isn't always faster, but can be. You need to test.

If you take a look at the top link in my signature and post questions using the suggestions in that article you'll have a better chance of getting solution that will work the first time.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search