we have a mysql table with the below fields
employeename, shift from date todate
111aaa, B, 2018-09-01 2018-09-30
222bbb, C, 2017-04-01 2030-12-01
how can i write a query like when i give particular employee number and particular date like 2018-09-20 it must show his shift as B as the date range fall under about 1st row date range.
pl. help me.
You could try this;
SELECT * FROM employees WHERE employeename = '111aaa' AND fromdate <= '2018-09-02' AND todate >= '2018-09-02' ORDER BY fromdate DESC LIMIT 1
This selects an employee who's employname is '111aaa' and where a supplied date '2018-09-02' is between the fromdate and todate on the employee record. It sorts the results with the latest fromdate first, and only returns a single result.
Related
I have a table (Hire Rates) which contains multiple records. I'm trying to find records based on a date query but cannot get it to work correctly.
Essentially I am looking for records that contain a date between two dates. If my query date is "2021-08-15", it should return the third row (3000) as the query date falls between the two dates.
It is mostly working for me, except if the query date is equal to the start date or end date - in that case it doesn't return any result.
Table
startDate
endDate
hireRate
2021-01-01
2021-03-05
2350
2021-03-06
2021-04-08
2890
2021-04-09
2021-09-15
3000
Query
$sql = "SELECT rate, currencyID FROM hire_rates WHERE status = '1' AND NOT (startDate >= '$queryDate' OR endDate <= '$queryDate')
Try adding 'between' condition.
Like
select rate, currencyId from hire_rates where states =1 and not (cast($queryDate as DATE) between cast(startDate As DATE) and cast(endDate AS DATE) )
You would pass in the date you care about as a parameter. Then you can use between (based on your description of the logic):
SELECT rate, currencyID
FROM hire_rates
WHERE status = 1 AND
? BETWEEN startDate AND endDate;
Where ? is a parameter for the date you are passing in.
For 2021-08-15, you can write:
WHERE status = 1 AND
'2021-08-15' BETWEEN startDate AND endDate;
If you want the current date, you can actually get that from the database:
SELECT rate, currencyID
FROM hire_rates
WHERE status = 1 AND
curdate() BETWEEN startDate AND endDate;
Just try this then, `$sql = SELECT hireRate FROM hire_rate WHERE startDate<= '$queryDate' and endDate >= '$queryDate'
first of all i want to extract dates of the relevant month from the database.for that i have used following query and i had generated the following output.
code
SELECT DISTINCT date as tot FROM attendance WHERE date LIKE '%2016-06%'
output
then i need to get the present employees for that dates from the database.but when i try that, it gives me total for one date only.how can i over come this.Here is my code.
SELECT DISTINCT date as days, COUNT(DISTINCT employee_id) as tot FROM attendance WHERE in_time != '' AND out_time != '' AND date LIKE '%2016-06%'
You need to use group by. mysql just returns an arbitrary value when you omit fields from the select list that are not included in aggregate functions. Then you can remove distinct:
SELECT date as days, COUNT(DISTINCT employee_id) as tot
FROM attendance
WHERE in_time != '' AND out_time != '' AND date LIKE '%2016-06%'
GROUP BY date
Per comment, what data type is your date field? Assuming it's a date, you could remove like and use year and month:
AND Month(date) = 6 AND Year(date) = 2016
I have a table which has product quantity column and the database has multiple entries in a single month.
Date format is (YYYY-MM-DD)
Date Quantity
2016-03-01 1200
2016-03-05 200
2016-04-05 500
2016-04-10 1000
2016-05-05 850
2016-05-10 50
So I want data as:
March (2016-03-01 to 2016-03-31) = 1400
April (2016-04-01 to 2016-04-30) = 1500
May (2016-05-01 to 2016-05-31) = 900
How can I do this?
Use the following query and it will return the result that you want.
' date field ' is the name of the column where date is inserted and Tablename is the name of the Table.
SELECT MONTH(date field) as month , YEAR(date field) as year , SUM(quantity) as
quantity FROM Tablename GROUP BY MONTH( date field )
select MONTH(Date) As dt , SUM(quantity) total from #Table1
GROUP BY MONTH(Date)
You can use DATE_FORMAT to convert the date field into just the year-month format, select the SUM of the quantity field, and then GROUP_BY the year-month field.
SELECT DATE_FORMAT('%Y-%m', `date`) as `ym`, SUM(`quantity`) FROM `table` GROUP BY `ym` ORDER BY `ym` ASC
You can try grouping by the month and year, taking the sum of the quantity field as an aggregate.
SELECT MONTH(Date), YEAR(Date), SUM(quantity)
FROM yourTable
GROUP BY MONTH(Date), YEAR(Date)
If you want the fancy output with actual date ranges, that would be a bit more work to do. Those dates may not exist in your original data set, so it could require a date table. And handling February in a leap year could be a real pain.
Use the following query same as it is it will return what you want
SELECT CONCAT(DATE_FORMAT(date,'%b'),' ',
(SELECT CONCAT('(',YEAR(date),'-',LPAD(MONTH(date),2,0),'-01 - ')
), (SELECT concat(last_day(date),')')
)) dates, SUM(quantity) qty FROM DATES GROUP BY MONTH(date) ORDER BY MONTH(date)
I retrieved data from my table by summing result and show in table.I want to show sum result record between date='2014-01' and date='2014-04' in 2014-04 column name.And then,I want to retrieve other sum result in 2014-05 to current date.
How can I do that?
SELECT MONTH(regist_timestamp) AS Month, YEAR(regist_timestamp) AS Year,
count(id) AS numberOfVisits FROM ash_members WHERE
DATE(regist_timestamp) < CURDATE() GROUP BY
DATE_FORMAT(`regist_timestamp`,'%Y/%m' )
Format the date on SELECT then GROUP BY. Try with -
SELECT MONTH(regist_timestamp) AS Month, YEAR(regist_timestamp) AS Year,
count(id) AS numberOfVisits, DATE_FORMAT(`regist_timestamp`,'%Y/%m' ) formatted_date
FROM ash_members
WHERE DATE(regist_timestamp) < CURDATE()
GROUP BY formatted_date
I have a table with two fields - a field for "date" entered as day/month/year and a field "rate"
date | rate
24/01/05 | 1.9754
26/01/05 | 1.3723
...
and so on
So, I like to find minimum and maximum values of "rate" for each month of year(s). My query selects only one row
SELECT DISTINCT DATE_FORMAT(date, '%d-%m-%y') as Date, MIN(rate) as r, MAX(rate) as mr FROM rates
This will get the rate values for each month and year
SELECT
YEAR(date) AS thisYear,
MONTH(date) AS thisMonth,
MIN(rate) AS minRate,
MAX(rate) AS maxRate
FROM rates
GROUP BY thisYear ASC, thisMonth ASC
If you need to have the individual date(s) on which the min or max occurs you'll need some additional grouping in there, however the above should suffice for your original question.
To get the minimum and maximum values of "rate" for each month of year, you can modify the query as follows:
SELECT DATE_FORMAT(date, '%m-%Y') as Month_Year, MIN(rate) as Min_Rate, MAX(rate) as Max_Rate
FROM rates
GROUP BY DATE_FORMAT(date, '%m-%Y');
The GROUP BY clause groups the rows in the rates table based on the month and year of the date field. The MIN and MAX functions then calculate the minimum and maximum values of the rate field for each group. The DATE_FORMAT function is used to format the date field into a string with the format %m-%Y, where %m is the month (01-12) and %Y is the year (e.g., 2005).
This query will return a result set with one row for each unique month and year in the rates table, along with the minimum and maximum values of the rate field for that month and year.