I have a table in my database which stores the meters energy value of 1st of every month. In case meter is offline it will store the value of the next day.
Below is my case
I have a record of a meter of past 2 months February and March. The February data is of 2019-02-01 00:00:00 but there are 4 rows for the month March. See the below image
In the above image the 1st,2nd and 3rd of March have a null value of FA but the 4th March contains some value.
What I have done?
I am able to select the rows having values of FA.
What I want to do?
I want to get only the current month data i.e. Current month is March so it should get only march record and then next month it should get only April record and so on.
The query should not exceed the days limit more than 4 i.e. It should only check record for 1st four days of every month.
Here is my DB-Fiddle
Any help would be highly appreciated.
one way to solve this is
FOR
I want to get only the current month data i.e. Current month is March so it should get only march record and then next month it should get only April record and so on.
means month(TV)= month(now())
and
The query should not exceed the days limit more than 4 i.e. It should only check record for 1st four days of every month. means day(TV)<= 4
and finally your query
select * from `biz_pub_data_f_energy_m` a
where a.`DATA_ID` = '1b9716122dd5408691a063227316ac0a'
and a.`FA` is NOT NULL and month(TV)= month(now())
and day(TV)<= 4
You can try below -
DEMO
select * from `biz_pub_data_f_energy_m` a
where a.`DATA_ID` = '1b9716122dd5408691a063227316ac0a'
and a.`FA` is NOT NULL and tv>=date(DATE_SUB(now(),INTERVAL DAYOFMONTH(now())-1 DAY))
and tv<=DATE_ADD(date(DATE_SUB(now(),INTERVAL DAYOFMONTH(now())-1 DAY)), INTERVAL 4 DAY)
Related
I have a monthly offering that gives customers 30 minutes of use every month and I need to tally up their usage and group it by month, but not sure the best way to handle that since the start date could be any day of the month.
Should I prorate? Convert into full months or count the days?
Is there an ideal way to tally this? Let's say each month they get 30 minutes of use time, so I want to tally how many minutes were used that "month."
Say there is a minutes used table and the start date for the subscription is 2019-06-13 00:00:00
minutes_used_table:
Userid MinutesUsed Date
1 5 2019-06-19
1 6 2019-06-23
1 8 2019-06-28
1 15 2019-07-05
1 3 2019-07-12
1 8 2019-07-19
1 5 2019-08-14
1 3 2019-08-22
1 1 2019-08-26
1 2 2019-09-13
Or, should I prorate it and instead of tracking in 30 day increments, if they start on the 13th of June, should I just count the days from the start date to the end of the month, whatever day that is and then the days from the first of the month until the start date?
Wondering what makes the most sense and how to carry it out?
Use
GROUP BY DATEDIFF(`Date`, #starting_date) DIV #period_length
In your particular case it will be
GROUP BY DATEDIFF(`Date`, '2019-06-13 00:00:00') DIV 30
From coding point of view it's not a big deal to find first date for user, subtract it from dates and count number of 30-days periods.
But this will be very hard to support in future and this solution will have a number of corner cases: for example someone first start to use product at 20 January, after month of usage have been idle for a while and came back in April, 5. What will be right start date for that case?
So I suggest to use calendar months. And may be reduce limit on first month accordingly to the number of days left or give full trial period even on first month as user has ability to spend 30 minutes.
So I want to select all my row that is recorded by this week.
For example. today is January 30 2019, which is Wednesday. I would like to select all my row from Monday(January 28,2019) to Wednesday only(current day). And when tomorrow comes, the rows added on Thursday will also be selected.
This will continue til Sunday. And when the Next week Monday comes, it will only select the rows recorded on that Monday. (February 4, 2019) and so on.
I just need to get the (THIS WEEK) data, not the past 7 days.
I have a dateadded column
-----------------------------
Firstname|Lastname|dateadded
Michael |Jordan |2019-02-03 <-(Feb 3, 2019)
Mark |Perez |2019-01-30
Paul |George |2019-01-28
John |Wayne |2019-01-25
-----------------------------
A query that will only select this weeks added data.
Mark Perez and Paul George which is in the same week of the current day.
select * from names WHERE YEARWEEK(`dateadded`, 1) = YEARWEEK(CURDATE(), 1)
will only select the last 7 days of the curdate.
Well you can use WEEKDAY() to get the day number of the current day within the week, and susbstract if from the current date :
SELECT SUBDATE(CURDATE(), WEEKDAY(CURDATE()))
Yields :
2019-01-28
Demo on DB Fiddle
As commented by Nick, here is an example of a query that uses the above approach to filter a table :
SELECT t.*
FROM test t
WHERE t.idate >= SUBDATE(CURDATE(), WEEKDAY(CURDATE()))
ORDER BY t.idate
Demo on DB Fiddle
You have example of how your table looks?
You could insert records with date and week numbers.
If you record days by week numbers it's easy like :
SELECT * FROM weektable WHERE weekno='5';
.. There are 52 weeks in a year ..
This will select everything from week number 5 and will be adding new rows from same week number automatically.
So what I want to achieve is to return data from within the last 3 months.
My current MySQL query is as follows:
SELECT MONTH(service_date_time) AS month, SUM(service_price) AS total FROM appointments WHERE user_id = 1 AND service_date_time >= last_day(now()) + INTERVAL 1 day - INTERVAL 3 month GROUP BY YEAR(service_date_time), MONTH(service_date_time)
My table appointments contains data from January and May and service_date_time is a date_time field.
The problem I'm having is that it returns 2 rows, one row totaling the price for January and one row totaling the price for May. The row for May shouldn't be returned since it is not within the past three months.
Anyone have an idea why?
You are requesting all records that are greater than the given date, If you want all the records up to now you'll have to ask for a range for example:
WHERE service_date_time BETWEEN (LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 3 MONTH) AND NOW()
this would limit the records & give you from 3 months ago till now
I want to calculate some percentage etc. between 1st and 13th of every month.
Like JAN 1st to JAN 13th Do some calculation. Similarly for every month I have to make the calculation for the first 13 days. How to get the first 13 days or any number of days in MySQL?
Assuming that you have a column of date data type it could be like this:
SELECT MONTH(date_column) AS mnth,
SUM(etc) AS col1,
SUM(etc2) AS col2
FROM tbl
WHERE DAY(date_column) BETWEEN 1 AND 13
GROUP BY MONTH(date_column)
So I'm setting up a query where I need to get items whose going to expire in a week before its expiration date(its expiration date is a month from its creation date)
I'm not really familiar with mysql's date and time functions so I'm not so sure of the syntaxes. Much appreciated ahead of time
EDIT: example, an item is created in feb 20th, its expiration date is march 20th. And lets say today is march 13th, my query needs to get the items whose expiration date is next week.
This is what I'm thinking what it may look like
SELECT * FROM ITEMS WHERE NOW() <= DATE_ADD(orders_items.cre_date, INTERVAL 1 MONTH) - 7 days?
you can do it the other way around (less complex)
get the items, whom were created 21 days -or more- ago
SELECT * FROM orders_items WHERE datediff(NOW() , orders_items.cre_date) >= 21
just to explain why 21? it's 3 weeks since create-date which leaves 7 days to expiration date.
you can read more about datediff