SQL: Counting the number of rows whose sum reaches a specific value - mysql

I have a data that looks like more or less like this table. Let's call this "expenses" table.
date
transportation
fruits
vegetables
2022-05-25
10
10
30
2022-05-26
10
0
40
2022-05-27
10
20
40
2022-05-28
10
20
30
2022-05-29
10
0
60
2022-05-30
10
10
30
2022-05-31
10
10
40
2022-06-01
10
10
30
2022-06-02
10
20
30
2022-06-03
10
30
40
2022-06-04
10
0
20
2022-06-05
10
30
30
2022-06-06
10
20
30
2022-06-07
10
0
30
2022-06-08
10
0
30
2022-06-09
10
10
20
2022-06-10
10
30
30
I want to know how many days, for the months of May and June, the sum of fruits and vegetables was equal to or greater than 50.
The answer that I'm expecting to get is 4 days for May (05-27,05-28, 05-29, 05-31) and 5 days for June (06-02, 06-03, 06-05, 06-06, 06-10)
I tried to use this script, however...
SELECT date_format(date, '%M' '%Y'), COUNT(date)
FROM expenses
GROUP BY date_format(date, '%M' '%Y')
HAVING SUM(fruits + vegetables)>=50
...instead of counting only the number days per month whose sum of fruits and vegetables was equal to or greater than 50, it counts all days in the table for the two months, that is to say, it yields an answer of 7 days for May and 10 days for June.
I am using the latest version of MySQL.

Instead of having clause, filter the rows with Where.
Query
select date_format(date, '%M' '%Y') as month, count(*) as no_of_days
from expenses
where fruits + vegetables >= 50
group by date_format(date, '%M' '%Y');
SQL Fiddle

Related

Calculate avg time in mysql from last 7 days

Tablename=run_detail
I have to calculate avg time of jobs for last 7 days, but in somecases
number of runs could be less than 7 days. eg abc has only 2 run_date.
(4.5+6+.....+7)/7=5.83 and (23.9+45.7)/2=34.8 and also need to
calculate based on latest 7 runs. for eg. 2020-07-04 to 2020-07-10,
not from 2020-07-01
Job_name run_date rownum count elapsed_time(sec) avg_time
xyz 2020-07-01 1 10 4.5 ?
xyz 2020-07-02 2 10 6 ?
.......
xyz 2020-07-10 10 10 7.0 ?
abc 2020-07-01 1 2 23.9 ?
abc 2020-07-02 2 2 45.7 ?
Desired Output
Job_name run_date rownum count elapsed_time(sec) avg_time
xyz 2020-07-01 1 10 4.5 5.83
xyz 2020-07-02 2 10 6 5.83
.......
xyz 2020-07-10 10 10 7.0 5.83
abc 2020-07-01 1 2 23.9 34.8
abc 2020-07-02 2 2 45.7 34.8
Could you please help how to achieve the avg time in mysql
If you want the overage over the preceding 7 days, you can use a window functions:
select t.*,
avg(elapsed_time) over (partition by job_name
order by run_date
range between interval -6 day preceding and current row
) as avg_time
from t;
Note: This assumes that you really want six preceding days plus the current date. If you really want 7 days before to 1 day before (the preceding week), then use:
range between interval -7 day preceding and interval -1 day preceding
EDIT:
In older versions of MySQL, you can use a correlated subquery:
select t.*,
(select avg(t2.elapsed_time)
from t t2
where t2.job_name = t.job_name and
t2.run_date <= t.run_date and
t2.run_date > t.run_date - interval 7 day
) as avg_time
from t;
Adjust the date comparison to get exactly the period you want.

Is there a way to have a conditional statement for comparing of tables using MySQL only?

I am having a dilemma from comparing my tables.
My problem is, I want to get each sum, which depends on the pricing. Here is the table.
Main table
main_id main_price main_date_created
25 8.5 2019-08-16
26 11.5 2019-08-01
Total table
id main_id total_price date_generated
1 25 10 2019-08-16
2 25 10 2019-08-17
3 25 10 2019-08-18
4 25 10 2019-08-19
5 25 10 2019-08-20
6 25 10 2019-08-21
7 26 20 2019-08-01
8 26 5 2019-08-02
9 26 5 2019-08-03
10 26 10 2019-08-04
Price History table
id main_id changed_main_price price_date_changed
1 25 15 2019-08-18
2 26 20 2019-08-03
I don't know if there is a way to do this just by using MySQL, what I am trying to achieve is, the Total table will be sum by MONTH and YEAR and will be multiplied by their designated price that depends on the date whether if the price has changed or not . the SUM will from each month will be generated by multiplying with the main price in the Main table but if the price had changed from its original price which it is on the Price history table
The output should be like this if the conditional is possible:
id main_id total_price price_generated (which is the prices) date
1 25 170 (10+10*8.5) 8.5
2 25 610 (10+10+10+10*15) 15
3 26 287.5 (20+5*11.5) 11.5
4 26 300 (5+10*20) 20
Here is my existing query,
SELECT m.main_id
, m.main_price
, SUM(t.total_price) total_generated
, t.date_generated
FROM main m
INNER JOIN total t
ON m.main_id = t.main_id
GROUP
BY MONTH(t.date_generated);
I know that my query is not enough, and I still don't know if my idea is really possible :(.
I racked my brain. hahaha Is this you're after?
SELECT pricelist.id, pricelist.price, SUM(t.total_price), SUM(t.total_price) * pricelist.price,
year( pricelist.latestdate), month(pricelist.latestdate),
year( t.total_date_generated), month(t.total_date_generated)
from totaltable as t
LEFT JOIN (
(
select main_id as id, main_price as price, main_date_created as latestdate
from maintable
)
UNION ALL
(
select total_main_id, changed_main_price , price_date_changed
from pricehistorytable
)
) as pricelist on pricelist.id = t.total_main_id
GROUP BY pricelist.id , t.total_price, pricelist.price,
year( pricelist.latestdate), month(pricelist.latestdate),
year( t.total_date_generated), month(t.total_date_generated) ;

Last hour wise count from 00 AM to last hour - mysql

I trying to fetch count of records from 00 AM i.e. 12 to last hour group by Hours.
select count(RESPONSE) AS TOTAL_521_ERROR from Sale_GT
where ERRORCODE='521'
AND SALEDATE > DATE_SUB(NOW(),INTERVAL 1 HOUR);
This query fetching last one hour record. similary I want if i run query at 5 AM then result should be count(RESPONSE) from 00 AM to 4 AM group by HOUR(SALEDATE).
Hour Count
0 345
1 432
2 36
3 87
4 90
so result will be from 12 night to last hour of query execution time.
Please help.
You can use SELECT with GROUP BY, e.g.:
SELECT HOUR(SALEDATE) AS `hour`, COUNT(RESPONSE) AS `errors`
FROM Sale_GT
WHERE ERRORCODE='521'
AND SALEDATE >= DATE(NOW())
GROUP BY `hour`;

How to sum the value for each

I have table following :
ClientNUM PIECES DID
NEWAGENC 10 5
NEWAGENC 25 5
WAY 30 4
CHCAH 20 2
AVC 21 2
i want the Result that sum the value for each client as below
CleintNUM Pieces DID
NEWAGENC 35 5
WAY 30 4
CHCAH 20 2
AVC 21 2
My query
SELECT
CLIENTNUM,
DID,
PIECES,
GETDATE() AS CURRENTDATE,
SUM(PIECES)
FROM Mytable
GROUP BY CLIENTNUM, DISPID, PIECES
So how can i do the sum for each CLIENTNUM in my query Means DISTINCT For each client Pieces like NEWAGENC has value 10 and in second row 25 so the pieces will be 10+ 25 = 35
Don't group by PIECES if you want to aggregate it
SELECT CLIENTNUM,
DID,
PIECES,
GETDATE() AS CURRENTDATE,
SUM(PIECES)
FROM Mytable
GROUP BY CLIENTNUM, DISPID

How to select database rows by days ,month,years

How to select database rows by days ,month,years
I have table like this
id count generatedAt
1 130 2013-01-13 02:21:02
2 120 2013-01-08 04:15:06
3 89 2013-01-08 01:42:57
4 24 2012-11-25 05:31:43
5 3 2012-02-31 09:25:24
I would like to select the rows by day or month or year.
For example by day.
2-3 is same day so I need only
1,2,4,5
for example by month,1,2,3 is same month so I need only
1,4,5
for year I need only 1,4
How can I make it?
I am using doctorine2
You can do something like this . .
you can choose a date , month or specific year to select rows.
select * from TabeName
//for days
where DAY(myDate) = 20
//for month
MONTH(myDate) = 12
// for year
YEAR(myDate) = 2008