In my questions table, each post is timestamped (MM-DD-YYYY). Each individual post has a QuestionID, and a response has both a QuestionID and ResponseID. I want to run a php script that will grab all questions (all posts where ResponseID = null) and display how many questions per day.
I'd like it to be avg questions per day since June 1, 2010.
Your help is much appreciated. Thank you!
This gets each day, and the number of questions:
select q.myTimestamp, count(*)
from questionsTable q
where q.myTimeStamp >= '6/1/2010'
and q.ResponseID IS NULL
group by myTimestamp
To get the average you need both the count of days, and sum of number of questions... I would probably do this with 2 queries to MySQL and calculate in PHP.
$query1 = "select count(*) as totalCount from questionsTable where myTimestamp >= '6/1/2010' and ResponseID IS NULL";
$query2 = "select count(*) as totalDays from (select distinct myTimestamp from questionsTable where myTimestamp >= '6/1/2010' and ResponseID IS NULL) a";
$res1 = mysql_query($query1);
$res2 = mysql_query($query2);
$row1 = mysql_fetch_array($res1);
$row2 = mysql_fetch_array($res2);
$avgPostCount = ($row1['totalCount'] / $row2['totalDays']);
not optimal, but should work..
I made an assumption, that you wouldn't want to count days where there were no questions.. otherwise it could be simplified to one query using a mysql function to get the number of days since 6/1/2010.
SELECT
AVG(NumQuestions)
FROM
(
SELECT
COUNT(*) AS NumQuestions
FROM
Questions
WHERE
ResponseID IS NULL AND
Question_Date >= '2010-06-01'
GROUP BY
Question_Date
) AS MySubquery
On second thought: This is silly. If you want to know the average number of questions per day since a given day, you may as well just count the number of questions (simple) and then divide by the number of days that have elapsed since then. Also, the above query gives inaccurate results if there are days on which nobody asked a question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.