Get records from current year

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2009
Posts: 9
Reputation: 21KristianN is an unknown quantity at this point 
Solved Threads: 0
21KristianN 21KristianN is offline Offline
Newbie Poster

Get records from current year

 
0
  #1
Nov 4th, 2009
Dear SQL sharks....

Please somebody help me with this struggle. I have been searching the web for hours now.

I have a table with sales data. In this table i have a columne called SalesAmount, CustomerName and SalesDate(the format i smalldatetime)

Now I would like to retreive all the records where SalesDate is within current year...How do I do this??
I want to make a sum of this years sales numbers and when i pass newsyear it should start from zero again.

I really hope somebody can help me!!

Kristian
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 123
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster
 
1
  #2
Nov 4th, 2009
There are alot of ways to do this here are a couple examples.

Using Year() function
  1. SELECT sum(Salesamount)
  2. FROM Table
  3. WHERE YEAR(salesdate) = YEAR(@userInput)

using Date functions

  1. SELECT sum(Salesamount)
  2. FROM Table
  3. WHERE salesdate BETWEEN DATEADD(yy,DATEDIFF(yy,0,@userInput),0) --'First Day of Year'
  4. AND DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,@userInput)+1,0))) --'Last Day of Year'
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 9
Reputation: 21KristianN is an unknown quantity at this point 
Solved Threads: 0
21KristianN 21KristianN is offline Offline
Newbie Poster

very cool solutions but can it be done without input

 
0
  #3
Nov 4th, 2009
Thanks for super fast reply - but is it possible to do this without userinput? So the query is based on the current year of the servertime?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 123
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster
 
0
  #4
Nov 4th, 2009
Just replace where I had @userInput with getdate() function

  1. SELECT sum(Salesamount)FROM TableWHERE YEAR(salesdate) = YEAR(getdate())

You can do the same with the date functions example.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,328
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 600
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #5
Nov 4th, 2009
The only downside to doing it with Year() on the search column is indexes wont be used since you're running a function on the column you want to search on. Ideally you should use date ranges wherever possible.

But cgyrob was awesome and gave you the solution using between() so you're all set
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 42
Reputation: kplcjl is an unknown quantity at this point 
Solved Threads: 5
kplcjl kplcjl is offline Offline
Light Poster
 
0
  #6
Nov 4th, 2009
What sknake said, however he didn't give an example. You need to provide constants in the where clause, so calculate constants. If you are only recording current datetimes in the table you only need to find "WHERE SalesDate >= @dt1". If you are recording future dates as well or if you want to go to some year in the past use "WHERE SalesDate BETWEEN @dt1 AND @dt2".
  1. DECLARE @dt1 SMALLDATETIME, @dt2 SMALLDATETIME, @dtyr CHAR(4)
  2. SELECT @dtyr=YEAR(GETDATE()),@dt1=@dtyr + '0101',@dt2=DATEADD(YEAR,1,@dt1),@dt2=DATEADD(MINUTE,-1,@dt2)
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 42
Reputation: kplcjl is an unknown quantity at this point 
Solved Threads: 5
kplcjl kplcjl is offline Offline
Light Poster
 
0
  #7
Nov 4th, 2009
Sorry, I didn't explain what I did.
"@dtyr=YEAR(GETDATE())" automatically casts the INT result of the YEAR() function into a 4 character string. The string format "YYYYMMDD" is globally recognized as a valid date format, no matter what format your local date setting uses. (IE "DD/MM/YYYY", or "MM/DD/YYYY", etc.) so "@dt1=@dtyr + '0101'" is the first day of the first month of this year at midnight. "DATEADD(YEAR,1,@dt1)" should explain itself, however if you use that in the BETWEEN statement it would include next year's data that started at January 1 midnight so "@dt2=DATEADD(MINUTE,-1,@dt2)" excludes next year's data. If you use DATETIME instead you would need to subtract 3 milliseconds. (If you did that and your comparison field was smalldatetime it would still round up to next year in the comparison.)

PS You should subscribe to SQLServerCentral, you get interesting articles e-mailed to you periodically. They just had an extensive article about this very issue.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 123
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster
 
1
  #8
Nov 5th, 2009
Why are you creating constants when you can just use the date functions in the where clause?


  1. SELECT sum(Salesamount)
  2. FROM Table
  3. WHERE salesdate BETWEEN DATEADD(yy,DATEDIFF(yy,0,getdate()),0) --'First Day of Year'
  4. AND DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0))) --'Last Day of Year'
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 9
Reputation: 21KristianN is an unknown quantity at this point 
Solved Threads: 0
21KristianN 21KristianN is offline Offline
Newbie Poster
 
0
  #9
Nov 5th, 2009
Thanks.... that was exactly what i needed... Perfect!!! Thank you for helping me.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 42
Reputation: kplcjl is an unknown quantity at this point 
Solved Threads: 5
kplcjl kplcjl is offline Offline
Light Poster
 
0
  #10
Nov 5th, 2009
Originally Posted by cgyrob View Post
Why are you creating constants when you can just use the date functions in the where clause?


  1. SELECT sum(Salesamount)
  2. FROM Table
  3. WHERE salesdate BETWEEN DATEADD(yy,DATEDIFF(yy,0,getdate()),0) --'First Day of Year'
  4. AND DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0))) --'Last Day of Year'
The key phrase is "functions in the where clause" As someone else has stated in this thread, this is a bad idea, expecially when selecting from indexed fields. First you are causing SQL to execute a process for every single row in the table. This increases CPU processing which slows down response. It does this for every row because functions are designed to give dynamic results and SQL doesn't realized this is producing a constant. By my count you are executing 6 functions. In a table with 20 million rows in it, that's 120 million executions and should take minutes if not hours to process.
That's if you are looking for 1 year or 1 hour, indexed or not indexed.
NEVER, EVER, EVER put functions that produce a constant value in SQL where clauses. (Unless you happen to like poor response and poor performance.)
PS I think my query is easier to read than yours with separate statements, 3 functions, sequentially executed.
PPS Whoops, miscounted ... 8 functions in yours, 3 in mine because I also removed the Midnight date error your query still has.
Last edited by kplcjl; Nov 5th, 2009 at 5:06 pm. Reason: Add a comment
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for MS SQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC