Table name: activity
Field name: ProcessYM
I have mysql data like below.
ProcessYM
==========
201312
201311
201310
201309
201308
201307
201306
201305
201304
201303
201302
201301
201212
201211
201210
201209
201208
201207
201206
I want to fetch the result like below. I mean, the mysql query to fetch the every quarter of the year like 201312, 201309, 201306, 201303, 201212, 201209.. and so on.
Actual Output I expect
=======================
ProcessYM
201312
201309
201306
201303
201212
201209
201206
I have tried the below query, but it does not produce the expected result.
SELECT distinct `ActProcessYM` from `activity` where `ActProcessYM`%3=0 order by ActProcessYM desc
Output of above query
=====================
201312
201309
201306
201303
201210
201207
It is much appreciated for your smart reply.
You need to modulo of the month part only. Your query is implicitly casting your ProcessYM as an INT.
For example:
SELECT DISTINCT ProcessYM
FROM activity
WHERE RIGHT(ProcessYM,2)%3=0
ORDER BY ProcessYM DESC
fiddle
you should retrieve the last two digits from field value and do the logic as you are doing.
SELECT distinct `ActProcessYM` from `activity` where substring(ActProcessYM,5,2)%3=0 order by ActProcessYM desc
Here's a not-so-quick-and-dirty way of handing this date processing. I believe you're looking for a MySQL formula like this:
yyyymm = TRUNC_QUARTER(yyyymm)
That is, you are looking for a function that converts any yyyymm month notation into a notation that shows the month that ends the quarter in question.
Let's start with an expression that converts any particular DATETIME expression to the DATE of the beginning of the quarter.
DATE(CONCAT(YEAR(value),'-', 1 + 3*(QUARTER(value)-1),'-01'))
This takes a timestamp (e.g. '2011-04-20 11:15:01') and turns it into the date of the starting of the quarter. (e.g. '2011-04-01')
Having things in this date form is helpful because you can use them for date arithmetic. For example, you can get the last day of that quarter like this.
DATE(CONCAT(YEAR(value),'-', 1 + 3*(QUARTER(value)-1),'-01'))
+ INTERVAL 1 QUARTER - INTERVAL 1 DAY
Here's a writeup on all this: http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/
I've found it helpful to try to stick to the date datatype when processing time series data.
You need to separate out the month value before doing the modulo 3 (% 3). Doing a modulo 100 first will do it:
(ProcessYM % 100) % 3) = 0
or
mod(mod(ProcessYM,100),3) = 0
Try this,
SELECT distinct `ProcessYM` from `activity` where SUBSTRING(`ProcessYM`,5,2)%3=0 order by ProcessYM desc
Related
I have a sample table here with the following columns and sample records. I want to be able to sum my column cases using with a specific date range (the helper column).
I want to get my results this way:
Sum all cases WHERE date range is in between 2022-03-23 - 2022-04-01 and so on.
date range
Sum of Cases
2022-03-23-2022-04-01
5 (sample result only)
2022-03-24-2022-04-02
9 (sample result only)
The logic of the date range is always n - n9 days.
I 've tried this type of query but it does not work, it there a way for me to get this without have to use a query to create another column?
SELECT Date,
sum([QUERY 1]) as "Reports 7 days prev",
sum ([QUERY 2]) as "Reports 7 days after"
FROM REPORTS
GROUP BY Date
Data:
Date
BuyerID
Cases
Helper (Date Range)
4/1/2022
20001
2
2022-03-23-2022-04-01
4/1/2022
20001
1
2022-03-23-2022-04-01
4/2/2022
20002
3
2022-03-24-2022-04-02
4/5/2022
20003
5
2022-03-27-2022-04-05
4/7/2022
20004
6
2022-03-29-2022-04-07
4/7/2022
20005
9
2022-03-29-2022-04-07
Are you looking to get total cases for last X number of days? What does your initial data look like?
you can try something like:
Step 1: You aggregate all the cases for each date.
WITH CASES_AGG_BY_DATE AS
(
SELECT Date,
SUM(Cases) AS Total_Cases
FROM REPORTS
GROUP BY Date
),
Step 2: you aggregate the last 7 days rolling cases sum for each date
LAST_7_DAY_AGG AS
(
SELECT Date, SUM(Total_Cases) OVER(ORDER BY Date ASC ROWS BETWEEN 7 PRECEDING AND CURRENT ROW) AS sum_of_cases,
LAG(Date, 7) AS 7th_day
FROM CASES_AGG_BY_DATE
)
Step 3: create final output and concatenate date and 7th day before that
SELECT Date, CONCAT(Date, "-", 7th_day), sum_of_cases
FROM LAST_7_DAY_AGG;
I am using MySQL 8 and need to create a stored procedure
I have a single table that has a DATE field and a value field which can be 0 or any other number. This value field represents the daily amount of rain for that day.
The table stores data between today and 10 years.
I need to find out how many periods of rain there will be in the next 10 years.
So, for example, if my table contains the following data:
Date - Value
2018-06-09 - 0
2018-06-10 - 50
2018-06-11 - 0
2018-06-12 - 15
2018-06-13 - 17
2018-06-14 - 0
2018-06-15 - 0
2018-06-16 - 12
2018-06-17 - 123
2018-06-18 - 17
Then the SP should return 3, because there were 3 periods of rain.
Any help in getting me closer to the answer will be appreciated!
You don't need to have a stored procedure for this.
A solution with MySQL's 8.0 LEAD function this supports dates with gaps.
The complete table needs to be scanned but i don't think that a huge problem with ~3560 records.
Query
SELECT
SUM(filter_match = 1) AS number
FROM (
SELECT
((t.value = 0) AND (LEAD(t.value) OVER (ORDER BY t.date ASC) != 0)) AS filter_match
FROM
t
) t
see demo https://www.db-fiddle.com/f/sev4NqgLsFPgtNgwzruwy/2
By the way, would you mind expanding your answer to understand how
LEAD and SUM work together?
LEAD(t.value) OVER (ORDER BY t.date ASC) simply means get the next value from the next record ordered by date.
this demo shows it nicely https://www.db-fiddle.com/f/sev4NqgLsFPgtNgwzruwy/6
SUM(filter_match = 1) is a conditional sum. in this case the alias filter_match needs to be true.
see what filter_match is demo https://www.db-fiddle.com/f/sev4NqgLsFPgtNgwzruwy/8
In MySQL aggregate functions can have a SQL expression something like 1 = 1 (which is always true or 1) or 1 = 0 (which is always false or 0).
The conditional sum only sums up when the condition is true.
see demo https://www.db-fiddle.com/f/sev4NqgLsFPgtNgwzruwy/7
Use MySQL join:
SELECT COUNT(*) Number_of_Periods
FROM yourTable A JOIN yourTable B
ON DATE(A.`DATE`)=DATE(B.`DATE` - INTERVAL 1 DAY)
AND A.`VALUE`=0 AND B.`VALUE`>0;
See Demo on DB Fiddle.
I wanted to arranged my table in a proper sequence of date to both the date_due column and date_paid column.
My table structure shows like this and it is ordered by the due_date column:
Period Due Date Paid Date
12/01/2014
02/05/2014
1 01/01/2015
2 02/01/2015
3 03/01/2015
4 04/01/2015
Then all I want is to do this:
Period Due Date Paid Date
12/01/2014
1 01/01/2015
2 02/01/2015
02/05/2014
3 03/01/2015
4 04/01/2015
Ok forget about my stupid comment - it was not constructive. First of all I think you have a mistake in your data: I guess "02/05/2014" should be "02/05/2015".
The problem is that you have to aggregate the two columns, to be able to sort after that. Using the GREATEST function like I stated in my comment actually would not work, because it is not null-save. You can use for example the COALESCE function here.
All in all this can either be done by "adding a column" like
SELECT
*, Coalesce(`Due Date`, `Paid Date`) as bananas
FROM
table1
ORDER BY
bananas
Or add the expression directly to the ORDER BY clause:
SELECT
*
FROM
table1
ORDER BY
Coalesce(`Due Date`, `Paid Date`)
Please see this fiddle and try yourself: http://sqlfiddle.com/#!2/21a84/1
But I cannot Format it? Date is saved currently in the database under a 'text' type structure and saved a '1/1/2000'. I'm trying to pull this info out of the database and DATE_FORMAT it to '01/01/2000' (2 digit day & month) but it's showing all dates as 12/31/1969? Here's the query i'm using:
$query = "SELECT Event_Title, Event_Details, Event_Time, Event_ID, DATE_FORMAT(Start_Date, '%m/%d/%Y') FROM tevents ORDER by Start_Date DESC";
Is there something wrong with my query?
database:
Rows Start_Date Ascending
1 04/30/2014
1 05/03/2014
1 05/1/2014
1 05/3/2014
3 5/1/2014
1 5/15/2014
1 5/2/2014
2 5/20/2014
1 5/23/2014
1 5/24/2014
5 5/3/2014
3 5/6/2014
1 6/21/2014
1 6/23/2014
1 6/24/2014
1 6/25/2014
1 6/26/2014
1 6/27/2014
1 6/28/2014
1 6/5/2014
1 6/6/2014
Date stored as varchar or TEXT makes life miserable so better to store as date or datetime data type.
In your case you need to use str_to_date() for conversion before doing any format
$query = "SELECT
Event_Title,
Event_Details,
Event_Time,
Event_ID,
date_format(str_to_date(Start_Date,'%m/%d/%Y'),'%m/%d/%Y') as Start_Date
FROM tevents
ORDER by date_format(str_to_date(Start_Date,'%m/%d/%Y'),'%Y-%m-%d') DESC";
If its just for ordering then no need to do the
date_format(str_to_date(Start_Date,'%m/%d/%Y'),'%m/%d/%Y') as Start_Date
You can directly select since its converting to the same format, for ordering its needed.
If you put a question into a closed envelope and give it to the neighbour, why can't he answer you?
'Text' means 'do not bother about the contents' for MySQL, it normally does not care about the content. If you use a DATE datatype, things get much easier at reading (and sorting works, and ranges, and where-clauses...) and a bit difficult on writing - you should format the dates into the form '2014-05-01' in the program code.
The following query didn't return correct results, because it returns results from "September" month but i need to get results from given month "August".
Is there something wrong in my query?
SELECT *
FROM table
WHERE YEAR(FROM_UNIXTIME(UNIX_date)) = '2012' AND
MONTH(FROM_UNIXTIME(UNIX_date)) = '08'
order by UNIX_date DESC
EDIT:
results that returned were like that:
post_id user_id UNIX_date
95319 12 1346475459
97370 5 1346474849
83527 25 1346474631
83526 51 1346473357
85929 12 1346471009
26677 29 1346462100
26839 12 1346432911
85927 12 1346411636
The month should not have a leading 0. So try 8 instead of 08.
You could also post a line of what is being returned so we can see how UNIX_date looks like.
reference: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_month
As written in the documentation http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_month, the DAY, MONTH and YEAR functions returns integer, thus values 1-12 (or zero) for the MONTH.