Displaying number of years worked since joined date - mysql

List all keepers that earn more than $37,000 a year at the zoo. Show the keepers name, the total salary for a year (assume 38hrs week) and the number of years they have worked at the zoo.
Select surname
, given
, RatePerHour*38*52
From Keeper
Where ‘ratePerHour’ *38*52 > 37000
This is what i've got so far, not sure how I'm supposed to display number of years worked?
Theres a "joined" table that shows date joined at the company.
I thought about trying '2019-03-08' - 'Joined' but that displayed the incorrect answer.

My best guess would be to utilize mySql's CURDATE() function to fetch the current date rather than hard coding today's date.
Then you can use DATEDIFF(%FutureDate%, %PastDate%) function to calculate the number of days worked and finally convert those number of days in a yearly representation.

Related

sql counting per year and month, show months even when zero

So I'm counting articles per year/month between the start of the year and the current time:
SELECT Year(FROM_UNIXTIME(date)) as year
, Month(FROM_UNIXTIME(date)) as month
, Count(*) as `total`
FROM articles
WHERE date BETWEEN UNIX_TIMESTAMP(DATE('2017-01-01 00:00:00')) AND UNIX_TIMESTAMP(DATE('2017-05-17 12:00:05'))
GROUP
BY Year(FROM_UNIXTIME(date))
, Month(FROM_UNIXTIME(date))
The only issue, is that months that have zero, won't show up.
Is there an easy way around it?
The best solution I can think of is to do an inner join with a lookup table that has months 1-12 in them. Thus verifying there will always be a 12 month result set?
Possibly including a restriction for the current date month can not be surpassed, so you don't actually always get for the whole year.
look here:
Include missing months in Group By query

Getting the number of days excluding years between two dates

I want to get the number of days ignoring year between two dates in MySql.
I am using the following query, which works fine when the dates are more than a year apart.
select dayofyear(from_days((to_days('2015-11-20') - to_days('2014-11-15'))))
select dayofyear(from_days((to_days('2019-11-20') - to_days('2014-11-15'))))
correctly both return 5
However when the two dates are in the same year
select dayofyear(from_days((to_days('2014-11-20') - to_days('2014-11-15'))))
this returns 0. I think this is because MySql has a problem with dates in years less than 100 (or at least the dayofyear function has).
Is there any way around this?
Absent your willingness to specify what happens on leap years, one is forced to guess.
I think this works. http://sqlfiddle.com/#!2/043543/1/0
IF(timestampdiff(DAY, st, str_to_date(concat_ws('-',YEAR(st),MONTH(fi), DAY(fi)),'%Y-%m-%d')) >= 0,
timestampdiff(DAY, st, str_to_date(concat_ws('-',YEAR(st),MONTH(fi), DAY(fi)),'%Y-%m-%d')),
timestampdiff(DAY, st, str_to_date(concat_ws('-',YEAR(st)+1,MONTH(fi), DAY(fi)),'%Y-%m-%d'))) delta
It tacks the starting year onto the ending day and tries to compute the number of days. Then, if that comes up negative, it tacks the year after the starting year onto the starting day and computes the number of days.
You are complicating this issue for nothing;
There is a DATEDIFF function in MySQL that allow you to do that.
SELECT DATEDIFF('2006-04-03','2006-04-01');
That would return 2 (days).
To get the result regardless of the year, then apply % 365.
Edit : If you want to get the exact number of days considering leap years, you could substract to the number of days, the number of days for both year at 1st january (or any other date) :
SELECT DATEDIFF('2006-04-03','2006-04-01') - DATEDIFF(concat(year(yourDate), '01-01'), concat(year(yourDate2), '01-01'));
This will substract the total number of days between 1st january of each year to the total number of days.

Calculating anniversaries of employees with their joining dates

As part of database testing, we are to verify if the data is correctly rendered onto the webpage from database.
We have a table called 'emp_details' which stores employee details. We store joining date of an employee in it. Now, using this joining date field, I need to get a list all the employees who have a start date or anniversary date within the last ten days.
I tried various combinations of DATEDIFF() in MySQL but did not succeed.
The format on the webpage would look like this:
Name Start Date Years
----------------------------------
William 07/25/2004 8
Gordon 07/22/2007 5
Jill 07/26/2009 3
Could anyone please help me with the query for MySQL DB.
Thanks,
select * from
employees where
dayofyear(`start date`) between dayofyear(curdate())-10 and dayofyear(curdate())
You can use following in WHERE clause
DAYOFYEAR(CURDATE()) - DAYOFYEAR(start_date) < 10
OR is greater than (365 - 10)

How to get month using date in MySQL [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how do I get month from date in mysql
I want to get month using date example 2011-04-02 so I want month april. How to get this in MySQL?
SELECT MONTHNAME(date) AS monthName for January, February...
SELECT MONTH(date) AS monthName for 1, 2...
SELECT MONTHNAME(`date`) AS month_name FROM table_name;
You can use MONTHNAME() to get the month name. If you want month number, consider to use MONTH()
You can have a much more elegant solution to this if you use a second table as a date dimension table, and join your date field to it, in order to extract more useful information. This table can contain dates, month names, financial quarters, years, days of week, weekends, etc.
It is a really tiny table, only 365(ish) rows per year of data you have... And you can easily write some code to populate this table with as much data as you require. I did mine in Excel, exported as a CSV file and then imported the data into a blank table.
It also gives lots of benefits, for example, imagine a monthly data table with the following fields (and any others you can think of!) fully populated for all the months in a given range;
Date (E.g. 2009-04-01)
Day (E.g. 1)
Day of Week (E.g. Wednesday)
Month (E.g. 4)
Year (E.g. 2009)
Financial Year (E.g. 2009/10)
Financial Quarter (E.g. 2009Q1)
Calendar Quarter (E.g. 2009Q2)
Then combining this with your own table as follows;
SELECT `DT`.`monthName`
FROM `your_table`
INNER JOIN `dateTable` as DT
ON `your_table`.`your_date_field` = `dateTable`.`theDate`
There are many other nice outputs that you can get from this data.
Hope that helps!

MySQL - Find date ranges matching a list of months

I have several rows in a table, each containing a start date and an end date. The user has a checkbox for each month of the year. I need to determine which rows contain a date range that includes any of the user's chosen months.
It's easy to check the start & end months by, for example, MONTH(start_date) IN ($month_list), but this approach won't match any months between the two dates.
So I suppose what I'm asking is: is there a way of obtaining the inclusive months from a date range purely in SQL?
I assume you would want to include data rows where the date range spans or intersects with the selected periods - in which case, I'd shove the user selected periods into a table and do a fuzzy join, something like.....
SELECT DISTINCT at.*
FROM a_table at, user_periods up
WHERE at.start_date<=up.end_date
AND at.end_date>=up.start_date
AND up.trans_id=$SOME_VAR
(the trans_id just allows the table to be used for multiple operations)
To minimise the effort here, the user_periods table should have an index on start_date and end_date, and similar for a_table.
Can something like this help?
WHERE
MONTH(start_date) < MONTH_YOU_ARE_CHECKING and
MONTH() > MONTH_YOU_ARE_CHECKING
If you need to check all at once you can do a list of all the months and after delete from the list the month that the user choose, and after compare against the list. It will be better with a pseudocode example :)
MONTHS = 1,2,3,4,5,6,7,8,9,10,11,12
USER_SELECTED_MONTHS= 1,6,8,9,12
LIST_TO CHECK = 2,3,4,5,7,10,11
so, now you can do:
MONTH(start_date) NOT IN (2,3,4,5,7,10,11)
What do you think, could it help you?
regards