Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello freinds

I am doing some reporting on sales figures and would like to make some generic reports that will update as time goes by. This Means that I would like to get all sales from current year and in another report current year -1 and in the NeXT year -2.. Is this possible?

Right now i'm using the below to get the current year - but how to get -1 and -2 and so on..


= YEAR(GETDATE()))

My full SQL Query
SQL
SELECT CAST(SUM(Cust.[Sales (LCY)]) AS decimal(8 , 2)) AS Totalsales, Cust.[Customer No.], Customer.Name 
FROM [Cust. Ledger Entry] AS Cust INNER JOIN Customer ON Cust.[Customer No.] = Customer.[No.] 
WHERE (Cust.[Document Type] = 2) AND (YEAR(Cust.[Posting Date]) = YEAR(GETDATE())) 
GROUP BY Cust.[Customer No.], Customer.Name ORDER BY Totalsales DESC
Posted
Comments
ZurdoDev 3-Feb-16 14:10pm    
YEAR(GETDATE()) - 1
Kristian_dk 4-Feb-16 12:22pm    
Dosen't work :-( Sql server is returning an error when doing so..

Use the DateAdd function.
DateAdd on MSDN
[^]

SQL
@LastYear = DateAdd(yyyy, -1, GetDate())
@TwoYearsAgo = DateAdd(yyyy, -2, GetDate())
 
Share this answer
 
v3
Comments
Kristian_dk 4-Feb-16 12:24pm    
This one was accepted - nice finally i can continue with my project. Thanks for helping
Just use YEAR(GetDate()) - 2 or -1 of whatever you want.
 
Share this answer
 
Comments
Kristian_dk 4-Feb-16 12:22pm    
Dosen't work :-( Sql server is returning an error when doing so..
ZurdoDev 4-Feb-16 12:48pm    
The error will tell you what is wrong. You likely missed a ) or something.
Kristian_dk 4-Feb-16 12:24pm    
But your code actually works on another SQL server.. hmmm thats weird..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900