Using SQL joins to determine when something is available - mysql

I have some data about products and services and the dates on which they are and are not available. I want to be able to produce a list of products that are available on any specified date.
The data I have assumes that products are always available by default, but that their availability can be restricted, either by specifying that they are NOT available within certain date ranges, or that they are ONLY available within certain date ranges.
The problem I am having is with the former scenario; I can't find a way to use joins to specify that if a product is within the date range of ANY of its NOT entries, then it should not appear in the results. I can't really find the words to explain this properly, so it is probably best illustrated with a simplified example...
Product table:
ID,Name
0,Apples
1,Bananas
2,Carrots
3,Dates
4,Eggs
Restriction table:
ID,Product_ID,Type,Start,End
0,2,Only,2014-05-20,2014-05-31
1,2,Only,2014-07-01,2014-07-14
2,3,Not,2014-03-05,2014-04-04
3,3,Not,2014-04-29,2014-06-15
Examples of intended results:
Date: 2014-01-01
Products available: Apples, Bananas, Dates, Eggs
Date: 2014-04-04
Products available: Apples, Bananas, Eggs
Date: 2014-05-25
Products available: Apples, Bananas, Carrots, Eggs
Date: 2014-07-02
Products available: Apples, Bananas, Carrots, Dates, Eggs
My current attempt with a left join:
SELECT *
FROM Product LEFT JOIN Restriction ON Product.ID = Restriction.ProductID
WHERE
(
Restriction.Type = 'Only'
AND DATEDIFF(Restriction.Start, '2014-04-04') <= 0
AND DATEDIFF(Restriction.End, '2014-04-04') >= 0
)
OR
(
Restriction.Type = 'Not'
AND
(
DATEDIFF(Restriction.Start, '2014-04-04') > 0
OR
DATEDIFF(Restriction.End, '2014-04-04') < 0
)
)
OR Restriction.Type IS NULL
Output from the above query:
Product.ID,Product.Name,Restriction.Product_ID,Restriction.Type,Restriction.Start,Restriction.End
0,Apples,-,-,-,-,-
1,Bananas,-,-,-,-,-
3,Dates,3,3,Not,2014-04-29,2014-06-15
4,Eggs,-,-,-,-,-
As you can see, "Dates" still appears in the results, because although its join with restriction #2 is omitted, its join with restriction #3 is not. I can't find any way to alter the query to resolve this without breaking the logic in some other way. I do hope that I am making sense here, and that somebody can see whatever piece of insight it is to which I am currently blind.
The database software I'm using is MySQL 5.5.

First get the ones in the restriction table that is not available for now() (you can change that to any date you like)
select *
from restriction
where (type = 'Not' and now() between start and end)
or (type = 'Only' and now() not between start and end);
Now make that part a left outer join and use all rows that get a null from the join
select *
from products p
left outer join (select *
from restriction
where (type = 'Not' and now() between start and end)
or (type = 'Only' and now() not between start and end)) r
on (p.id = r.product_id)
where r.product_id is null;

Related

how can i do mysql query to find a user everytime he is logged on a table, and all the other users with him, filtered by date, and time

I am making a covid log db for easy contact tracing.
these are my tables
log_tbl (fk_UserID, fk_EstID, log_date, log_time)
est_tbl (EstID, EstName)
user_tbl (User_ID, Name, Address, MobileNumber)
I wanted to write a statement that shows when and where an individual (User_ID)
enters an Establishment (EstID),
SELECT l.*
FROM log_tbl l
WHERE (l.EstID, l.log_date) IN (SELECT l2.EstID, l2.log_date
FROM log_tbl l2
WHERE l2.User_ID = 'LIN78JFF5WG'
);
[Result of Query]1
this currently works,
but it still has to be filterd by +-2 hours based on the time the when User_ID was logged on log_tbl, so that it would narrow down result when first query would spit out 1000 logs. Because these Results will be Contacted, and to reduce Costs, it needs to be narrowed down to less than 50%.
So, table below should not include first 2 and last one because it doesn't fit with 1, the date, and 2 the time, in relation to the searched userLIN78JFF5WG
[Unfiltered Result]2
FROM log_tbl
WHERE User_ID = 'LIN78JFF5WG'
AND (BETWEEN subtime(log_tbl.log_time, '02:00:00') AND addtime(log_tbl.log_time, '02:00:00'
I know this is wrong, but I don't have any idea how to join the two queries
and result should include
EstID, Name, Address, MobileNumber, log_date, log_time sorted by Date
Imagine it like this,
There are 3 baskets full of tomatoes,
2 of the baskets have rotten tomatoes inside.
Do you throw away the whole basket full of tomatoes?
No.. you select the rotten tomato, and others close to it, and throw them away.
I need that for the DB, instead of Getting Result for the Whole Day,
I only need the People who are in close contact with The Target user.
is it possible to do this on mysql? I have to use mysql because of reasons..
Here I include the data sample fiddle:
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=050b2103d3adf5828524f49066c12e74
MySQL supports window functions with the range window frame specification. I would suggest:
select l.*
from (select l.*,
sum(case when fk_UserID = 'LIN78JFF5WG' then 1 else 0 end) over
(partition by log_date
order by log_time
range between interval 2 hour preceding and interval 2 hour following
) as cnt_user
from log_tbl l
) l
where cnt_user > 0;
Here is a db<>fiddle.
You can then annotate the results would other columns from other tables to get your final result.
This should be much faster than alternative methods.
Note, however, that you have a flaw in this logic, because it is not checking four hours between 0:00-2:00 a.m. and 22:00-0:00. You can store the date/time in a single column to make it easier to get a more accurate list.
I am not fully understand your requirements.
but I write sample sql so that we can make it clear
select *,(select UNIX_TIMESTAMP(CONCAT(log_date," ",log_time)) as ts from log_tbl where fk_UserID='LIN78JFF5WG') as target_time
from
log_tbl as l
-- simple join query.to get intend information
left join user_tbl as u on (u.User_id=l.fk_UserID)
left join est_tbl as e on (l.fk_EstID=e.EstID)
-- mysql datediff only return day as unit.so we convert to timestamp to do the diff
where UNIX_TIMESTAMP(CONCAT(l.log_date," ",l.log_time)) - target_time between 60*60*2 and 60*60*2
-- solution two
-- but I suggest you divide it into two sql like this.
select UNIX_TIMESTAMP(CONCAT(log_date," ",log_time)) as ts from log_tbl where fk_UserID='LIN78JFF5WG';
-- we get the user log timestamp.and use it in next query
select *
from
log_tbl as l
-- simple join query.to get intend information
left join user_tbl as u on (u.User_id=l.fk_UserID)
left join est_tbl as e on (l.fk_EstID=e.EstID)
-- mysql datediff only return day as unit.so we convert to timestamp to do the diff
where UNIX_TIMESTAMP(CONCAT(l.log_date," ",l.log_time)) - [target_time(passed by code)] between 60*60*2 and 60*60*2

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.

Selecting ALL Max Values SQL query

I am working with two tables in this query Table 1: admit, Table 2: Billing.
What I want to do is show people who are admitted to our crisis services (program codes '44282' and '44283'). For these people, I want to show their insurance, which is under the field guarantor_id in the Billing Table. To do this, I need to show ALL the max coverage effective dates cov_effective_date where the coverage effective date is less than the admission date preadmit_admission_date and the coverage expiration date cov_expiration_date is greater than the admission date (or Is Null). The code I have right now does everything I want, but doesn’t get all the max coverage effective dates. So if someone had two different insurances that began on the same date it will only show one and I want it show both.
Select
A.patid
,A.episode_number
,A.preadmit_admission_date
,A.program_code
,A.program_value
,A.c_date_of_birth
,A.guarantor_id
,max(A.cov_effective_date) as "MaxDate"
from(
Select
SA.patid
,SA.episode_number
,SA.preadmit_admission_date
,SA.program_code
,SA.program_value
,SA.c_date_of_birth
,BGE.guarantor_id
,BGE.cov_effective_date
From System.view_episode_summary_admit as "SA"
Left Outer Join
(Select
BG.patid
,BG.episode_number
,BG.guarantor_id
,BG.cov_effective_date
,BG.cov_expiration_date
from System.billing_guar_emp_data as "BG"
Inner Join
(Select patid, episode_number, preadmit_admission_date
from System.view_episode_summary_admit ) as "A"
On
(A.patid = BG.patid) and (A.episode_number = BG.episode_number)
Where
BG.cov_effective_date <= preadmit_admission_date and
(BG.cov_expiration_date >= preadmit_admission_date or
BG.cov_expiration_date Is Null)
) as "BGE"
on
(BGE.patid = SA.patid) and (BGE.episode_number = SA.episode_number)
Where
(program_code = '44282' or program_code = '44283' )
and preadmit_admission_date >= {?Start Date}
and preadmit_admission_date <= {?End Date}
) A
Group By Patid, Episode_number
Sorry this is such a psuedo answer.
Select (your fields)
from (your entire query)bg
left join
(select patid, max(cov_effective_date) maxdate from system.billing_guar_emp_data group by patid) maxdate
on maxdate.patid = bg.patidate
Remove the group bys for the aggregate...you can now refer to maxdate.maxdate as a field in your opening select statement. Might be a better place to join this maxdate than joined at the very end of the query (possibly right under BG in the from statement), but psuedo code right? :) Hopefully you can apply the concept, let me know I'm free (freer?) in the afternoon if you need more.

Select rows based on approximate value

I have a table with prices of some goods registered for some 
dates, such as:
2014-01-10      $120
        2014-01-11      $121
        2014-01-13      $119
        2014-01-15      $118
        I would like to create a view which would fill the missing 
prices, ie. quote the existing prices where available, and return the 
last known price if the price for a particular day is unknown, perhaps 
with a reasonable limit in case a product disappears from the market:
2014-01-10      $120
2014-01-11      $121
2014-01-12      $121
2014-01-13      $119
2014-01-14      $119
2014-01-15      $118
2014-01-16      $118
2014-01-17      $118
2014-01-18      $118
..........
2014-02-01      NULL
        If it was a mere matter of calculating the price one time, I 
would probably use a function for that, but I need later to combine 
the figures with the data from other tables with a date being a common  
field.
2014-01-10      $120    data a
    2014-01-11      $121    data b
    2014-01-12      $121    data c
    2014-01-13      $119    data d
    2014-01-14      $119    data e
    2014-01-15      $118    NULL
    2014-01-16      $118    NULL
    2014-01-17      $118    data f
    2014-01-18      $118    data g
        Normally, on 12th, 14th, and after 15th, the price field would 
return NULL, which I want to avoid, as it would ruin calculations which
use the data later on.
        If you believe that a view construction is not the best 
solution here, I'd be grateful for pointing me in the right direction 
- as you can see, the databases are not a field of my deepest 
expertise. ;)
If you have a list of all the dates that you care about, then you can use a correlated subquery to get the price:
select d.*,
(select p.price
from prices p
where p.date <= d.date
order by p.date desc
limit 1
) as price
from dates d;
This is MySQL view-compatible -- the subquery is in the select clause rather than in the from clause.

MySql retrieve products and prices

I would like to retrieve a list of all the products, with their associated prices for a given period.
The Product table:
Id
Name
Description
The Price table:
Id
Product_id
Name
Amount
Start
End
Duration
The most important thing to not here, is that a Product can have mutliple prices, even over the same period, but not with the same duration.
For example, a price from "2013-06-01 -> 2013-06-08" and another from "2013-06-01 -> 2013-06-05"
So my aim is to retrieve, for a given period, the lists of all products, paginated by 10 product for example, joined to the prices existant over the period.
The basic way to do so would be:
SElECT *
FROM product
LEFT JOIN prices ON ...
WHERE prices.start >= XXX And prices.end <= YYY
LIMIT 0,10
The problem while using this simple solution, is that I can't retrieve only 10 Products, but 10 Products*Prices, which is not acceptable in my case.
So the solution would be:
SElECT *
FROM product
LEFT JOIN prices ON ...
WHERE prices.start >= XXX And prices.end <= YYY
GROUP BY product.id
LIMIT 0,10
But the problem here is, i'll only retrieve "1" price for each product.
So I wonder what would be the best way to handle this.
I could for example use a group function, like "group_concat", and retrieve in a field all the prices in a string, like "200/300/100" and so on. That seem weird, and would need work on server-language side to transform to a readable information, but it could work.
Another solution would be to use different column for each prices, depending on duration:
SELECT
IF( NOT ISNULL(price.start) AND price.duration = 1, price.amount , NULL) AS price_1_day
---- same here for all possible durations ---
From ...
Thta would work too i guess (i'm not really sure if this is possible however), but I may need to create about 250 columns to cover all possibilities. Is that a safe option ?
Any help will be much appreciated
I believe that a group_concat would be the best way forward on this, as its very purpose is to aggregate multiple pieces of data relating to a particular column.
However, adapting on peterm's SQL fiddle, this is possible to do in 1 query if using user defined variables. (If one ignores the initial query for setting the vars)
http://dev.mysql.com/doc/refman/5.7/en/user-variables.html
SET #productTemp := '', #increment := 0;
SElECT
#increment := if(#productTemp != Product_id, #increment + 1, #increment) AS limiter,
#productTemp :=Product_id as Product_id,
Product.name,
Price.id as Price_id,
Price.start,
Price.end
FROM
Product
LEFT JOIN
Price ON Product.Id=Price.Product_id
WHERE
`start` >= '2013-05-01' AND `end` <= '2013-05-15'
GROUP BY
Price_id
HAVING
limiter <=2
What we're doing here is only incrementing the user defined var "incrementer" only when the product id is not the same as the last one that was encountered.
As aliases cannot be used in the WHERE condition, we must GROUP by the unique ID (in this case price ID) so that we can reduce the result using HAVING. In this case, I have a full result set that should include 3 Product IDs, reduced to only showing 2.
Please note: This is not a solution I would recommend on large data sets, or in a production enviornment. Even the mysql manual makes a point of highlighting that user defined vars can behave somewhat erratically depending on what paths the optimizer takes. However, I have used them to great effect for some internal statistics in the past.
Fiddle: http://sqlfiddle.com/#!2/96c92/3
It's hard to tell without sample data and desired output but you can try something like this
SElECT p.*, q2.*
FROM
(
SElECT Product_id
FROM Price
WHERE `start` >= '2013-05-01' AND `end` <= '2013-05-15'
GROUP BY Product_id
LIMIT 0,10
) q1 JOIN
(
SELECT *
FROM Price
WHERE `start` >= '2013-05-01' AND `end` <= '2013-05-15'
) q2 ON q1.Product_id = q2.Product_id JOIN product p
ON q1.Product_id = p.Id
Here is SQLFiddle demo