MySQL - get data for time before some quarter AND year - mysql

I need to get MySQL results for users records with 'registered' date that happens BEFORE some quarter for some year.
For example to get data (users count, registered before Q3 2018) I do this request:
SELECT count(userid) AS value
FROM users
WHERE QUARTER(registered) <= "3" && YEAR(registered) <= "2018"
However this request give me not expected results (value much lower that it should be, looks like I get results only for Q3 for 2018 year, and without values for previous years with any quarters). If I remove quarter and check just data before Year - this works fine and show correct results for every year that I use.
Why this happens and how to correctly get value with Quarter?

You can use the following solution:
SELECT COUNT(userid) AS value
FROM users
WHERE YEAR(registered) < 2018 OR (
YEAR(registered) = 2018 AND QUARTER(registered) <= 3
)
With your current SELECT you get only all users which registered on third quarter or ealier on every year (2018 and earlier). But you can count all users of the years before 2018 (the quarter doesn't matter in this case) and all users of the current year but in the specified quarter or ealier.

Related

check if in existing result set all days within month are present

How can I check if in my result set (which is formed by query shown below at the end) for every month exists every day? For example in below result set:
Date_List
08/01/2016
08/02/2016
08/03/2016
08/04/2016
08/05/2016
08/06/2016
08/07/2016
08/08/2016
08/09/2016
08/10/2016
08/11/2016
08/12/2016
08/13/2016
08/14/2016
08/15/2016
08/16/2016
08/17/2016
08/18/2016
08/19/2016
08/20/2016
08/21/2016
08/22/2016
08/23/2016
08/24/2016
08/25/2016
08/26/2016
08/27/2016
08/28/2016
08/29/2016
08/30/2016
08/31/2016
We can see that August 2016 has all days available. I want to return only such months (with all days available) with a year part of course. In this case it would be 08/16 (MM/YYYY) to return. Thus if any day for any month is missing - I do not want to return such month.
Here is the query I use to form result set:
SELECT d.`Date_List` FROM staging_area.`g360_dates_ca` d
LEFT JOIN dm_research_development.g360_pro_saas_availability a
ON d.`Date_List` = a.`DATE_OCCURRED_DATE` AND a.`IS_OUTAGE` = 'YES'
WHERE a.`DATE_OCCURRED_DATE` IS NULL AND STR_TO_DATE(d.`Date_List`,'%m/%d/%Y') <= NOW();
g360_dates_ca table contains all dates starting 01-01-2013 till 01-01-2018.
g360_pro_saas_availability - is my incidents table with details.

Grouping months by quarter over multiple years depending on a dynamic start month

Using MySQL and PHP I am building a JSON array to populate a data table.
For the purposed of my question suppose the data table has the following columns:
Year: 2010,2011,2012,2013...<br/>
Month: 1,2,3,4,5...<br/>
Value: 100, 150, 200 etc...<br/>
The table structure cannot be altered and my solution needs come into the MySQL query
The data can be viewed either monthly, quarterly or yearly. Monthly and yearly is achieved easily through grouping by year and month.
Quarterly data can be grouped by calendar quarter (Jan-Mar, Apr-Jun, Jul-Sep, Oct-Dec) by this group statement:
GROUP BY year, round((month/3)+0.3,0)
So where Jan, Feb and March might all have 100 for their value the summed result is 300, same for other months.
Now my problem comes when I want to group values by a financial quarter, for example a quarter that starts in Feb, or any other quarters.
I have a statement that works for the quarter grouping using two variables that can be accessed via the SQL query, start_year (i.e. 2014) and start_month (i.e. 2)
GROUP BY year, (((round(((((month-(start_month-1))+((year-start_year)*12))-((year-start_year)*12))/3)+0.33,0)/4)+4)-floor(((round(((((month-(start_month, '%m')-1))+((year-start_year)*12))-((year-start_year*12))/3)+0.33,0)/4)+4)))*12
which basically will assign a 0,3,6,9 value to each calendar month for the purposes of grouping.
In the financial year starting February this works fine for quarters 1-3, however breaks for the final quarter as it includes Nov and Dec 2014 data and Jan from 2015.
As a result I get 5 rows of data instead of 4.
This is because of the preceding GROUP by year clause, an important addition as we might want to generate a table that views sequential quarters for multiple years.
So what I am looking for is a way of grouping the years together by offsetting the start month.
So when the year starts in Jan it will group Jan-Dec but if we change that to starting Feb it will group Feb-Jan.
Any ideas, suggestions most welcome!
Regards,
Carl
I solved a similar problem just now (a Moodle report aggregating assignment scores by year and quarter) with something like this:
select year(from_unixtime(s.timemarked)) as year, quarter(from_unixtime(s.timemarked)) % 4 + 1 as quarter, count(distinct data1) as "tickets graded" from mdlassignment_submissions s where grade >= 0 group by year, quarter order by year, quarter;
The relevant part for what you're doing is quarter(from_unixtime(s.timemarked)) % 4 + 1 as quarter
As another commenter pointed out, MySQL has a quarter() function, but it doesn't do financial quarters. However, since (as I understand it, at least, based on consulting the relevant wikipedia page) financial quarters are just offset by 1, the % 4 + 1 at the end should convert it.

Get sum of values based on the value of 2 other date related columns

Given the sample data in the screenshot below, would it be possible in mysql to return a sum of values from monthly_amount only where the values are before this month. I used a join to pull this data. The 5 left columns are from one table, and the rest are from another.
The issue I'm running into is, lets say its April of 2015, I can't just do a sum WHERE goal_year <= 2015 AND month_id_FK <= 4, or else I'll get only those 4 months from both years, when in that scenario, I really want all the months from 2014, plus the 4 months from 2015.
I could handle this in PHP, but I wanted to first see if there would be a way to do this in mysql?
try
WHERE Goal_Year*100+month_id_FK <= 201504
alternatively:
WHERE
GOAL_YEAR < 2015 OR
(GOAL_YEAR = 2015 and month_id_FK <= 4)
select sum(monthly_amount) from table where goaldate<(SELECT CURDATE())
this is not the actual query for your table..but if you do like this you will get the answer
you need the sum of monthly amount where the date is before current-date means today.
then you can just compare the currentdate with goal date

Using the NOW Function In Access

I am setting up a code to pull all employees hired within the last 2 years who got a certain rating. I have been looking into the YEAR(NOW()) function but I am having a difficult time setting it up. I need to use the NOW function because I need it to pull the data from the time the user access the query. The ratings are completed every following feburary (i.e 2013 ratings will be completed in February of 2014) so it needs to read something like
YEAR(NOW()-12) but it
This way if I were to run it today it would go back and pull the ratings for 2012 and 2011 since 2013 have not yet been completed.
My whole code looks like:
SELECT dbo_v_TMS_QPR_01_Score.TMS_ID, dbo_v_TMS_QPR_01_Score.QPR_Year, dbo_v_TMS_QPR_01_Score.Final_QPR_Score
FROM O867IA_VJOBHST INNER JOIN dbo_v_TMS_QPR_01_Score ON O867IA_VJOBHST.SYS_EMP_ID_NR = dbo_v_TMS_QPR_01_Score.GEMSID
WHERE (((dbo_v_TMS_QPR_01_Score.Final_QPR_Score)>="1.25") AND ((O867IA_VJOBHST.EMP_ACN_TYP_CD)="HIR") AND ((O867IA_VJOBHST.REC_EFF_STT_DT)=Year(Now()-12)))
GROUP BY dbo_v_TMS_QPR_01_Score.TMS_ID, dbo_v_TMS_QPR_01_Score.QPR_Year, dbo_v_TMS_QPR_01_Score.Final_QPR_Score;
But I keep getting the error: INCONSISTENT DATATYPES: EXPECTED DATE GOT NUMBER (#932)
What you have does not work. It subtracts 12 days off the current date/time and then converts it to the year. Thus, it returns 2013.
Use the dataadd() function. The following is a blank query in the query designer.
I am asking for today's date minus 12 months. See output below.
I would THINK you want something like:
If Month(Now()) > 3 then 'If it's after Feb, we want the last 2 years
LastDayOfPrevYear = DateAdd("d", -1, DateSerial(Year(Now()), 1, 1))
Else 'If it's before March, we want the 2 years prior to last year
LastDayOfPrevYear = DateAdd("d", -1, DateSerial(Year(Now())-1, 1, 1))
End If
SELECT dbo_v_TMS_QPR_01_Score.TMS_ID, dbo_v_TMS_QPR_01_Score.QPR_Year, dbo_v_TMS_QPR_01_Score.Final_QPR_Score
FROM O867IA_VJOBHST INNER JOIN dbo_v_TMS_QPR_01_Score ON O867IA_VJOBHST.SYS_EMP_ID_NR = dbo_v_TMS_QPR_01_Score.GEMSID
WHERE (((dbo_v_TMS_QPR_01_Score.Final_QPR_Score)>="1.25") AND ((O867IA_VJOBHST.EMP_ACN_TYP_CD)="HIR") AND ((O867IA_VJOBHST.REC_EFF_STT_DT)>=DateAdd("m", -24, LastDayOfPrevYear)))
GROUP BY dbo_v_TMS_QPR_01_Score.TMS_ID, dbo_v_TMS_QPR_01_Score.QPR_Year, dbo_v_TMS_QPR_01_Score.Final_QPR_Score;
This will give you a "rolling" 24 month timespan from the last date of the previous year.
This may need a little tweaking, but it should be, at the very least, extremely close.

get data for the year within from and to dates

I have the following query which gets year,Sum data between from and to date.
It works perfectly.
SELECT strftime('%Y',Date), SUM(Amount)
from table where (Date between '2008-08-01' and '2012-07-31')
GROUP BY strftime('%Y',Date)
I am displaying the sum on a bar chart.
Now If I click on the respective year it should show all the transactions till the "To Date" for that year or if the value is smaller than "To date" then the entire year's transaction.
For Eg: In the example above it displays Sum From Date : 2008-08-01 and To Date: 2012-07-31 .
If the user taps on the year 2012 bar column it should show transactions till the date 2012-07-31 and not for the entire year but if the user taps on 2011 it should transaction for the entire year as the TO Date is greater than 2011 year.
Same for the year 2008, it should display data from "2008-08-01" not the entire year and if I tap the bar column of that year it should show the transactions from that particular date till the end of the year and not for the entire year.
I have the query below which returns wrong value .
SELECT strftime('%Y',Date) ,*
from table where (Date between '2011-08-01' and '2012-07-31')
and strftime('%Y',Date)='2012'
GROUP BY strftime('%Y',Date)
This returns only one row for the date 2012-01-01 not all the values for the year 2012 till 2012-07-31.
Basically I would like to change only year in the following query and rest should be same. strftime('%Y',Date)='2012'
I don't know how to fix it or How to go about it w.r.t the above requirement.
Please help me fix it or Any Alternative. Thanks in Advance.
You don't need GROUP BY
SELECT strftime('%Y',Date) ,*
from table where (Date between '2011-08-01' and '2012-07-31')
and strftime('%Y',Date)='2012'