Please help a newbe

  • 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())

  • 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.

Viewing 2 posts - 1 through 1 (of 1 total)

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