Using the NOW Function In Access - ms-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.

Related

How to make SSRS subscriptions Kick off on Business Day 6 of month

Found a similar question
5th BUSINESS DAY subscription SSRS
But in that case a work around -schedule for 1st of month- was suggested and accepted as the answer.
I want to know if anyone has found a clever way to make SSRS subscriptions run on a specified Business Day. I.E run on Business Day 6. This is not just to prevent the report from going out on a weekend, but also because certain Finance operations related to closing the month have an agreed upon date (EX. "Will be done by BD 3") And I need my report to run after that each month.
One comment also suggested setting up a sql agent job to calculate what BD X would be each month, and insert a SQL job for that date to kick off the Report. I can see in theory how this would work - but the subscription wouldn't be managed within SSRS then and could easily be over-looked in the future.
I have been getting by with the following imperfect code:
--: returns last day of last month (DT), but only provides a result row if we are on BusinessDay X of the month.
--No rows returned prevents the report from being processed & sent.
--SSRS Schedule must be set to run monthly in a range covering all possible Calendar Days that could be Business Day X
declare #dt datetime = Getdate(), #BDTarget int = 6
SELECT DATEADD(MONTH, DATEDIFF(MONTH, -1,#dt)-1, -1) as DT,sum(dimdate.BusinessDay) as BD
FROM DimDate
where FullDate between DATEADD(mm, DATEDIFF(mm, 0, #dt), 0) and #dt
having sum(dimdate.BusinessDay)=#BDTarget
Though recently discovered that this logic can kick off the report two days in a row, for example if we are looking for BD 6, this month July 2021 the above query returned a row on both Friday 7/9 and Sat 7/10.
You can use a data-driven subscription that runs daily.
Use a query that will only give a result on the 6th business day.
e.g.
WITH cte_6th
AS
(
SELECT *
FROM dimDate dd
WHERE dd.TheMonth = MONTH(current_timestamp)
AND dd.TheYear = YEAR(current_timestamp)
AND dd.BusinessDay = 1
ORDER BY dd.Date
OFFSET 5 ROWS
FETCH NEXT 1 ROWS ONLY
)
SELECT *
FROM cte_6th c
WHERE c.Date = CAST(current_timestamp as DATE);

MySQL - get data for time before some quarter AND year

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.

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

sql Date in month time span

I have entries in my Table like
uid / start / end / value
I now want to select all entries which lie in a specific month. So if the user chooses "June", I want to get all Entries than are available in June. The start and end are saved as timestamp (cant change that). I found somthing like:
WHERE month(start)=5
This does work, but unfortunately it only gives me the entries that start in June. I Can of course add the same for the end, but this would still not help if an entry starts in may and ends in july. I could of course calculate timestamps and compare directly, but i want to select this for june of any year - not just one specific. I was thinking of something like:
WHERE month(start) <= 5 && month(end) >= 5
which would work fine with timestamps, but obviously this has a problem with year-breaks.
Is there a nice solution to do this without calculating all timestamps for the following years and creating a sick big query?
Ok i figured this out:
WHERE month(start)=5
OR month(end)=5
OR ( month(start)<=5 AND month(end) >= 5)
OR ( month(start)<=5 AND year(start)<year(end))
OR ( month(end)>=5 AND year(start)<year(end))
I think it is correct and works fine.
It looks like it should work, but it's quite convoluted, and I'm pretty sure the use of the month function means you won't be hitting any indices.
You can also rephrase it as:
where start <= 1 Jun 2011
and end >= 1 Jul 2011