SQL get year to date for last year

I am currently using this to get a year to date sales number.  I have this snip working but I am trying to get the same info for last year.

  SUM(CASE WHEN YEAR(OINV.DocDate) = YEAR(getdate()) THEN INV1.Quantity*INV1.Price END)AS YTD$

I would like to generate a LYTD$ field calculating the same info but year to date for the same time period last year.  In this case it would be 1/1/14-6/2/14.  I want this to be dynamic as is above.
notasgoodasyouAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsackerContract ERP Admin/ConsultantCommented:
I appended an extra field onto yours. Try this:
SUM(CASE WHEN YEAR(OINV.DocDate) = YEAR(getdate()) THEN INV1.Quantity*INV1.Price END) AS YTD$,
SUM(CASE WHEN YEAR(OINV.DocDate) = YEAR(getdate()) - 1 AND OINV.DocDate <= DATEADD(year, -1, GETDATE()) THEN INV1.Quantity*INV1.Price END) AS LYTD$

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dsackerContract ERP Admin/ConsultantCommented:
Also, you may wish to have an ELSE in both CASE statements saying: ELSE 0.

That way you won't be summing NULLs in your aggregate and get that SQL nastygram. :) :)

SUM(CASE
        WHEN YEAR(OINV.DocDate) = YEAR(getdate()) THEN INV1.Quantity*INV1.Price
        ELSE 0
    END) AS YTD$,
SUM(CASE
        WHEN YEAR(OINV.DocDate) = YEAR(getdate()) - 1 AND OINV.DocDate <= DATEADD(year, -1, GETDATE()) THEN INV1.Quantity*INV1.Price
        ELSE 0
    END) AS LYTD$

Open in new window

notasgoodasyouAuthor Commented:
Thanks worked great
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.