If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > Studying Access Databases

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-26-12, 15:57
jjs8990 jjs8990 is offline
Registered User
 
Join Date: Jan 2012
Location: Andover
Posts: 2
Studying Access Databases

In my class were are currently learning the basics of an Access database, and I am having a problem getting it to cooperate. The question is "How many orders per year?"

This is what I've gotten so far:

SELECT COUNT (Orderdate)
FROM Orders

Now obviously that will count all of the order dates, but what I need it to do is group the orders by year and count each group. I would assume that would be:

GROUP BY DATEPART ('YYYY')

However, that does not work. Any suggestions?
Reply With Quote
  #2 (permalink)  
Old 01-26-12, 16:39
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,446
The DatePart() function expects at least 2 parameters: interval and date (both are mandatory):
Code:
DatePart(interval, date[,firstdayofweek, firstweekofyear]])
Try:
Code:
SELECT Count(Orders.Orderdate) AS CountByYear
FROM Orders
GROUP BY DatePart('yyyy', Orders.Orderdate);
__________________
Have a nice day!
Reply With Quote
  #3 (permalink)  
Old 01-26-12, 16:55
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
or take advantage of some of the inbuiolt date tome functions such as year()
group by year (orders.orderdate)
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #4 (permalink)  
Old 01-26-12, 17:27
jjs8990 jjs8990 is offline
Registered User
 
Join Date: Jan 2012
Location: Andover
Posts: 2
Quote:
Originally Posted by Sinndho View Post
The DatePart() function expects at least 2 parameters: interval and date (both are mandatory):
Code:
DatePart(interval, date[,firstdayofweek, firstweekofyear]])
Try:
Code:
SELECT Count(Orders.Orderdate) AS CountByYear
FROM Orders
GROUP BY DatePart('yyyy', Orders.Orderdate);
Thank you! That worked wonderfully! May I ask where you learned this stuff? I am (and my classmates) are having troubles with our teacher not telling us where to find these things.
Reply With Quote
  #5 (permalink)  
Old 01-26-12, 17:39
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,446
Try to learn how to use the Access help features: select a keyword (e.g. "DatePart") and press the F1 key. As far as the syntax is concerned, (almost) everything you need to know is in there. Experience helps too (in my case, almost 30 years as a professional in I.T.).

You're welcome, by the way!
__________________
Have a nice day!
Reply With Quote
Reply

Tags
access, databases

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On