I have a small problem with selecting some results from the database.
The results I'm trying to select have a start_date and end_date which store the full date. My client wants a feature on his site to filter all of the records by month.
Here are a couple of example records:
id start_date end_date
01 01.03.13 20.05.13
02 12.04.13 30.06.13
03 24.05.13 29.07.13
04 10.05.13 30.05.13
05 19.06.13 13.08.13
06 03.07.13 18.09.13
If the month is 05, then records id 01-04 should be displayed.
Any ideas how this would be done with MySQL?
This query will return all events whose range includes May 2013.
SELECT *
FROM MyTable
WHERE start_date < '2013-06-01'
AND end_date >= '2013-05-01'
try
select id from yourTable where month(start_date) == 5 or month(end_date)==5
Related
We have mysql table with below columns. I am given startDate and endDate and now I have to fetch rows between those dates. Can some one please help with mysql query with how I can get those rows?
For example if startDate is November 2021 and endDate is March 2022. Then query should select rows with the months between these dates.
month
year
values
09
2021
30
10
2021
40
11
2021
90
12
2021
10
01
2022
25
02
2022
15
03
2022
89
You can compare tuples in MySQL:
select month, year, `values`
from tbl
where (year, month) >= ('2021', '12')
and (year, month) <= ('2022', '03');
Another way is combining year and month to a single string:
select month, year, `values`
from tbl
where concat(year, month) between ('202112') and ('202203');
This only works if year and month (at least month) are zero-filled strings.
I think the first step would be to compute a column with a proper DATE value, starting from the column you have.
For example, STR_TO_DATE(CONCAT(year,'-', month, '-', 1), '%Y-%m-%d') will return a proper SQL date on the 1st day of the year-month that you consider.
This value can then be compared (with <= and >=) with the limit dates (as requested in the comments, the answer depends on how those are represented).
how to code a query with this problem. I need to select total hours within a day. from first-in and Last-out then to sum again The period date_01 to date_16 which is 15days.
[EmployeeID] [Att_Date]
01 2022-02-01 07:39:12
01 2022-02-01 19:39:12
01 2022-02-02 08:39:12
01 2022-02-02 19:00:12
01 2022-02-03 08:00:12
01 2022-02-03 19:00:12
02 2022-02-02 08:00:12
02 2022-02-02 19:39:12... (Raj Marvic)
I tried this code below, but it just only sum result total hours from first-indate to Last-out date, which is its wrong. I need to total daily first, to sum the total hours from first-indate and Last-outDate.
SELECT EmployeeID,
TIMESTAMPDIFF(hour, MIN(Att_Date),MAX(Att_Date)) AS diff_in_hours
FROM att_details
GROUP BY EmployeeID;
I think the reason you are missing your correct total is because the MIN() and MAX() are getting the earliest date/time vs latest date/time which span multiple days. What I think you are trying to get is the aggregation of EACH DAY, such as totaled over a given week, or multiple day span. That said, your first query should be grouped by employee AND Date (without time context), THEN sum that result. One caveat not mentioned... Is it possible for one employee to clock-in/out multiple times in a single day, such as to account for lunch time where that time is excluded from the total work day? If so, that will need to be accounted for.
select
EmployeeID,
Sum( DailyHours ) TotalHours
from
( SELECT
EmployeeID,
Date( Att_Date ) SingleDay,
TIMESTAMPDIFF(hour, MIN(Att_Date),MAX(Att_Date)) AS DailyHours
FROM
att_details
GROUP BY
EmployeeID,
Date( Att_Date ) ) PQ
group by
EmployeeID
Using aggregation along with DATEDIFF we can try:
SELECT
[Employee ID],
DATEDIFF(hour, MIN([Date/time]), MAX([Date/time])) AS diff_in_hours
FROM yourTable
GROUP BY
[Employee ID];
table food
"Expire date"food name
"2010-01-01"porato
"2011-01-01"tomamto
"2013-01-01"chips
"2011-01-04"orange
"2017-01-01"banana
i wanna use select to get out how much food expire in date between 2010-01-01 to 2013-12-30
so the out should be like
2010 1
2011 2
2013 1
i try
select foodName,ExpireDate
from food
where ExpireDate between '2010-01-01' and '2013-12-30'
but not work
If you want the results by year, you need aggregation:
select year(ExpireDate) as year, count(*)
from food
where ExpireDate between '2010-01-01' and '2013-12-30'
group by year(ExpireDate);
Note: It seems odd that you are skipping the last day or two of 2013 (depending on whether ExpireDate has a time component). More typical logic would be:
select year(ExpireDate) as year, count(*)
from food
where ExpireDate >= '2010-01-01' and
ExpireDate < '2014-01-01'
group by year(ExpireDate);
I have student_mark table which has date, mark, student_id column.
I want to find max(mark) of each month of all students.
I am doing this:
select max(mark),exam_dt
from student_mark
where month(exam_dt) = 04 or month(exam_dt) = 05 or month(exam_dt) = 06
group by exam_dt;
but i am getting this.
It is grouping by individual date and not the month.
It should display the maximum marks of a month.
Just change your query to SELECT and GROUP BY the month i.e.
select max(mark), month(exam_dt)
from student_mark
where month(exam_dt) = 04 or month(exam_dt) = 05 or month(exam_dt) = 06
group by month(exam_dt);
use in instead of or and also use month function in group by
select max(mark),month(exam_dt)
from student_mark
where month(exam_dt) in (04,05,06)
group by month(exam_dt);
You have some different ways for example you can do it like this :
select max(mark),exam_month from
(select mark,month(exam_dt) as exam_month from student_mark)
where exam_month = 04 or exam_month = 05 or exam_month = 06 group by exam_month;
It is rarely interesting to look at months independent of years. It is in fact dangerous to disregard the year in the real world.
I would start with:
select year(exam_dt) as yyyy, month(exam_dt) as mm, max(mark)
from student_mark
where month(exam_dt) in (4, 5, 6)
group by year(exam_dt), month(exam_dt);
If you are only interested in 2017, then your query should be explicit:
select month(exam_dt) as mm, max(mark)
from student_mark
where exam_dte >= '2017-04-01' and exam_dt < '2017-07-01'
group by month(exam_dt);
Note that I changed the where clause to compare actual dates. This is another best practice. The use of functions on columns can impede the optimizer and make the query slower than it otherwise would be.
I have events table. I want latest event which event type='appointment' and group on type and instruction_id. Problem is event_date come first '2013-12-02' instead of '2013-12-05'. More information required give comment I explain in detail.
My Expected output :
ID INSTRUCTION_ID TYPE COMMENT EVENT_DATE
3 2 appointment at home December, 05 2013 00:00:00+0000
10 1 appointment at home November, 22 2013 00:00:00+0000
5 3 appointment office September, 17 2013 00:00:00+0000
For more information check SQL fiddle:
try below query:
select *
from
(SELECT *
FROM EVENTS
WHERE EVENTS.type='appointment'
ORDER BY EVENTS.event_date DESC) EVENTS
GROUP BY EVENTS.type,EVENTS.instruction_id;
Query 2:
select *
from
(SELECT *
FROM EVENTS
WHERE EVENTS.type='appointment'
ORDER BY EVENTS.event_date DESC) EVENTS
GROUP BY EVENTS.type;
If you want the latest of only "appointment type" grouping by event type:
SELECT max(EVENT.event_date) -- use other fields
FROM EVENTS
WHERE EVENTS.type='appointment'
GROUP BY EVENTS.instruction_id;
Because if you filter by one specific type you will only get the max date of that type. If you want to get the max date of each type
SELECT max(EVENT.event_date) -- use other fields
FROM EVENTS
GROUP BY EVENTS.type, EVENTS.instruction_id
EDIT: if you add the rest of the fields is working as you expected. Anyway, I paste you the query tested:
SELECT ID, INSTRUCTION_ID, TYPE, COMMENT, max(EVENT_DATE) EVENT_DATE
FROM EVENTS
WHERE EVENTS.type='appointment'
GROUP BY EVENTS.instruction_id
ORDER BY EVENT_DATE;