Average of mysql-records in a specific time - php

I've got a mysql-database like this:
ID | date | value
1 | 2017-02-20 | 200
2 | 2017-02-17 | 400
3 | 2017-02-15 | 630
4 | 2017-01-28 | 110
5 | 2017-01-29 | 220
I know how to get the sum of january, february, ....
SELECT SUM(value) AS wert
FROM table
WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 2 MONTH)
AND MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 2 MONTH)
But now I need a code to get the average of january and february.
I tried
SELECT SUM(value) AS wert, AVG(wert) as average
FROM table
WHERE YEAR (date) = YEAR(CURRENT_DATE - INTERVAL 2 MONTH)
AND MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 2 MONTH)
But this didn't work.
Thank you for your help!

WHERE date >= '2017-1-1' AND date < '2017-3-1'

Related

Get the count of users before 90 days records from the e_date

Database table i have:
S.no | j_id |age | e_date |
-------------------------------------
1 | 1 |32 | 2018-05-09 |
-------------------------------------
-------------------------------------
1 | 1 |32 | 2018-05-09 |
-------------------------------------
-------------------------------------
1 | 2 |32 | 2018-05-09 |
-------------------------------------
-------------------------------------
1 | 2 |32 | 2018-04-16 |
-------------------------------------
-------------------------------------
1 | 1 |32 | 2018-09-16 |
-------------------------------------
-------------------------------------
1 | 3 |32 | 2018-04-16 |
------------------------------------
In my table I have expiry date I want to get the count of the result whose expiry date (90 days before) is equal to current date.
like I have expiry date 2018-05-09 and current date is 2018-02-2018 (90 days before date ) now i want to get to the count of the 90 days before result by query.
select * from yourtable
where datediff(CURDATE(), e_date) > 90
try to use datediff to get the different date count in day. Hope that this is what you want to get.
I like to keep the logic on the PHP side as much as possible, so I would probably calculate my expiry date in PHP and just add a simple where to the query. In Laravel that could look something like this:
$expireTreshold = Carbon::now()->addDays(90);
$expireCount = $myModel->where('e_date', '<=', $expireTreshold)->count();
For getting the current date, you can use CURDATE(), CURRENT_DATE(), and NOW() any one of these functions would get the current date. While the DATEDIFF() will get the difference between two periods (start date to end date).
If you only need to get the expiry dates that fit the 90 days condition use this :
SELECT e_date
FROM tableName
WHERE
e_date >= NOW()
AND datediff(e_date, NOW()) <= 90
In the query, you're scanning for future dates (from the current date and forward) and then get the differences, if the differences is less than or equal to 90 days, then it'll be selected.
if you need to show how many days left to each user:
SELECT sno, datediff(e_date, NOW())
FROM test
WHERE
e_date >= NOW()
AND datediff(e_date, NOW()) <= 90
It will work fine.
SELECT count(*) FROM table WHERE e_date < CURDATE() - INTERVAL 90 DAY;
OR
SELECT count(*) FROM table WHERE e_date < NOW() - INTERVAL 90 DAY;
cheers :)
Try this:
$your_date = "2018-01-01";
query = 'SELECT COUNT(*)
FROM thetable WHERE e_date =
DATE_ADD($your_date, INTERVAL 90 DAY)';

Splitting month into weeks and using GROUP BY

This is a question, which I could not find answer to anywhere. Okay. here it is.
I have two date ranges (This month and the last month)
Last month - 01/01/2015 (January 1 2015) to 31/01/2015
This month - 01/02/2015 (1st Feb 2015) to 28/02/2015
Now, each month has weeks. I have a table with column created_at. I want to fetch all the records week-wise into an array (to plot a graph) with their corresponding sum(value) or count(value) .
So it will be something like this:
Last Month:
Week 1 - 25
Week 2 - 34
etc.
This Month:
Week 1: 55
Week 2: 56
etc.
The date is in this format in created_at: 2015-07-21 01:27:14 (Y-m-d H:i:s)
In MySql You can use WEEK() to get the number of the week (from 1 to 53)
O you can use WEEKDAY() or DAYOFWEEK() the first bigins on Monday the second on Sunday.
You can use them into a GROUP BY with HAVING
Something like:
SELECT count(*)
FROM `YourTable`
WHERE `created_at` >= '2015-10-01' AND `created_at`< '2015-11-01'
GROUP BY WEEK(`created_at`)
To use the workaround you found You need to do something similar:
create a table named "numbers" with a field "id" (autoincrement) and 31 rows (one for each day of a month)
Then use a query like this:
SELECT count(i.created_at)
FROM
(SELECT DATE_FORMAT(DATE_ADD('2015-12-01', INTERVAL -n.id DAY), '%Y-%m-%d') AS AllDays
FROM numbers n) AS DaysOfMonth
Left Join
YourTableName i
ON STR_TO_DATE(i.created_at, '%Y-%m-%d') = DaysOfMonth.AllDays
GROUP BY WEEK(AllDays)
(try to adapt it to your needs)
What you need to do is group by the week and then sum the values. Here's a simple example of how it might work:
SELECT DATE_FORMAT(created_at,'%Y-%V') as interval, SUM(units_sold) as total_sold
FROM sales
GROUP BY DATE_FORMAT(created_at,'%Y-%V')
What you'll be getting is the year ant week number (ex. 2015-50) and the sum from that interval.
A table like this:
+----+------------+---------------------+
| id | units_sold | created_at |
+----+------------+---------------------+
| 1 | 2 | 2015-01-01 09:00:00 |
| 2 | 4 | 2015-01-04 10:00:00 |
| 3 | 1 | 2015-01-12 12:00:00 |
| 4 | 4 | 2015-01-16 13:00:00 |
+----+------------+---------------------+
Would result to:
+----------+------------+
| interval | total_sold |
+----------+------------+
| 2015-01 | 6 |
| 2015-03 | 5 |
+----------+------------+
I think it is useful for you...
SELECT GROUP_CONCAT(id), COUNT(id) AS idcount,SUM(id) AS idsum,
MONTHNAME(order_created_date) AS month_name, WEEK(order_created_date)
AS weeks FROM orders GROUP BY WEEK(order_created_date)

Fetching last 30 days data from mysql table and grouping them by date

Here is how my table looks like
| total_hit |success_hit | date_time |
| 12 | 12 | 01-11-2009 07:32:44 |
| 12 | 11 | 01-11-2009 08:33:49 |
| 12 | 10 | 01-11-2009 09:08:24 |
| 12 | 11 | 01-12-2009 10:33:57 |
| 12 | 12 | 01-12-2009 11:37:34 |
| 12 | 11 | 01-12-2009 12:23:49 |
I am fetching the data from the table for the past 30 days using the following query:
SELECT *
FROM my_table
WHERE DATE(date_time) >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
what I want to do is group the returned data in terms of date and calculate the percentage success (success_hit/total_hit)*100
Something like
Date: 01-11-2009 Percentage: x
Date: 01-12-2009 Percentage: y
any ideas?
select (success/total)*100 as perc_success, Date(date_time) from
(select sum(success_hit) as success, sum(total_hit) as total,
date_time from my_table
where DATE(date_time) >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
group by Date(date_time) )
tbl
This should work!!!
Try this
SELECT (sum(success_hit)/sum(total_hit)*100) AS `Percentage`,
DATE(date_time) AS `Date`
FROM my_table
WHERE DATE(date_time) >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
GROUP BY DATE(date_time);

php mysql Get monthly sales group by timestamp

So I have this order table
| order_id | sales | order_date(timestamp) |
2 100 2013-10-30 04:07:01
3 300 2013-10-31 04:30:02
4 400 2014-01-10 08:07:01
and here is my query, $m1 and $m2 are from order_date
SELECT `order_id`, MONTH(`order_date`) AS month,
COUNT( `order_id`) AS orders_made,
SUM(`total`) AS sales, YEAR(`order_date`) AS YEAR
FROM `orders`
WHERE `order_date` BETWEEN ('$m1') AND ('$m2')
GROUP BY MONTH(`order_date`)
And this is what I got
| month | year | sales | orders_made |
10 2013 100 1
11 2014 400 1
My query does not return all the data that I wanted, what should be the query to display something like this
| month | year | sales | orders_made |
10 2013 400 2
11 2014 400 1
Need your help guys
SELECT YEAR(order_date),
MONTH(order_date),
COUNT(order_id) AS orders_made,
SUM(sales) AS sales
FROM orders
WHERE order_date BETWEEN ('$m1') AND ('$m2')
GROUP BY YEAR(order_date), MONTH(order_date)

mysql month wise report

I need an output like this:
Month -->| Pre_Mnth2 | Pre_Mnth1 | current_Month_ Name
Product A | 3387 | 87985 | 2338
Product B | 6386 | 67983 | 6374
Product C | 3880 | 76988 | 9378
...
All the required data is in single table only.
Column looks like this:
id | companycode | merchantcode| pdtname | qty | value | invdate |
Can anybody help me with php mysql query code?
My current code returns the data for current month only
SELECT `pdtname`, `qty`, `value`, sum(`qty`), sum(`value`)
FROM `ist`
WHERE merchantcode = $q AND companycode = $companycode
AND MONTH(invdate) = $currentdate[mon]
GROUP BY `pdtname`
Here variable $q and $companycode is taken from user session.
Are you looking for something like this?
SELECT pdtname,
SUM(CASE WHEN invdate
BETWEEN LAST_DAY(CURDATE()) - INTERVAL 3 MONTH + INTERVAL 1 DAY
AND LAST_DAY(CURDATE()) - INTERVAL 2 MONTH THEN qty END) qty_2m,
SUM(CASE WHEN invdate
BETWEEN LAST_DAY(CURDATE()) - INTERVAL 3 MONTH + INTERVAL 1 DAY
AND LAST_DAY(CURDATE()) - INTERVAL 2 MONTH THEN value END) value_2m,
SUM(CASE WHEN invdate
BETWEEN LAST_DAY(CURDATE()) - INTERVAL 2 MONTH + INTERVAL 1 DAY
AND LAST_DAY(CURDATE()) - INTERVAL 1 MONTH THEN qty END) qty_1m,
SUM(CASE WHEN invdate
BETWEEN LAST_DAY(CURDATE()) - INTERVAL 2 MONTH + INTERVAL 1 DAY
AND LAST_DAY(CURDATE()) - INTERVAL 1 MONTH THEN value END) value_1m,
SUM(CASE WHEN invdate
BETWEEN LAST_DAY(CURDATE()) - INTERVAL 1 MONTH + INTERVAL 1 DAY
AND LAST_DAY(CURDATE()) THEN qty END) qty_m,
SUM(CASE WHEN invdate
BETWEEN LAST_DAY(CURDATE()) - INTERVAL 1 MONTH + INTERVAL 1 DAY
AND LAST_DAY(CURDATE()) THEN value END) value_m
FROM ist
WHERE invdate BETWEEN LAST_DAY(CURDATE()) - INTERVAL 3 MONTH + INTERVAL 1 DAY
AND LAST_DAY(CURDATE())
AND companycode = 1
AND merchantcode = 1
GROUP BY pdtname
Note: Make sure that you have an index on invdate.
Output:
| PDTNAME | QTY_2M | VALUE_2M | QTY_1M | VALUE_1M | QTY_M | VALUE_M |
|----------|--------|----------|--------|----------|-------|---------|
| ProductA | 30 | 3000 | 30 | 3000 | 30 | 3000 |
| ProductB | 100 | 1000 | 100 | 1000 | 100 | 1000 |
| ProductC | 320 | 3200 | 320 | 3200 | 320 | 3200 |
Here is SQLFiddle demo

Categories