need help in sql query with counting values within dates - php

the table:
tab_tasks
id|....|date_completed|completed
1 | | 2016-11-05 | Y
2 | | 2016-11-07 | N
3 | | 2016-11-09 | Y
4 | | 2016-12-11 | Y
5 | | 2017-01-15 | Y
6 | | 2017-01-30 | Y
and so on...
I need help in writing sql query by giving current date and get months with counted completed tasks back to max 12 months something like:
month | num_complited
november | 2
december | 1
january | 2
or if it would be easier the instead of letters of the alphabet, month can be numeric 11, 12, 01 in column month.
How can be this accomplished? Is there a need for any php code or can be this done with just sql query? Or should I add extra column to that table, like month?

you should put some date range otherwise for example 11-2016 has 6 count and 11-2017 has 5 so out put will show 11 count data mix-up
SELECT MONTHNAME(date_completed) as month ,
count(completed) as num_complited
from tab_tasks
where completed='Y' and
date_completed between '2016-11-05' and '2017-11-05'
group by MONTHNAME(date_completed)

Try this:
select MONTHNAME(date_completed) as MosName,count(1) as num_completed
from TableName
where completed = 'Y'
GROUP BY MONTHNAME(date_completed);

Related

MYSQL: Grouping by week intervals, where greater then Monday

Say we have a posts table that has the columns: id, title, expires_at. We want to show how many posts where not "expired" in each week of every year.
Maybe a simpler way of putting it would be: "a count of posts grouped by weeks of the year where the expires_at date is great then the start of each week"
For example:
-------------------------------------------------
| Year | Week | posts_not_expired |
------------|-----------|-----------------------|
| 2017 | 01 | 22 |
| 2017 | 02 | 103 |
| 2017 | 03 | 7 |
| ... | ... | ... |
| 2009 | 52 | 63 |
|-----------|-----------|-----------------------|
What we have so far:
SELECT
COUNT(id) as posts_not_expired,
YEAR(expires_at) * 100 as Year,
YEARWEEK(expires_at) as Week,
FROM posts
GROUP BY Year, Week
You can use DAYOFWEEK to count non-expired posts for a given week. (where 1 = Sunday,2=Monday,..7=Saturday)
SELECT
YEAR(expires_at) as `Year`,
WEEKOFYEAR(expires_at) as `Week`,
SUM(DAYOFWEEK(expires_at) > 2) as `posts_not_expired`
FROM posts
GROUP BY YEAR(expires_at), WEEKOFYEAR(expires_at)

Mysql Sum Value of a Varchar Column that Holds date in the following format into months

I have a Mysql column named weekly periods (varchar )and the dates inside the column are listed in the following format startperiod-Endperiod.
weekly_periods Amount
01/01/16 - 10/01/16 100
11/01/16 - 17/01/16 20
18/01/16 - 24/01/16 10
25/01/16 - 31/01/16 300
01/02/16 - 07/02/16 250
08/02/16 - 14/02/16 100
Any ideas on the best way to Sum the value's and Group by month for the above table ?
Trying to get the output as below .
January :430 February:350
Try this:
SELECT YEAR(STR_TO_DATE(LEFT(weekly_periods, 8), '%d/%m/%Y')) AS PeriodYear
, MONTH(STR_TO_DATE(LEFT(weekly_periods, 8), '%d/%m/%Y')) AS PeriodMonth
, SUM(Amount) AS TotalAmount
FROM my_table
GROUP BY PeriodYear, PeriodMonth;
Output:
| PeriodYear | PeriodMonth | TotalAmount |
|------------|-------------|-------------|
| 2016 | 1 | 430 |
| 2016 | 2 | 350 |
SQL Fiddle

MySql AVG() and WHERE

I'm struggling on how to write this query and cant quite find an answer to help me with my case.
Consider the following table:
-----------------------------------------------
| ID | Value1 | Value2 | Value3 | Date |
-----------------------------------------------
| 1 | 10 | 23 | 30 | 2015-01-01 |
-----------------------------------------------
| 1 | 11 | 33 | 40 | 2015-02-01 |
-----------------------------------------------
| 2 | 26 | 93 | 20 | 2015-01-01 |
-----------------------------------------------
| 2 | 11 | 33 | 50 | 2015-02-01 |
-----------------------------------------------
I want to retrieve the average value of Value1 where the Date is 2015-01-01
I thought that
SELECT AVG(PAM_1) FROM MyTable WHERE DATE = 2015-01-01
would work but of course it does not. I'm aware that I probably need to use HAVING but I'm being confused if I must also use GROUP BY and if do I need the AS (something) part.
EDIT
The problem was not related to the query. I was supplying the date trough a variable as such:
$sth = $db->prepare("SELECT AVG(Value1) FROM MyTable WHERE DATE = $date");
Which is not possible to do with prepared statements.
Your query is basically fine. Your date constant is not. Dates constants should be enclosed in single quotes:
SELECT AVG(PAM_1)
FROM MyTable
WHERE DATE = '2015-01-01';
If the date could have a time component, then the following is the best way to handle this:
SELECT AVG(PAM_1)
FROM MyTable
WHERE DATE >= '2015-01-01' AND DATE < '2015-01-02';

How to calculate date time in one field

I have a table below named transaction_detail
id_transd | id_trans | id_cust | inputtime |
1 | 1 | 1 | 2013-04-15 16:55:58 |
2 | 1 | 1 | 2013-05-15 16:55:58 |
3 | 1 | 1 | 2013-06-15 16:55:58 |
4 | 2 | 2 | 2013-06-15 16:55:58 |
I want to get the amount of the inputtime (type : datetime) which have the same id_cust. I've done with the date, but I don't know how to calculate the time. This is my sql syntax to calculate the date :
select (date(max(inputtime)) - date (min(inputtime))) as total from transaction_detail where id_cust = '$idp'
Any help will be very appreciated. Thanks before.
Have a look in this website this might help what you need.
http://www.sqlusa.com/bestpractices/datetimeconversion/
Use TIMESTAMPDIFF function to get difference between two datetime values in units you want.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timestampdiff
Units could be: MICROSECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.

sum hits for every week in mysql

I have a table with number of page views per day. Something like this:
+------+------------+------+----------+
| id | date | hits | mangaID |
+------+------------+------+----------+
| 4876 | 1331843400 | 132 | 13 |
+------+------------+------+----------+
| 4876 | 1331929800 | 24 | 236 |
+------+------------+------+----------+
| 7653 | 1331929800 | 324 | 13 |
+------+------------+------+----------+
I'm trying to get sum hits from last week with the below code:
SELECT sum(hits) as hits FROM om_manga_views WHERE DATE_SUB(CURDATE(),INTERVAL 1 week) <= date and mangaID = '13'
My problem is that I'm storing date as time using strtotime in date's field as int type.
So how can i get what i want!?
Try this:
select sum(hits) hitCount from t
where from_unixtime(date) >= current_date() - interval 1 week and mangaId = 11
Here is the fiddle to play with.
I slightly changed your data because the records you provided are older than 7 days, so the sum would return 0.

Categories