Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Please help a newbe Expand / Collapse
Author
Message
Posted Tuesday, March 4, 2014 4:10 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, March 5, 2014 8:31 AM
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())
Post #1547594
Posted Tuesday, March 4, 2014 4:56 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 8:26 AM
Points: 10,381, Visits: 13,436
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

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
Post #1547604
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse