Is there a clever way to the do the following? - mysql

Is ther any clear way of doing the following in one SQL (PL) code? :)
What I have in the tables is the following:
CONTACT_TBL (HEADER)
CONTRACT_ID
BEGIN_DATE
END_DATE
TOT_AMOUNT
123
13-MAY-16
12-MAY-34
100
456
13-JAN-14
12-DEC-25
300
789
13-SEP-14
12-OCT-34
700
CONTRACT_ACTIVTY (DETAIL)
CONTRACT_ID
SEQNUM
END_DATE
AMOUNT
COMMENTS
123
1
12-MAY-19
25
25 - Initial Expiration
123
2
12-MAY-24
25
25
123
3
12-MAY-29
25
25
123
4
12-MAY-34
25
25 - End of Contract
What we need is to populate for each row:
BEGIN_DATE
For SEQNUM #1 - BEGIN_DATE is always CONTACT_TBL.BEGIN_DATE (in this case 13-MAY-15, a 3 year span)
For SEQNUM #2 to N - BEGIN_DATE is 1 day more than previous row's END_DATE (in this case 12-MAY-19 + 1 = 13-MAY-19, all 5 year spans)
Mark the row 'Active' if the SYSDATE is between BEGIN_DATE and END_DATE (in this case it's Row #2)
CONTRACT_ID
SEQNUM
BEGIN_DATE
END_DATE
AMOUNT
STATUS
123
1
13-MAY-16
12-MAY-19
25
123
2
13-MAY-19
12-MAY-24
25
Active
123
3
13-MAY-24
12-MAY-29
25
123
4
13-MAY-29
12-MAY-34
25

N.B. This answer assumes the db is Oracle
This can be done by using LAG to find the date from the previous row.
LAG can accept 3 parameters, the first of which is the column whose value you want to find from the previous row, the second is how many rows to go back each time (default is 1), and the third is what to display if there is no previous row found.
In your case, you want to default to the begin_date from the contact_tbl if there is no previous row to the first row in the contract_activity table, so you need to join the two tables as well, like so:
WITH contact_tbl AS (SELECT 123 contract_id, to_date('13/05/2016', 'dd/mm/yyyy') begin_date, to_date('12/05/2034', 'dd/mm/yyyy') end_date, 100 tot_amount FROM dual UNION ALL
SELECT 456 contract_id, to_date('13/05/2016', 'dd/mm/yyyy') begin_date, to_date('12/05/2034', 'dd/mm/yyyy') end_date, 100 tot_amount FROM dual UNION ALL
SELECT 789 contract_id, to_date('13/05/2016', 'dd/mm/yyyy') begin_date, to_date('12/05/2034', 'dd/mm/yyyy') end_date, 100 tot_amount FROM dual),
contract_activity AS (SELECT 123 contract_id, 1 seqnum, to_date('12/05/2019', 'dd/mm/yyyy') end_date, 25 amount, '25 - Initial Expiration' comments FROM dual UNION ALL
SELECT 123 contract_id, 2 seqnum, to_date('12/05/2024', 'dd/mm/yyyy') end_date, 25 amount, '25' comments FROM dual UNION ALL
SELECT 123 contract_id, 3 seqnum, to_date('12/05/2029', 'dd/mm/yyyy') end_date, 25 amount, '25' comments FROM dual UNION ALL
SELECT 123 contract_id, 4 seqnum, to_date('12/05/2034', 'dd/mm/yyyy') end_date, 25 amount, '25 - End of Contract' comments FROM dual),
-- end of creating subqueries to mimic your tables
contract_details AS (SELECT ca.contract_id,
ca.seqnum,
LAG(ca.end_date + 1, 1, c.begin_date) OVER (PARTITION BY ca.contract_id ORDER BY seqnum) begin_date,
ca.end_date,
ca.amount
FROM contact_tbl c
INNER JOIN contract_activity ca ON c.contract_id = ca.contract_id)
SELECT contract_id,
seqnum,
begin_date,
end_date,
amount,
CASE WHEN TRUNC(SYSDATE) BETWEEN begin_date AND end_date THEN 'Active' END status
FROM contract_details;
CONTRACT_ID SEQNUM BEGIN_DATE END_DATE AMOUNT STATUS
----------- ---------- ----------- ----------- ---------- ------
123 1 13/05/2016 12/05/2019 25
123 2 13/05/2019 12/05/2024 25 Active
123 3 13/05/2024 12/05/2029 25
123 4 13/05/2029 12/05/2034 25
Your status column is a simple case expression, but since it refers to the calculated begin_date, rather than repeating the calculation, I did the begin_date calculation in one subquery, and then referenced that in the outer query.
Note how I used TRUNC(sysdate) instead of just sysdate in the case expression for the status - DATEs in Oracle have a time component (which defaults to midnight if you don't specify the time), so if you ran the query any time between 12/05/2024 00:00:01 and 12/05/2024 23:59:59, your query wouldn't return a status of Active for the second row if you used sysdate. You need to explicitly truncate sysdate to midnight in order for the comparison to work.

Hope below snippet serve the purpose.
DECLARE
CURSOR cur_contracts IS
SELECT
*
FROM
CONTACT_TBL
ORDER BY
CONTRACT_ID;
CURSOR cur_contracts_act(contact_id NUMBER) IS
SELECT
*
FROM
CONTRACT_ACTIVTY
ORDER BY
CONTRACT_ID,
SEQNUM;
l_ca_begin_date DATE;
l_ca_status VARCHAR2(10);
l_prev_rec_end_date DATE;
BEGIN
FOR i IN cur_contracts
LOOP
contract_activity_seq := 0;
FOR j IN cur_contracts_act (i.CONTRACT_ID)
LOOP
contract_activity_seq := contract_activity_seq + 1;
IF (contract_activity_seq = 1) THEN
l_ca_begin_date := i.begin_date;
ELSE
l_ca_begin_date := l_prev_rec_end_date + 1;
END IF;
IF SYSDATE BETWEEN l_ca_begin_date AND j.end_date THEN
l_ca_status := 'Active';
ELSE
l_ca_status := NULL;
END IF;
l_prev_rec_end_date := j.end_date;
UPDATE CONTRACT_ACTIVTY
SET BEGIN_DATE = l_ca_begin_date,
status = l_ca_status
WHERE CURRENT OF cur_contracts_act;
END LOOP;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN raise_application_error(
-20001,
'An error was encountered - ' || SQLCODE || ' -ERROR- ' || SQLERRM
);
END;
/

Related

A query for getting results separated by a date gap

ID
TIMESTAMP
1
2020-01-01 12:00:00
2
2020-02-01 12:00:00
3
2020-05-01 12:00:00
4
2020-06-01 12:00:00
5
2020-07-01 12:00:00
I am looking for a way to get records in a MySQL database that are within a certain range of each other. In the above example, notice that there is a month between the first two records, then a three month gap, before we see another three records with a month between.
What is a way to group these into two result sets, so I will get Ids 1, 2 and 3, 4, 5 A solution using days would be probably work the best as thats easier to modify.
You can use lag() and then logic to see where a gap is big enough to start a new set of records. A cumulative sum gives you the groups you want:
select t.*,
sum(case when prev_timestamp >= timestamp - interval 1 month then 0 else 1 end) over (order by timestamp) as grouping
from (select t.*,
lag(timestamp) over (order by timestamp) as prev_timestamp
from t
) t;
If you want to summarize this with a start and end date:
select min(timestamp), max(timestamp)
from (select t.*,
sum(case when prev_timestamp >= timestamp - interval 1 month then 0 else 1 end) over (order by timestamp) as grouping
from (select t.*,
lag(timestamp) over (order by timestamp) as prev_timestamp
from t
) t
) t
group by grouping;
For example, the following query:
select group_concat(ID)
from (
select w1.ID,w1.TS,w2.ID flag
from work1 w1 left outer join work1 w2
on timestampdiff(month,w2.TS,w1.TS)=1
order by w1.ID
) w
group by
case when flag is null then #str:=ID else #str end
See db fiddle

how to sum of multiple count on different table and different filter

i want to get box_id, date , hour and sum of multiple count id in different table with different status in each table but having same box_id,
example
table 1
(filter by status = finished)
id box_id date status
i 20 2019-01-01 01:00:00.000 UTC finished
2 21 2019-01-01 02:00:00.000 UTC finished
3 21 2019-01-01 01:00:00.000 UTC unfinished
table 2
(filter by status = start)
id box_id date status
i 21 2019-01-01 01:00:00.000 UTC start
2 22 2019-01-01 02:00:00.000 UTC end
3 23 2019-01-01 01:00:00.000 UTC start
4 24 2019-01-01 01:00:00.000 UTC start
table 3
(filter by status = close)
id box_id date status
i 21 2019-01-01 03:00:00.000 UTC close
2 22 2019-01-01 02:00:00.000 UTC end
3 24 2019-01-01 01:00:00.000 UTC close
result that i want:
box_id date hour count
20 2019-01-01 1 1
21 2019-01-01 1 1
21 2019-01-01 2 1
21 2019-01-01 3 1
23 2019-01-01 1 1
24 2019-01-01 1 2
this is my query that works for table 1:
how i get for all in 1 table ?
select box_id,
date(date_update),
EXTRACT(hour FROM date_update) as hourly,
count(id)
from table1
where status = "finished"
group by box_id, date(date_update), EXTRACT(hour FROM date_update)
format hour = 0 - 23
Assuming your date field is of TIMESTAMP data type - below is for BigQuery Standard SQL
#standardSQL
SELECT box_id, date, hour, COUNT(1) cnt
FROM (
SELECT box_id, DATE(date) date, EXTRACT(HOUR FROM date) hour
FROM `project.dataset.table1` WHERE status = 'finished' UNION ALL
SELECT box_id, DATE(date) date, EXTRACT(HOUR FROM date) hour
FROM `project.dataset.table2` WHERE status = 'start' UNION ALL
SELECT box_id, DATE(date) date, EXTRACT(HOUR FROM date) hour
FROM `project.dataset.table3` WHERE status = 'close'
)
GROUP BY box_id, date, hour
You can test, play with above using sample/dummy data from your question as in below example
#standardSQL
WITH `project.dataset.table1` AS (
SELECT 1 id, 20 box_id, TIMESTAMP '2019-01-01 01:00:00.000 UTC'date, 'finished' status UNION ALL
SELECT 2, 21, '2019-01-01 02:00:00.000 UTC', 'finished' UNION ALL
SELECT 3, 21, '2019-01-01 01:00:00.000 UTC', 'unfinished'
), `project.dataset.table2` AS (
SELECT 1 id, 21 box_id, TIMESTAMP '2019-01-01 01:00:00.000 UTC' date, 'start' status UNION ALL
SELECT 2, 22, '2019-01-01 02:00:00.000 UTC', 'end' UNION ALL
SELECT 3, 23, '2019-01-01 01:00:00.000 UTC', 'start' UNION ALL
SELECT 4, 24, '2019-01-01 01:00:00.000 UTC', 'start'
), `project.dataset.table3` AS (
SELECT 1 id, 21 box_id, TIMESTAMP '2019-01-01 03:00:00.000 UTC' date, 'close' status UNION ALL
SELECT 2, 22, '2019-01-01 02:00:00.000 UTC', 'end' UNION ALL
SELECT 3, 24, '2019-01-01 01:00:00.000 UTC', 'close'
)
SELECT box_id, date, hour, COUNT(1) cnt
FROM (
SELECT box_id, DATE(date) date, EXTRACT(HOUR FROM date) hour
FROM `project.dataset.table1` WHERE status = 'finished' UNION ALL
SELECT box_id, DATE(date) date, EXTRACT(HOUR FROM date) hour
FROM `project.dataset.table2` WHERE status = 'start' UNION ALL
SELECT box_id, DATE(date) date, EXTRACT(HOUR FROM date) hour
FROM `project.dataset.table3` WHERE status = 'close'
)
GROUP BY box_id, date, hour
-- ORDER BY box_id, date, hour
with result
Row box_id date hour cnt
1 20 2019-01-01 1 1
2 21 2019-01-01 1 1
3 21 2019-01-01 2 1
4 21 2019-01-01 3 1
5 23 2019-01-01 1 1
6 24 2019-01-01 1 2
Below are slightly refactored versions of the same (with same output obviously)
#standardSQL
SELECT box_id, DATE(date) date, EXTRACT(HOUR FROM date) hour,
COUNTIF(
(t = 1 AND status = 'finished') OR
(t = 2 AND status = 'start') OR
(t = 3 AND status = 'close')
) cnt
FROM (
SELECT 1 t, * FROM `project.dataset.table1` UNION ALL
SELECT 2, * FROM `project.dataset.table2` UNION ALL
SELECT 3, * FROM `project.dataset.table3`
)
GROUP BY box_id, date, hour
HAVING cnt > 0
OR
#standardSQL
SELECT box_id, DATE(date) date, EXTRACT(HOUR FROM date) hour, COUNT(1) cnt
FROM (
SELECT * FROM `project.dataset.table1` WHERE status = 'finished' UNION ALL
SELECT * FROM `project.dataset.table2` WHERE status = 'start' UNION ALL
SELECT * FROM `project.dataset.table3` WHERE status = 'close'
)
GROUP BY box_id, date, hour
As discussed in the comments since you want to add fields from multiple tables I recommend you use a JOIN. There are multiple JOIN types, in case all the table have the same amount and values for box_id, you can use INNER JOIN. However, if that is not the case and you still want to see the count for each box_id, even though this box_id might not be present in all the three tables I suggest you to use FULL JOIN.
Below I have written a simplified example where I used FULL JOIN, together with other built in functions in BigQuery.
SELECT DISTINCT
coalesce(t1.box_id, t2.box_id, t3.box_id) AS id,
(ifnull(t1.count,0)+ifnull(t2.count,0)+ifnull(t3.count,0)) AS count
FROM (
SELECT
box_id,
count(box_id) AS count
FROM
`source_table1`
WHERE status = 'finished'
GROUP BY
box_id) t1
FULL JOIN (
SELECT
box_id,
count (box_id) AS count
FROM
`source_table2`
WHERE status = 'finished'
GROUP BY
box_id ) t2
ON
t1.box_id=t2.box_id
FULL JOIN (
SELECT
box_id,
count (box_id) AS count
FROM
`source_table3`
WHERE status = 'finished'
GROUP BY
box_id) AS t3
ON
t1.box_id=t3.box_id
WHERE
t1.box_id IS NOT NULL
OR t2.box_id IS NOT NULL
OR t3.box_id IS NOT NULL
ORDER BY
id
Notice that I used COALESCE to select the box_id, in case this field does not exist in table 1 it will move to table 2 and so on. Subsequently, I have used IFNULL to sum the result of the count encountered in each table, this method was used to make sure the count is set to zero when box_id is not in the table. Finally, I used the WHERE clause, so the count would respect the condition you imposed.
I used the sample data beneath to reproduce your case:
Table 1:
Table 2 and Table 3:
Hence, the output was:
I hope it helps.

SQL query to extract the most recent part and anything that is 30 days older than that date.

I am very new to SQL and I need to write a query that selects data for a specific part. However, It should select only the part that is the most recent(given by date) and anything that is only 30 days prior to it. Please consider the table below:
PartID | Part_NAME | DATE
-----------------------------
1 AAA 6/16/2015
2 BBB 6/15/2015
3 AAA 6/11/2015
4 AAA 1/1/2008
I need a query that gives me:
PartID | Part_NAME | DATE
-----------------------------
1 AAA 6/16/2015
3 AAA 6/11/2015
I have tried:
select * from ( select * from sales_table where Part_NAME = 'AAA') where DATE BETWEEN (max(DATE) and (max(DATE)-30))
I have read some articles saying that I cannot use WHERE and functions like max() together and advised me to use group by or having but it didn't work for me as well. Thank you.
IF you want data from the last 30 days of the current day, you can do :
SELECT *
FROM sales_table
WHERE
[DATE] >= DATEADD(DAY, -30,GETDATE())
AND [DATE] <= GETDATE()
AND Part_NAME = 'AAA'
IF you want data from the last 30 days from the last date of sale of each Part_NAME (this will take the max recorded date of sale for each Part_NAME and get the last 30 days records of each one of them.)
SELECT *
FROM (
SELECT *,
MAX([DATE]) OVER(PARTITION BY Part_NAME ORDER BY PartID) AS RecentDate
FROM sales_table
) D
WHERE
[DATE] >= DATEADD(DAY, -30, RecentDate)
AND [DATE] <= RecentDate
AND Part_NAME = 'AAA'
You can accomplish by using datediff and getdate() and a subquery.
SELECT * FROM (
SELECT *,DATEDIFF(DD,[DATE],GETDATE()) AS DAYSBETWEEN FROM sales_table
) AS X
WHERE DAYSBETWEEN <= 30
If you want data from the last 30 days, it would be:
select st.*
from sales_table st join
(select top (1) st2.*
from sales_table st2
order by st2.date desc
) st2
on st2.part_name = st.part_name and
st.date >= dateadd(day, -30, cast(getdate() as date));

interval by 4 using sql - Mysql

I've a table and i want that data is interval by 4 or, when i'm using modulo the record is not that i expected, PFB `
SELECT (DATE_FORMAT(subscribed_from, '%Y-%m')) AS date_ FROM subscription
WHERE operator = 'tim'
AND DATE_FORMAT(subscribed_from, '%Y-%m-%d') BETWEEN '2013-01-01' AND '2014-12-31'
GROUP BY (DATE_FORMAT(subscribed_from, '%Y-%m'));
it will show record like this
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
i want take only data interval by 4, this below is record that i expected.
2013-01
2013-05
2013-09
2014-02
and also for interval by 2, this below record is that i expected
2013-01
2013-03
2013-05
2013-07
2013-09
if i using modulo % 2 it will start from 2013-01 and jump by 2, but the problem if the where range i want to start from 2013-02, 02 it self not showing on the result. so if the where clause the month start from 2 it will given the interval such as 2,4,6,8,10,12
SELECT date_, SUM(the_metric_you_want_to_aggregate)
FROM (
SELECT 4*FLOOR(
(DATE_FORMAT(subscribed_from, '%Y%m') - 201301)
/4) AS date_,
the_metric_you_want_to_aggregate
FROM subscription
WHERE operator = 'tim'
AND subscribed_from BETWEEN 20130101000000 AND 201412315959
) AS ilv
GROUP BY date_
(where 201301 is the year/month start of the range you are selecting by - assuming that is the reference for the 4-month aggregation)
Note that enclosing column references in functions (...DATE_FORMAT(subscribed_from, '%Y-%m-%d') BETWEEN...) prevents the use of indexes.
You have to use variables. Here is sample for interval by 4.
SET #row_number:=0;
SELECT date_ from (
SELECT (DATE_FORMAT(subscribed_from, '%Y-%m')) AS date_,#row_number:=#row_number+1 FROM subscription
WHERE operator = 'tim' AND DATE_FORMAT(subscribed_from, '%Y-%m-%d') BETWEEN '2013-01-01' AND '2014-12-31'
GROUP BY (DATE_FORMAT(subscribed_from, '%Y-%m'))
) as tbl where #row_number % 4=0;
let says i'm using this method to generate the intevals, but i want the start number is from my input, let says it start from 4 and if the condition put %4 should be the output is 4, 8 ,12 ....
enter code here
SET #row:=0;
SELECT *
FROM (
SELECT
#row := #row +1 AS rownum
FROM (
SELECT #row) r, subscription
) ranked
WHERE rownum %4 = 1

Mysql : Finding empty time blocks between two dates and times?

I wanted to find out user's availability from database table:
primary id | UserId | startdate | enddate
1 | 42 | 2014-05-18 09:00 | 2014-05-18 10:00
2 | 42 | 2014-05-18 11:00 | 2014-05-18 12:00
3 | 42 | 2014-05-18 14:00 | 2014-05-18 16:00
4 | 42 | 2014-05-18 18:00 | 2014-05-18 19:00
Let's consider above inserted data is user's busy time, I want to find out free time gap blocks from table between start time and end time.
BETWEEN 2014-05-18 11:00 AND 2014-05-18 19:00;
Let me add here schema of table for avoiding confusion:
Create Table availability (
pid int not null,
userId int not null,
StartDate datetime,
EndDate datetime
);
Insert Into availability values
(1, 42, '2013-10-18 09:00', '2013-10-18 10:00'),
(2, 42, '2013-10-18 11:00', '2013-10-18 12:00'),
(3, 42, '2013-10-18 14:00', '2013-11-18 16:00'),
(4, 42, '2013-10-18 18:00', '2013-11-18 19:00');
REQUIREMENT:
I wanted to find out free gap records like:
'2013-10-27 10:00' to '2013-10-28 11:00' - User is available for 1 hours and
'2013-10-27 12:00' to '2013-10-28 14:00' - User is available for 2 hours and
available start time is '2013-10-27 10:00' and '2013-10-27 12:00' respectively.
Here you go
SELECT t1.userId,
t1.enddate, MIN(t2.startdate),
MIN(TIMESTAMPDIFF(HOUR, t1.enddate, t2.startdate))
FROM user t1
JOIN user t2 ON t1.UserId=t2.UserId
AND t2.startdate > t1.enddate AND t2.pid > t1.pid
WHERE
t1.endDate >= '2013-10-18 09:00'
AND t2.startDate <= '2013-11-18 19:00'
GROUP BY t1.UserId, t1.endDate
http://sqlfiddle.com/#!2/50d693/1
Using your data, the easiest way is to list the hours when someone is free. The following gets a list of hours when someone is available:
select (StartTime + interval n.n hour) as FreeHour
from (select cast('2014-05-18 11:00' as datetime) as StartTime,
cast('2014-05-18 19:00' as datetime) as EndTime
) var join
(select 0 as n union all select 1 union all select 2 union all select 3 union all select 4 union all
select 5 union all select 6 union all select 7 union all select 8 union all select 9
) n
on StartTime + interval n.n hour <= EndTime
where not exists (select 1
from availability a
where StartTime + interval n.n hour < a.EndDate and
StartTime + interval n.n hour >= a.StartDate
);
EDIT:
The general solution to your problem requires denormalizing the data. The basic query is:
select thedate, sum(isstart) as isstart, #cum := #cum + sum(isstart) as cum
from ((select StartDate as thedate, 1 as isstart
from availability a
where userid = 42
) union all
(select EndDate as thedate, -1 as isstart
from availability a
where userid = 42
) union all
(select cast('2014-05-18 11:00' as datetime), 0 as isstart
) union all
(select cast('2014-05-18 19:00' as datetime), 0 as isstart
)
) t
group by thedate cross join
(select #cum := 0) var
order by thedate
You then just choose the values where cum = 0. The challenge is getting the next date from this list. In MySQL that is a real pain, because you cannot use a CTE or view or window function, so you have to repeat the query. This is why I think the first approach is probably better for your situation.
The core query can be this. You can dress it up as you like, but I'd handle all that stuff in the presentation layer...
SELECT a.enddate 'Available From'
, MIN(b.startdate) 'To'
FROM user a
JOIN user b
ON b.userid = a.user
AND b.startdate > a.enddate
GROUP
BY a.enddate
HAVING a.enddate < MIN(b.startdate)
For times outside the 'booked' range, you have to extend this a little with a UNION, or again handle the logic at the application level