I have a table like this (plus 10 more columns) containing more than 1 million of frequently updated records:
id pid start_date end_date
1 761 2011-07-25 00:00:00 2011-08-01 00:00:00
2 761 2011-08-01 00:00:00 2011-08-22 00:00:00
3 761 2011-08-22 00:00:00 2011-09-19 00:00:00
4 802 2011-08-22 00:00:00 2011-09-19 00:00:00
5 761 2011-06-05 00:00:00 2011-07-05 00:00:00
and would like to get result for a particular pid (761 in the example below) with all consecutive intervals combined:
id pid start_date end_date
1 761 2011-07-25 00:00:00 2011-09-19 00:00:00
5 761 2011-06-05 00:00:00 2011-07-05 00:00:00
Currently I am doing this in the code, but would like to move this functionality entirely to the db side.
Any ideas how to do this?
edit: start_date and end_date columns are of DATETIME type.
This is really much better done in code. Loop over the rows, when it's for the same product, update the end date, otherwise create a new array entry.
For an idea of how complex this is in SQL, see my attempt at solving this in SQL Server :)
Related
I'm hoping the query will work both in Mysql and BigQuery
For each customer I need to find the first date they had a subscription and the first date that they stopped having any subscriptions (e.g. a break in subscription). A customer can have multiple overlapping subscriptions. Once a customer stops having access then future subscriptions are not considered.
This is a sample table with a few rows. The actual table will have millions of rows and thousands of customers.
using this sample data:
select * from test_sub order by customer_id, effect_date, expire_date;
subscription_id
customer_id
effect_date
expire_date
1
1
2022-01-01 00:00:00
2022-03-01 00:00:00
2
2
2021-01-01 00:00:00
2021-03-01 00:00:00
3
2
2021-02-01 00:00:00
2021-04-25 00:00:00
4
2
2021-05-01 00:00:00
2021-06-01 00:00:00
5
2
2021-08-01 00:00:00
2022-10-01 00:00:00
The answer should be:
customer_id
min(effect_date)
max(expire_date)
1
2022-01-01 00:00:00
2022-03-01 00:00:00
2
2021-01-01 00:00:00
2022-04-25 00:00:00
I want to create a stored procedure in MySQL, but first, I want to get the query right. However, I keep getting the problem that I can't seem to get the correct id back from my query that correspond with the DateTime stamps that I get back.
this is the table I am trying to get the result from:
id EventId start end
1 1 2019-04-05 00:00:00 2019-04-07 00:00:00
2 2 2020-04-03 00:00:00 2020-04-03 00:00:00
3 3 2020-04-02 00:00:00 2020-04-02 00:00:00
7 1 2020-06-11 00:00:00 2020-06-11 00:00:00
9 2 2020-06-18 00:00:00 2020-06-18 00:00:00
10 3 2020-06-11 00:00:00 2020-06-11 00:00:00
11 3 2020-06-07 00:00:00 2020-06-07 00:00:00
query:
SELECT DISTINCT Eventid, MIN(start), id
from date_planning
WHERE `start` >= NOW()
GROUP BY Eventid
this gives me the following result
EventId Min(start) id
1 2020-06-11 00:00:00 3
2 2020-06-18 00:00:00 9
3 2020-06-07 00:00:00 10
but these are the correct ids that belong to those DateTimes:
EventId Min(start) id
1 2020-06-11 00:00:00 7
2 2020-06-18 00:00:00 9
3 2020-06-07 00:00:00 11
You want the row with the minimum "future" date for each eventId. To solve this greatest-n-per-group problem, you need to filter rather than aggregate. Here is one option using a correlated subquery:
select dt.*
from date_planning dt
where dt.start = (
select min(dt1.start)
from date_planning dt1
where dt1.eventId = dt.eventId and dt1.start >= now()
)
For performance, you need an index on (eventId, start).
I have a sample database.
Name CheckIn CheckOut
Jake 2017-08-02 00:00:00 2017-08-05 00:00:00
Rowan 2017-08-07 00:00:00 2017-08-11 00:00:00
Xander 2017-08-08 00:00:00 2017-08-10 00:00:00
Anna 2017-08-09 00:00:00 2017-08-15 00:00:00
Nat 2017-08-11 00:00:00 2017-08-14 00:00:00
For example user search the date of 2017-08-08 to 2017-08-10. User want to search the all data that who has the date of 8, 9 and 10. Ex. Rowan choose the date of checkin in 7 and checkout in 10, So rowan had the the date of 7, 8, 9 10 and 11. This is the output that i want.
Name CheckIn CheckOut
Rowan 2017-08-07 00:00:00 2017-08-11 00:00:00
Anna 2017-08-09 00:00:00 2017-08-15 00:00:00
Xander 2017-08-08 00:00:00 2017-08-10 00:00:00
I really dont know the logic of reservation searching help me. Thanks guys.
If you want to check for overlapping intervals, then this is the way:
SELECT Name, CheckIn, CheckOut
FROM mytable
WHERE CheckIn <= '2017-08-10' AND CheckOut >= '2017-08-08';
Demo here
To better understand this you can draw a sketch depicting the search interval [2017-08-08, 2017-08-10] against the reservation interval [CheckIn, CheckOut]:
If:
CheckOut < '2017-08-08' then the reservation interval comes before the search interval
CheckIn > '2017-08-10` then the reservation interval comes after the search interval
Hence none of the above conditions must hold if we want the intervals to overlap. So we end up with the following predicates:
`CheckOut` >= '2017-08-08' AND `CheckIn` <= '2017-08-10`
try this:
SELECT * FROM test.TableName WHERE
(DATE(CheckIn) BETWEEN DATE('2017-08-08') AND DATE('2017-08-10') )
OR
(DATE(CheckOut) BETWEEN DATE('2017-08-08') AND DATE('2017-08-10') )
OR
(DATE('2017-08-08') BETWEEN DATE(CheckIn) AND DATE(CheckOut))
OR
(DATE('2017-08-10') BETWEEN DATE(CheckIn) AND DATE(CheckOut));
You need a between condition for start and end date in where clause for check_in and check_out columns as below.
SELECT *
FROM table1
WHERE check_in BETWEEN '2017-08-07' AND '2017-08-10'
OR check_out BETWEEN '2017-08-07' AND '2017-08-10'
Result
name check_in check_out
-------------------------------------------------
Rowan 07.08.2017 00:00:00 11.08.2017 00:00:00
Xander 08.08.2017 00:00:00 10.08.2017 00:00:00
Anna 09.08.2017 00:00:00 15.08.2017 00:00:00
You can check the demo here
Let's say I have a date 2013-03-01 and date 2013-04-02. How do I get a integer value between that date, for example in this case 2 days. In SQL kindly advise how to construct the query
day_date
---------------------
2005-07-29 00:00:00
2013-03-01 00:00:00
2013-04-02 00:00:00
2013-06-01 00:00:00
2013-10-19 00:00:00
2013-10-23 00:00:00
2013-12-31 00:00:00
The DATEDIFF() function returns the time between two dates.
SELECT DATEDIFF('2014-11-30','2014-11-29') AS DiffDate
I have two tables and trying to find records between start_date & end_date.
campaign table
campaign_id campaign_name start_date end_date
*********** ************* ********** ********
1 Deacon Navarro 2015-10-28 00:00:00 2015-10-31 00:00:00
2 Emily Oliver 2015-10-29 00:00:00 2015-11-04 00:00:00
statistic table
id campaign_id comments likes created_date
** *********** ******** ***** ************
1 1 14 24 2015-10-28 00:00:00
2 1 34 12 2015-10-29 00:00:00
3 1 23 12 2015-10-30 00:00:00
4 1 23 24 2015-10-31 00:00:00
5 1 21 45 2015-11-01 00:00:00
6 2 12 17 2015-10-31 00:00:00
7 2 23 12 2015-11-01 00:00:00
Now I want to find all records from statistic table where campaign_id=1 and created_date is between created_date to end_date from campaign table.
I need this output:
1 1 14 24 2015-10-28 00:00:00
2 1 34 12 2015-10-29 00:00:00
3 1 23 12 2015-10-30 00:00:00
4 1 23 24 2015-10-31 00:00:00
I have written very basic select query to find start_date & end_date from campaign table
SELECT start_date, end_date FROM campaign WHERE campaign_id = '1'
and I got this result:
start_date end_date
********** ********
2015-10-28 00:00:00 2015-10-31 00:00:00
but now I don't know how to find records from statistic table where created_date is between above start_date & end_date
Hope you are understand. I am not good with MySQL because I have just stared to learning so I need help If possible :)
Thanks.
Try this :
SELECT statistic.*
FROM statistic, campaign
WHERE
campaign.campaign_id = '1'
AND campaign.campaign_id = statistic.campaign_id
AND statistic.created_date BETWEEN campaign.start_date AND campaign.end_date
try this way
SELECT statistic.*
FROM statistic
INNER JOIN campaign ON statistic.campaign_id= campaign.campaign_id;
WHERE statistic.created_date BETWEEN campaign.start_date AND campaign.end_date
Try this :
SELECT s.*
FROM statistic AS s
INNER JOIN campaign AS c ON s.campaign_id = c.campaign_id;
WHERE c.campaign_id = 1
AND s.created_date BETWEEN c.start_date AND c.end_date
For more information about JOIN syntax here.