Check hall avaiblity by date range in mysql where condition - mysql

My problem is that i want to count of id's between two dates range. But my written query is not give me required answer. sample is below .
select COUNT(hall_id) as countids
from comm_hall_form
where hall_id ='13' and ((date(booked_from)
between '2019-11-08' and '2019-11-09') or (date(booked_to)
between '2019-11-08' and '2019-11-09'))

You need to check if the booked time fully or partially covers the period you are asking for. Right now you are only checking if it starts or ends with that period.
SELECT COUNT(id) as countids
FROM comm_hall_form
WHERE hall_id ='13'
(DATE(booked_from) <= '2019-11-09' AND DATE(booked_to) >= '2019-11-09')
OR
(DATE(booked_from) <= '2019-11-08' AND DATE(booked_to) >= '2019-11-08')

Related

What's the difference between the two SQL statements?

This is a question from leetcode, using the second query I got the question wrong but could not identify why
SELECT
user_id,
max(time_stamp) as "last_stamp"
from
logins
where
year(time_stamp) = '2020'
group by
user_id
and
select
user_id,
max(time_stamp) as "last_stamp"
from
logins
where
time_stamp between '2020-01-01' and '2020-12-31'
group by
user_id
The first query uses a function on every row to extract the year (an integer) and compares that to a string. (It would be preferable to use an integer instead.) Whilst this may be sub-optimal, this query would accurately locate all rows that fall into the year 2020.
The second query could fail to locate all rows that fall into 2020. Here it is important to remember that days have a 24 hour duration, and that each day starts at midnight and concludes at midnight 24 hours later. That is; a day does have a start point (midnight) and an end-point (midnight+24 hours).
However a single date used in SQL code cannot be both the start-point and the end-point of the same day, so every date in SQL represents only the start-point. Also note here, that between does NOT magically change the second given date into "the end of that day" - it simply cannot (and does not) do that.
So, when you use time_stamp between '2020-01-01' and '2020-12-31' you need to think of it as meaning "from the start of 2020-01-01 up to and including the start of 2020-12-31". Hence, this excludes the 24 hours duration of 2020-12-31.
The safest way to deal with this is to NOT use between at all, instead write just a few characters more code which will be accurate regardless of the time precision used by any date/datetime/timestamp column:
where
time_stamp >= '2020-01-01' and time_stamp <'2021-01-01'
with the second date being "the start-point of the next day"
See answer to SQL "between" not inclusive

Having Problem fetching rows from mysql table between two date columns

I am having the table and i want to fetch rows based on 'rb_rm_id' column between the 2 dates 'rb_chkin' and 'chkout'. But, It gives me 0 result even if the values are present. Please help me. Thanks in Advance.
I have Tried the Following Mysql Query but, failed:
SELECT *
FROM room_booking
WHERE `rb_rm_id` = 2 AND
(`rb_chkin` >= '2019-08-05' AND `rb_chkin` <= '2019-08-06') AND
(`rb_chkout` >= '2019-08-05' AND `rb_chkout` <= '2019-08-06');
The Table image is here [1]: https://imgur.com/eQZ1I0C "Table"
Question is not clear and also your mysql table doesn't contains any value in that range. There is no check in value in your given range.
try this one
SELECT *
FROM room_booking
WHERE `rb_rm_id` = 2 AND
(`rb_chkin` <= '2019-08-05' AND `rb_chkout` >= '2019-08-06');
Your query says:
Give me any rows where rb_chkin and rb_chkout both are between 2019-08-05 and 2019-08-06 and the rb_rm_id is 2.
There is no such row in your table.
What you probably want is to detect whether a room is booked during the given date range. For a room to be, at least partially, occupied there can three case:
The check-in date falls within the date range.
The check-out date falls within the date range.
The check-in date is before the date range, and the check-out is after the date range.
If either one of these three conditions is met the room has a booking during the given date range. The equivalent query would be:
SELECT *
FROM room_booking
WHERE `rb_rm_id` = 2 AND
(`rb_chkin` BETWEEN '2019-08-05' AND '2019-08-06') OR
(`rb_chkout` BETWEEN '2019-08-05' AND '2019-08-06') OR
(`rb_chkin` < '2019-08-05' AND `rb_chkout` > '2019-08-06');
So this returns all rows that have a booking for the given room in the given date range.
Please consider using better column names, which would make your code actually readable. Something like: rb_room_id, rb_customer_id, rb_check_in_date, rb_check_out_date. I really don't understand this need for obfuscation.

How would you query a cumulative sum over x amount of days?

I'm trying to find the cumulative sum of sessions of a link for its first 3 days. I tried this but it doesn't seem to take the date clause into account:
select
date,
link,
sum(sessions) as sessions
from ga
where date <= date+interval 3 day
group by link
But if I manually enter a date, it seems to work. Why is it not seeing date+interval 3 day as a proper date...?
Any help would be greatly appreciated! :)
Date is a column, not a value, you need to provide a specific date entry. Also "between" is a better keyword to use in this situation.
You need to also add date column in GROUP BY clause. Also, avoid using column name as date. It will create confusion.
Try below query :
select date_column,
link,
sum(sessions) as sessions
from ga
where date_column BETWEEN CURDATE()-3 AND CURDATE()
group by link, date_column

mysql filter by date and time separately

I have to perform a query on a MySQL database.
I have a table with records, have a column called "date" (the date type), and a column called "time" (type. Integer is stored by multiplying the time of day by 60. eg 8 am is stored as 480).
Unfortunately, the format of this table can not be modified.
My table stores attentions of doctors on call. The doctors on duty working in two shifts: from 8-20, and 20-8.
I need to know the amount of attention for every doctor.
My query must be filtered by date range and shift.
The problem is that, in the case of doctors working at the turn of 20-8, I have to consider a change of day. (sorry for my bad English).
What I have done is this, this would be an example to date of yesterday, and doctors shift 20-8.
SELECT * FROM attentions WHERE (date >= '2015-07-23' and time >=1200) and (date <= '2015-07-24' and time <480)
the query does not work at all.
Supposing the date field is called: 'a_date' with format 'yyyy-mm-ss' and the time field is a number, the query should be:
SELECT * FROM attentions WHERE (date(a_date) >= '2015-07-23' and time >=1200) and (date(a_date) <= '2015-07-24' and time <480)
Can you check using between?
SELECT * FROM attentions WHERE date between '2015-07-23' and '2015-07-24' and time between 1200 and 480
I think you can also use this -
SELECT * FROM ***** where CREATED_DATETIME between '2015-03-12 00:00:00' and '2015-05-11 00:00:00';

Select leave data from attendance table given the following condition

I have attendance data for employees stored in the table attendance with the following column names:
emp_id (employee ID)
date
type (leave, absent, etc.)
(there are others but I'm omitting them for the sake of simplicity)
My objective is to retrieve all dates of the given month on which the employee was on leave (type = 'Leave') and the last leave taken in the last month, if any.
It's easy to do it using two queries (I'm using PHP to get process the data), but is there any way this can be done in a single query?
I'm answering my own question so as to close it. As #bpgergo pointed out in the comments, UNION will do the trick here.
SELECT * FROM table_name
WHERE type="Leave" AND
date <= (CURRENT_DATE() - 30)
Select the fields, etc you want then se a combined where clause using mysql's CURRENT_DATE() function. I subtracted 30 for 30 days in a month.
If date is a date column, this will return everyone who left 1 month or longer ago.
Edit:
If you want a specific date, change the 2nd month like this:
date <= (date_number - 30)