MySQL Calculating The Difference Between two dates but with a twist - mysql

I have 2 tables: Product, User.
Product:
Date(Datetime)
Phone_Number(Number).
User:
Date (Datetime)
Phone_Number(Number)
Type(Value of N or C).
When a new user is added to the User table, it is given a date, Phone Number and value of "N" (N for New).
When a user gets cancelled, it is given a date, the Phone Number and value of "C" (C for Cancelled).
Using MySQL, I need to find out the date difference between the Date the user was created and the date the user was cancelled and if they had a product (can match the user to the product by Phone Number) between the date of the creation and the date of the cancellation.
Any assistance would be greatly appreciated as my Query writing isn't terrible but this I can't work out.
Thanks.

You need to start by self-joining the User table to find the created and cancelled dates. Then join to the Product table to see if anything is there.
SELECT u.Phone_Number, u.Created, u.Cancelled, p.Date as 'ProductDate'
FROM (
SELECT u1.Phone_Number, u1.Date as 'Created', u2.Date as 'Cancelled'
FROM User u1
JOIN User u2 on u1.Phone_Number=u2.Phone_Number
WHERE u1.Type = 'N'
and u2.Type = 'C'
) u
LEFT JOIN Product p on u.Phone_Number = p.Phone_Number
and p.Date between u.Created and u.Cancelled

SELECT
/* Anything you want from the User table */
/* CASE statement that checks if prod.phone_number is not null which means a product was present */
FROM
User user_new
JOIN User user_can
ON user_new.phone_number = user_can.phone_number
LEFT JOIN Product prod
ON prod.phone_number = user_new.phone_number
WHERE
(prod.Date IS NULL) OR -- Either there was no record.
(prod.Date BETWEEN user_new.Date AND user_can.Date) -- or a product existed between the two dates.

It doesn't sound like your table will have the required data to determine the difference between the date the user is created and cancelled if you're only storing one date field. But if you had 2 columns, user.createdDate and user.cancelledDate you could use
SELECT DATEDIFF(user.createdDate, user.cancelledDated) AS DiffDate
I don't know if DATEDIFF works with datetime objects, I've used it with plain dates formatted like "year-mm-dd" and it will give you a Query result object with DiffDate, an integer of the number of days.
If user.cancelledDate is before user.createdDate, you will get a negative integer.

Related

Conditional SELECT SQL statements in MySQL

These 3 tables are set as follows in a MySQL database (not all columns are shown):
events_table
id
event_date
event_type // Working day or holiday
occasion // If event_type = 'holiday', occasion of holiday is entered here
classes_table
id
class_type
teacher_id (foreign key. Reference = events_table.id)
teachers_table
id
name
I want to write a SQL query wherein I want to select data depending on the data in the events_table.event_type on a particular date i.e., if the event_type on the event_date(let's say 25th Dec) is holiday then SELECT events_table.occasion... else if event_type on the event_date(let's say 28th Aug) is working_day then SELECT classes_table.id, classes_table.class_type
IF events_table.event_type <> 'holiday' THEN
-- If event on a particular day was not a holiday, select the following
SELECT
classes_table.id,
classes_table.class_type
FROM
classes_table
WHERE
teachers_table.username = 'teacher_name'
INNER JOIN teachers_table ON teachers_table.id = classes_table.teacher_id
ELSE
-- Else, select the following
SELECT
events_table.occasion
FROM
events_table
END IF
However as per this answer it is not possible. Also we cannot use IF...ELSE without BEGIN....END blocks, however doing that too doesn't help me. Is there any way we can do this in MySQL (preferably without sub-queries), if not what would be the most efficacious way of implementing the same?

MySQL - get users who placed 25th order during period

I have users and orders tables with this structure (simplified for question):
USERS
userid
registered(date)
ORDERS
id
date (order placed date)
user_id
I need to get array of users (array of userid) who placed their 25th order during specified period (for example in May 2019), date of 25th order for each user, number of days to place 25th order (difference between registration date for user and date of 25th order placed).
For example if user registered in April 2018, then placed 20 orders in 2018, and then placed 21-30th orders in Jan-May 2019 - this user should be in this array, if he placed 25th (overall for his account) order in May 2019.
How I can do this with MySQL request?
Sample data and structure: http://www.sqlfiddle.com/#!9/998358 (for testing you can get 3rd order as ex., not 25th, to not add a lot of sample data records).
One request is not required - if this can't be done in one request, few is possible and allowed.
You can use a correlated subquery to get the count of orders placed before the current one by a user. If that's 24 the current order is the 25th. Then check if the date is in the desired range.
SELECT o1.user_id,
o1.date,
datediff(o1.date, u1.registered)
FROM orders o1
INNER JOIN users u1
ON u1.userid = o1.user_id
WHERE (SELECT count(*)
FROM orders o2
WHERE o2.user_id = o1.user_id
AND o2.date < o1.date
OR o2.date = o1.date
AND o2.id < o1.id) = 24
AND o1.date >= '2019-01-01'
AND o1.date < '2019-06-01';
The basic inefficient way of doing this would be to get the user_id for every row in ORDERS where the date is in your target range AND the count of rows in ORDERS with the same user_id and a lower date is exactly 24.
This can get very ugly, very quickly, though.
If you're calling this from code you control, can't you do it from the code?
If not, there should be a way to assign to each row an index describing its rank among orders for its specific user_id, and select from this all user_id from rows with an index of 25 and a correct date. This will give you a select from select from select, but it should be much faster. The difficulty here is to control the order of the rows, so here are the selects I envision:
Select all rows, order by user_id asc, date asc, union-ed to nothing from a table made of two vars you'll initialize at 0.
from this, select all while updating a var to know if a row's user_id is the same as the last, and adding a field that will report so (so for each user_id the first line in order will have a specific value like 0 while the other rows for the same user_id will have a 1)
from this, select all plus a field that equals itself plus one in case the first added field is 1, else 0
from this, select the user_id from the rows where the second added field is 25 and the date is in range.
The union thingy is only necessary if you need to do it all in one request (you have to initialize them in a lower select than the one they're used in).
Edit: Well if you need the date too you can just select it along with the user_id, but calculating the number of days in sql will be a pain. Just join the result table to the users table and get both the date of 25th order and their date of registration, you'll surely be able to do the difference in code.
I'll try building an actual request, however if you want to truly understand what you need to make this you gotta read up on mysql variables, unions, and conditional statements.
"Looks too complicated. I am sure that this can be done with current DB structure and 1-2 requests." Well, yeah. Use the COUNT request, it will be easy, and slow as hell.
For the complex answer, see http://www.sqlfiddle.com/#!9/998358/21
Since you can use multiple requests, you can just initialize the vars first.
It isn't actually THAT complicated, you just have to understand how to concretely express what you mean by "an user's 25th command" to a SQL engine.
See http://www.sqlfiddle.com/#!9/998358/24 for the difference in days, turns out there's a method for that.
Edit 5: seems you're going with the COUNT method. I'll pray your DB is small.
Edit 6: For posterity:
The count method will take years on very large databases. Since OP didn't come back, I'm assuming his is small enough to overlook query speed. If that's not your case and let's say it's 10 years from now and the sqlfiddle links are dead; here's the two-queries solution:
SET #PREV_USR:=0;
SELECT user_id, date_ FROM (
SELECT user_id, date_, SAME_USR AS IGNORE_SMUSR,
#RANK_USR:=(CASE SAME_USR WHEN 0 THEN 1 ELSE #RANK_USR+1 END) AS RANK FROM (
SELECT orders.*, CASE WHEN #PREV_USR = user_id THEN 1 ELSE 0 END AS SAME_USR,
#PREV_USR:=user_id AS IGNORE_USR FROM
orders
ORDER BY user_id ASC, date_ ASC, id ASC
) AS DERIVED_1
) AS DERIVED_2
WHERE RANK = 25 AND YEAR(date_) = 2019 AND MONTH(date_) = 4 ;
Just change RANK = ? and the conditions to fit your needs. If you want to fully understand it, start by the innermost SELECT then work your way high; this version fuses the points 1 & 2 of my explanation.
Now sometimes you will have to use an API or something and it wont let you keep variable values in memory unless you commit it or some other restriction, and you'll need to do it in one query. To do that, you put the initialization one step lower and make it so it does not affect the higher statements. IMO the best way to do this is in a UNION with a fake table where the only row is excluded. You'll avoid the hassle of a JOIN and it's just better overall.
SELECT user_id, date_ FROM (
SELECT user_id, date_, SAME_USR AS IGNORE_SMUSR,
#RANK_USR:=(CASE SAME_USR WHEN 0 THEN 1 ELSE #RANK_USR+1 END) AS RANK FROM (
SELECT DERIVED_4.*, CASE WHEN #PREV_USR = user_id THEN 1 ELSE 0 END AS SAME_USR,
#PREV_USR:=user_id AS IGNORE_USR FROM
(SELECT * FROM orders
UNION
SELECT * FROM (
SELECT (#PREV_USR:=0) AS INIT_PREV_USR, 0 AS COL_2, 0 AS COL_3
) AS DERIVED_3
WHERE INIT_PREV_USR <> 0
) AS DERIVED_4
ORDER BY user_id ASC, date_ ASC, id ASC
) AS DERIVED_1
) AS DERIVED_2
WHERE RANK = 25 AND YEAR(date_) = 2019 AND MONTH(date_) = 4 ;
With that method, the thing to watch for is the amount and the type of columns in your basic table. Here orders' first field is an int, so I put INIT_PREV_USR in first then there are two more fields so I just add two zeroes with names and call it a day. Most types work, since the union doesn't actually do anything, but I wouldn't try this when your first field is a blob (worst comes to worst you can use a JOIN).
You'll note this is derived from a method of pagination in mysql. If you want to apply this to other engines, just check out their best pagination calls and you should be able to work thinks out.

Generating complex sql tables

I currently have an employee logging sql table that has 3 columns
fromState: String,
toState: String,
timestamp: DateTime
fromState is either In or Out. In means employee came in and Out means employee went out. Each row can only transition from In to Out or Out to In.
I'd like to generate a temporary table in sql to keep track during a given hour (hour by hour), how many employees are there in the company. Aka, resulting table has columns HourBucket, NumEmployees.
In non-SQL code I can do this by initializing the numEmployees as 0 and go through the table row by row (sorted by timestamp) and add (employee came in) or subtract (went out) to numEmployees (bucketed by timestamp hour).
I'm clueless as how to do this in SQL. Any clues?
Use a COUNT ... GROUP BY query. Can't see what you're using toState from your description though! Also, assuming you have an employeeID field.
E.g.
SELECT fromState AS 'Status', COUNT(*) AS 'Number'
FROM StaffinBuildingTable
INNER JOIN (SELECT employeeID AS 'empID', MAX(timestamp) AS 'latest' FROM StaffinBuildingTable GROUP BY employeeID) AS LastEntry ON StaffinBuildingTable.employeeID = LastEntry.empID
GROUP BY fromState
The LastEntry subquery will produce a list of employeeIDs limited to the last timestamp for each employee.
The INNER JOIN will limit the main table to just the employeeIDs that match both sides.
The outer GROUP BY produces the count.
SELECT HOUR(SBT.timestamp) AS 'Hour', SBT.fromState AS 'Status', COUNT(*) AS 'Number'
FROM StaffinBuildingTable AS SBT
INNER JOIN (
SELECT SBIJ.employeeID AS 'empID', MAX(timestamp) AS 'latest'
FROM StaffinBuildingTable AS SBIJ
WHERE DATE(SBIJ.timestamp) = CURDATE()
GROUP BY SBIJ.employeeID) AS LastEntry ON SBT.employeeID = LastEntry.empID
GROUP BY SBT.fromState, HOUR(SBT.timestamp)
Replace CURDATE() with whatever date you are interested in.
Note this is non-optimal as it calculates the HOUR twice - once for the data and once for the group.
Again you are using the INNER JOIN to limit the number of returned row, this time to the last timestamp on a given day.
To me your description of the FromState and ToState seem the wrong way round, I'd expect to doing this based on the ToState. But assuming I'm wrong on that the following should point you in the right direction:
First, I create a "Numbers" table containing 24 rows one for each hour of the day:
create table tblHours
(Number int);
insert into tblHours values
(0),(1),(2),(3),(4),(5),(6),(7),
(8),(9),(10),(11),(12),(13),(14),(15),
(16),(17),(18),(19),(20),(21),(22),(23);
Then for each date in your employee logging table, I create a row in another new table to contain your counts:
create table tblDailyHours
(
HourBucket datetime,
NumEmployees int
);
insert into tblDailyHours (HourBucket, NumEmployees)
select distinct
date_add(date(t.timeStamp), interval h.Number HOUR) as HourBucket,
0 as NumEmployees
from
tblEmployeeLogging t
CROSS JOIN tblHours h;
Then I update this table to contain all the relevant counts:
update tblDailyHours h
join
(select
h2.HourBucket,
sum(case when el.fromState = 'In' then 1 else -1 end) as cnt
from
tblDailyHours h2
join tblEmployeeLogging el on
h2.HourBucket >= el.timeStamp
group by h2.HourBucket
) cnt ON
h.HourBucket = cnt.HourBucket
set NumEmployees = cnt.cnt;
You can now retrieve the counts with
select *
from tblDailyHours
order by HourBucket;
The counts give the number on site at each of the times displayed, if you want during the hour in question, we'd need to tweak this a little.
There is a working version of this code (using not very realistic data in the logging table) here: rextester.com/DYOR23344
Original Answer (Based on a single over all count)
If you're happy to search over all rows, and want the current "head count" you can use this:
select
sum(case when t.FromState = 'In' then 1 else -1) as Heads
from
MyTable t
But if you know that there will always be no-one there at midnight, you can add a where clause to prevent it looking at more rows than it needs to:
where
date(t.timestamp) = curdate()
Again, on the assumption that the head count reaches zero at midnight, you can generalise that method to get a headcount at any time as follows:
where
date(t.timestamp) = "CENSUS DATE" AND
t.timestamp <= "CENSUS DATETIME"
Obviously you'd need to replace my quoted strings with code which returned the date and datetime of interest. If the headcount doesn't return to zero at midnight, you can achieve the same by removing the first line of the where clause.

Search Room Availability

These are my tables:
Room (roomId, roomName, roomTypeId, roomStatus)
RoomType (roomTypeId, roomTypeName, roomTypeDesc, roomTypePrice)
ReservationRoom (reservationId, roomId,checkInDate, checkOutDate, totalStay)
I receive three parameter from user (check in date, check out date, quantity)to search the available room type that can be reserve into a grid view.
This is my sql that show the available roomtype for reserve.
SELECT rt.roomTypeName, rt.roomTypePrice, count(*) as quantity
FROM Room r,
RoomType rt
WHERE roomStatus = 'Online'
AND r.roomTypeId = rt.roomTypeId
AND NOT EXISTS (SELECT 1 FROM ReservationRoom b
WHERE b.roomId = r.roomId
AND (#CheckInDate BETWEEN b.checkInDate AND b.checkOutDate
OR #CheckOutDate BETWEEN b.checkInDate AND b.checkOutDate
OR (#CheckInDate<= b.checkInDate AND #CheckOutDate >= b.checkOutDate)))
group by rt.roomTypeName, rt.roomTypePrice
Having COUNT(*) >= #quantity";
It works quite well. But it got problem when my ReservationRoom table already have a record which is checkindate (1/11/2015) and check out date (2/11/2015) made by guest A for roomtypeA.
When a guest B search for room on check in date is(2/11/2015) and check out date is(3/11/2015).it will still show the roomtypeA is not available. But it should be available because the guest A already check out on 2/11. so is there any way to let user B reserve the roomtype in this situation?
You could convert from type DATE columns to DATETIME columns, and then compare specific times of day (i.e., your specific check-out time, such as 11am).
Or, convert all times to UNIX_TIMESTAMP, and then you will have simpler greater-than / less-than comparisons altogether, since it's just an integer.
use MySQL str_to_date or cast because you must need to convert your type as DATE
cast(str_to_date('1/11/2015','%d/%m/%Y')as date)
or
cast(str_to_date(checkindate,'%d/%m/%Y')as date)
Avoid using BETWEEN as it is inclusive of the two dates - see the reference material

MySQL query one table based on sum of records or no records in another table

I'm struggling to select records from a table of locations (eg hotels) based on their availability stored in a seperate table. To avoid having lots of availability records for every possible day/location combo, the availability table only holds records for limited or no availability for a given date - so the absence of a matching record means there is FULL availabilty.
The tables are a bit like...
locations: id, name, maxRooms etc.
availability: locationID, date, roomsAvailable( an integer from zero meaning no availability to maxRooms)
...and what I need to do is to select all locations who - for a given date period - have some availability. That means they either have no matching availability records (ie fully available) or the sum of their matching availability.roomsAvailable records is greater than zero.
I'm getting a headache just trying to explain this :-( any ideas gratefully received...
SELECT locations.id FROM locations LEFT JOIN availability
ON( locations.id = availability.locationID AND
( availability.roomsAvailable > 0 OR ISNULL( availability.locationID ) ) )
You need to use a LEFT JOIN
I think you can query like this, get all locations whose sum of rooms available are greater than 0 for a specific date, then get all locations that are not listed in availability.
SELECT * FROM locations
WHERE id in
(
SELECT locationId
FROM availability
WHERE date >= '2011-05-01' and date <= '2011-07-01'
GROUP by locationId
HAVING SUM(roomsAvailable) > 0
)
OR id not in (SELECT DISTINCT locationId FROM availabilty)
Thanks for your suggestions everyone. After considering them I came up with this combined PHP/MySQL solution - which only works on MySQL 4 or later ...
SELECT * FROM locations
WHERE locations.id NOT IN (
SELECT distinct availability.locationID
FROM availability
WHERE availability.date >= '$periodStartYMD'
AND availability.date <= '$periodEndYMD'
GROUP BY locationID
HAVING sum(availability.availableRooms) = 0
AND count(availability.locationID) = $daysInPeriod)
the PHP vars should be self explanatory.
To summarise - If there are less availability records than days in the period that indicates full availability for the missing day records. If there IS a record, it indicates no or limited availability..
The sub-select statement gets all availability records matching a location which have a total availability of zero and ( the bit that was eluding me) a count of matching records totalling the number of days in the period.
So if there is an availability record for every day AND the total availability count is zero there must be no rooms available for the whole period.
Phew - hope that makes some sort of sens and thanks again...