MYSQL: Grouping by week intervals, where greater then Monday - php

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)

Related

How to get data in past 30 days in MYSQL and Print Past 30 days inside a loop using a variable in php

I need to get past 30 days sales records from MYSQL database to display in a bar chart how many items sold each day. Also I need to display past 30 days in my chart x axis in my php document. I tried run this code inside a for loop. because I am running some other codes to display some data using this loop. Take a look at the following code and help me how to achieve this.
This is the table I try to get data from
Date format (YYYY-MM-DD)
Products Table
product_id | sold_by | qty | added_date | sold_date
-----------+---------+-----+-------------+-----------
3 | 12 | 7 | 2022-05-05 | 2022-07-28
3 | 12 | 7 | 2022-05-05 | 2022-07-29
3 | 12 | 7 | 2022-05-05 | 2022-07-30
3 | 12 | 1 | 2022-05-05 | 2022-07-30
3 | 12 | 2 | 2022-05-05 | 2022-07-30
6 | 22 | 4 | 2022-06-06 | 2022-07-31
8 | 11 | 6 | 2022-08-05 | 2022-07-31
Deleted Table
product_id | added_date | delete_date
-----------+-------------+-----------
3 | 2022-05-05 | 2022-07-28
3 | 2022-05-05 | 2022-07-29
3 | 2022-05-05 | 2022-07-30
3 | 2022-05-05 | 2022-07-30
3 | 2022-05-05 | 2022-07-30
6 | 2022-06-06 | 2022-07-31
8 | 2022-08-05 | 2022-07-31
As you can see for some days I have multiple records.
<?php
//for loop to run 30 times to get 30 days results
for ($i = 30; $i >= 0; $i--) {
$sold_products_count = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM products WHERE sold_date = CURRENT_DATE - $i"));
$deleted_count = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM deleted WHERE delete_date = CURRENT_DATE - $i"));
?>
{
<?php
$current_date = date("Y-m-d");
$new_date = date_create($current_date);
date_sub($new_date, date_interval_create_from_date_string("$i day"));
?>
x: '<?php echo date_format($new_date, "Y-m-d"); ?>',
a: <?php echo $deleted_count; ?>,
s: <?php echo $sold_products_count; ?>,
},
<?php
}
?>
x- X axis data in the chart
a - number of products deleted in specific date
s - number of products sold in specific date
With the above code I was able to print the x axis data. I am not sure how to get past 30 days data from the SQL database. I think the problem is in the $i part in the SQL query.
In your query, if you use INTERVAL, you can use some days for adding/reducing from the current or specific date.
For example in your case, if you always want to get the last 30 days data, you can use your WHERE like this:
sold_date >= CURDATE() - INTERVAL 30 DAY

How to use Group By, but keep all other values in the column (in mysqli)

Is it possible to use Group By, yet keep all of the other associated values from the columns ?
month | date | location
---------------------------
april | 4/11/18 | US
april | 4/16/18 | US
may | 5/14/18 | Canada
june | 6/05/18 | Canada
june | 6/30/18 | US
Basically, I want to show distinct values for the month, but match the date underneath the respective month, and keep the other values.
april: 4/11/18 (US), 4/16/18 (US)
may: 5/14/18 (Canada)
june: 6/05/18 (Canada), 6/30/18 (US)
Here is the code I used:
$query = ("SELECT * FROM events GROUP BY month");
This is just showing the distinct months.
Any help would be greatly appreciated.
Use GROUP_CONCAT
SQL DEMO
SELECT month,
GROUP_CONCAT( date, '(', location, ')' ) as data
FROM YourTable
GROUP BY month
OUTPUT
| month | data |
|-------|-----------------------------------------------------|
| april | 2018-04-11 00:00:00(US),2018-04-16 00:00:00(US) |
| june | 2018-06-05 00:00:00(Canada),2018-06-30 00:00:00(US) |
| may | 2018-05-14 00:00:00(Canada) |

need help in sql query with counting values within dates

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);

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.

Grab all items from MYSQL Table expired 1 day ago

MYSQL Table trial_list structure as follows...
id | product_id | expiry_date(date) | by_user | curr_datentime(timestamp)
we are able to extend any trial, and if we do that it simply another row with new expiry_date.
Now we would like to get rows got expired yesterday, we are currently using following sql query.....
Sample MYSQL DATASET
+----+-------------+-------------+-------------+----------+---------------------+
| id | product_id | comment | expiry_date | by_user | dnt |
+----+-------------+-------------+-------------+----------+---------------------+
| 2 | 50 | testing | 2011-02-18 | tester | 2011-02-17 23:36:12 |
+----+-------------+-------------+-------------+----------+---------------------+
| 3 | 50 | again | 2011-02-20 | tester | 2011-02-19 20:36:12 |
+----+-------------+-------------+-------------+----------+---------------------+
| 4 | 50 | extend | 2011-02-23 | tester | 2011-02-21 22:36:12 |
+----+-------------+-------------+-------------+----------+---------------------+
$sql = 'SELECT id, product_id, expiry_date, by_user, curr_datentime FROM trial_list WHERE expiry_date < CURDATE() ORDER BY expiry_date DESC';
We believe this is not correct as its getting all rows which date is older than yesterday not updated expiry_date, suppose we have given some user expiry date 1st feb 2011 and then we change again with 12th feb 2011, so it selects 1st feb 2011 entry. I think it makes sense.
What you have to do first is get the latest item per product_id. After that you can further filter it down to those which are expired. Something like:
SELECT a.* FROM
trial_list AS a
LEFT JOIN trial_list AS b ON a.product_id = b.product_id AND a.id < b.id
WHERE b.product_id IS NULL
AND a.expiry_date < curdate()
See http://dev.mysql.com/doc/refman/5.5/en/example-maximum-row.html
Try using NOW() instead of CURDATE(), you are comparing a Date to a Timestamp, NOW() will compare timestamps.

Categories