Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

first group by date, then count... all in one query?

Status
Not open for further replies.

cgcg

Programmer
May 19, 2006
2
US
Hello, I have a mySQL database that has individual records for each time a page on my site is hit. What I'm trying to do is set up a tracking system which will display the hit counts for each page, by month, for the past three months, and ordered by total hits over that timespan.

The table is simple:
visitedPage | visitedTime

My current query is this:
SELECT visitedPage, count(*) AS Hits, month(visitedTime) AS Month, year(visitedTime) AS Year
FROM visited_pages
WHERE month(visitedTime) <6
AND month(visitedTime) >2
GROUP BY visitedPage, month(visitedTime), year(visitedTime)
ORDER BY visitedPage, Year ASC, Month ASC

which returns a resultset like this:
visitedPage | Hits | Month | Year
Background | 117 | 3 | 2006
Background | 121 | 4 | 2006
Background | 155 | 5 | 2006
Clients | 211 | 3 | 2006
Clients | 234 | 4 | 2006
Clients | 288 | 5 | 2006
etc.

the resultset I'd like to get is this:
visitedPage | totalHits | Hits | Month | Year
Background | 393 | 117 | 3 | 2006
Background | 393 | 121 | 4 | 2006
Background | 393 | 155 | 5 | 2006
Clients | 733 | 211 | 3 | 2006
Clients | 733 | 234 | 4 | 2006
Clients | 733 | 288 | 5 | 2006
etc.

I can't seem to get that all to happen with one query. Anyone have any suggestions?

Thanks.
 
Nevermind, y'all. I figured it out on my own. Just joined two separate queries into one.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top