?
Solved

AVERAGE Count in 4.1.16

Posted on 2006-04-17
6
Medium Priority
?
825 Views
Last Modified: 2012-05-05
This query works in Mysql but I need it to work in 4.1.16
when I run it I get
1064 check right syntax to use near '(`count`) FROM (SELECT DATE_FORMAT(`date_placed`,'%W %d %M %Y'), COUNT(*)' at line1

SELECT AVG(`count`) from (
SELECT DATE_FORMAT( `date_placed` , '%W %d %M %Y' ) , COUNT( * )  `count`
FROM (
SELECT `date_placed`
FROM `orders`
WHERE  `date_placed` >= Date_Sub(CURDATE(), INTERVAL 1 WEEK)
) AS tmp
GROUP BY DATE_FORMAT( `date_placed` , '%W %d %M %Y' )
) as l

I would like a query to return the average of the order count for the entire week. Also I would like a query that returns the average order count for the week from last year. So  Date_Sub(CURDATE(), INTERVAL 1 WEEK) would be  Date_Sub(CURDATE() **MINUS 365 days**, INTERVAL 1 WEEK)

test data
order_id             date_placed
0076619             2006-03-09 14:46:46
0075994             2006-02-15 21:52:59
0075997             2006-02-16 09:35:14
0076008             2006-02-16 12:21:37

Thanks for any help.
0
Comment
Question by:Scott_Edge
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Expert Comment

by:star_trek
ID: 16471752
try this you missed a , after Count(*)
SELECT AVG(`count`) from (
SELECT DATE_FORMAT( `date_placed` , '%W %d %M %Y' ) , COUNT( * ),  `count`
FROM (
SELECT `date_placed`
FROM `orders`
WHERE  `date_placed` >= Date_Sub(CURDATE(), INTERVAL 1 WEEK)
) AS tmp
GROUP BY DATE_FORMAT( `date_placed` , '%W %d %M %Y' )
) as l
0
 
LVL 17

Expert Comment

by:akshah123
ID: 16471760
I have version 4.1.12 and the query seems to work just fine on my machine.  (Except for the 1 week part, which i changed to 7 day.)
0
 

Author Comment

by:Scott_Edge
ID: 16471793
I still get a syntax error near

1 WEEK)
) AS tmp
GROUP BY DATE_FORMAT( `date_placed` , '%W %d %M %Y' )
) as l
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:Scott_Edge
ID: 16471870
akshah123 It seems the week interval was what was the problem.

star_trek unfortunetly the query doesn't work with the comma after COUNT(*) thank you for your help though.


Do you know how I can select for the previous years week?
0
 
LVL 17

Accepted Solution

by:
akshah123 earned 2000 total points
ID: 16471939
>>Do you know how I can select for the previous years week?

Simple try ..


SELECT AVG(`count`) from (
SELECT DATE_FORMAT( `date_placed` , '%W %d %M %Y' ) , COUNT( * )  `count`
FROM (
SELECT `date_placed`
FROM `orders`
WHERE  `date_placed` >= (CURDATE() - INTERVAL 1 YEAR - INTERVAL 7 DAY)
) AS tmp
GROUP BY DATE_FORMAT( `date_placed` , '%W %d %M %Y' )
) as l
0
 
LVL 11

Expert Comment

by:star_trek
ID: 16471978
SELECT AVG(`count`) from (
SELECT DATE_FORMAT( `date_placed` , '%W %d %M %Y' ) , COUNT( * ),  `count`
FROM (
SELECT `date_placed`
FROM `orders`
WHERE  `date_placed` >= Date_Sub(CURDATE(), INTERVAL 7 DAY)
) AS tmp
GROUP BY DATE_FORMAT( `date_placed` , '%W %d %M %Y' )
) as l
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question