I have a R script that I need to migrate into SQL. In R I can use the tidyr::complete to fill in the gaps no worries. And when I know I can do it in SQL with the cross join function. the problem arises when I want to maintain some of the nulls that exist.
I reduced the results here for simplicity but the sequence would go
if month1 = 2012-01-01 then month2 would sequence every month from 2012-01-01 until today month
if month1 = 2020 then month2 would sequence from 2020-01-01 to 2022-11-01
month1
month2
units
2012-01-01
null
10
2012-01-01
2012-01-01
null
2012-01-01
2012-03-01
15
2012-01-01
2013-01-01
12
2012-01-01
2013-04-01
17
2012-01-01
2013-05-05
5
Ideally I would get
month1
month2
units
2012-01-01
null
10
2012-01-01
2012-01-01
null
2012-01-01
2012-02-01
null
2012-01-01
2012-03-01
15
2012-01-01
2013-01-01
12
2012-01-01
2013-02-01
null
2012-01-01
2013-03-01
null
2012-01-01
2013-04-01
17
2012-01-01
2013-05-05
5
but in SQL I am getting
month1
month2
units
2012-01-01
2012-01-01
null
2012-01-01
2012-02-01
null
2012-01-01
2012-03-01
15
2012-01-01
2013-01-01
12
2012-01-01
2013-02-01
null
2012-01-01
2013-03-01
null
2012-01-01
2013-04-01
17
2012-01-01
2013-05-05
5
SELECT
seq.*
, COALESCE(m.n, 0) AS termed
FROM (
SELECT *
FROM (
SELECT DISTINCT month1
FROM table1
WHERE submit_dt IS NOT NULL
) sign
CROSS JOIN (
SELECT DISTINCT month2
FROM table1
WHERE submit_dt IS NOT NULL
)
WHERE month2 >= mmonth1 and month2 is not null
) seq
LEFT JOIN (
SELECT
t2.month1,
, t2.month2
, COUNT(*) AS units
FROM table2 t2
GROUP BY 1,2
) u ON u.month1 = seq.month1 AND u.month2 = seq.month2
Here is the R version that gives me what I want
df
<- (month1, month2, units) %>%
group_by(month1) %>%
tidyr::complete(month2 = seq.Date(as.Date("2012-01-01"), floor_date(today(), "month"), "month"))
Any help is appreciated. I know it is something super simple I am missing. Thanks
So you want the gaps filled for month year 2012 from 2012-01-01 - currentdate
and the same goes for every year in month1
the foolowing extract first all unique years in the month1 generates all dates from the extract year till now and joins it to the orignal table
CREATE TABLE tab1
("month1" timestamp, "month2" timestamp, "units" varchar(4))
;
INSERT INTO tab1
("month1", "month2", "units")
VALUES
('2012-01-01', NULL, '10'),
('2012-01-01', '2012-01-01', NULL),
('2012-01-01', '2012-03-01', '15'),
('2012-01-01', '2013-01-01', '12'),
('2012-01-01', '2013-04-01', '17'),
('2012-01-01', '2013-05-05', '5'),
('2020-01-01', '2020-01-01', '5')
;
CREATE TABLE
INSERT 0 7
SELECT s.date1
,COALESCE(t."month2",s.date2) as month2
,t."units"
FROM (
SELECT (t._year || '-01-01') ::date as date1, generate_series( (t._year || '-01-01') ::date
, (date_part('year', CURRENT_DATE) || '-12-01') ::date
, interval '1 month'
)::date AS date2
FROM (SELECT DISTINCT
extract(year from "month1") _year FROM tab1) t
) s
LEFT JOIN tab1 t
ON date_trunc('month',t."month1"::date)::date = s.date1
AND date_trunc('month',t."month2"::date)::date = s.date2
ORDER BY 1,2;
date1
month2
units
2012-01-01
2012-01-01 00:00:00
null
2012-01-01
2012-02-01 00:00:00
null
2012-01-01
2012-03-01 00:00:00
15
2012-01-01
2012-04-01 00:00:00
null
2012-01-01
2012-05-01 00:00:00
null
2012-01-01
2012-06-01 00:00:00
null
2012-01-01
2012-07-01 00:00:00
null
2012-01-01
2012-08-01 00:00:00
null
2012-01-01
2012-09-01 00:00:00
null
2012-01-01
2012-10-01 00:00:00
null
2012-01-01
2012-11-01 00:00:00
null
2012-01-01
2012-12-01 00:00:00
null
2012-01-01
2013-01-01 00:00:00
12
2012-01-01
2013-02-01 00:00:00
null
2012-01-01
2013-03-01 00:00:00
null
2012-01-01
2013-04-01 00:00:00
17
2012-01-01
2013-05-05 00:00:00
5
2012-01-01
2013-06-01 00:00:00
null
2012-01-01
2013-07-01 00:00:00
null
2012-01-01
2013-08-01 00:00:00
null
2012-01-01
2013-09-01 00:00:00
null
2012-01-01
2013-10-01 00:00:00
null
2012-01-01
2013-11-01 00:00:00
null
2012-01-01
2013-12-01 00:00:00
null
2012-01-01
2014-01-01 00:00:00
null
2012-01-01
2014-02-01 00:00:00
null
2012-01-01
2014-03-01 00:00:00
null
2012-01-01
2014-04-01 00:00:00
null
2012-01-01
2014-05-01 00:00:00
null
2012-01-01
2014-06-01 00:00:00
null
2012-01-01
2014-07-01 00:00:00
null
2012-01-01
2014-08-01 00:00:00
null
2012-01-01
2014-09-01 00:00:00
null
2012-01-01
2014-10-01 00:00:00
null
2012-01-01
2014-11-01 00:00:00
null
2012-01-01
2014-12-01 00:00:00
null
2012-01-01
2015-01-01 00:00:00
null
2012-01-01
2015-02-01 00:00:00
null
2012-01-01
2015-03-01 00:00:00
null
2012-01-01
2015-04-01 00:00:00
null
2012-01-01
2015-05-01 00:00:00
null
2012-01-01
2015-06-01 00:00:00
null
2012-01-01
2015-07-01 00:00:00
null
2012-01-01
2015-08-01 00:00:00
null
2012-01-01
2015-09-01 00:00:00
null
2012-01-01
2015-10-01 00:00:00
null
2012-01-01
2015-11-01 00:00:00
null
2012-01-01
2015-12-01 00:00:00
null
2012-01-01
2016-01-01 00:00:00
null
2012-01-01
2016-02-01 00:00:00
null
2012-01-01
2016-03-01 00:00:00
null
2012-01-01
2016-04-01 00:00:00
null
2012-01-01
2016-05-01 00:00:00
null
2012-01-01
2016-06-01 00:00:00
null
2012-01-01
2016-07-01 00:00:00
null
2012-01-01
2016-08-01 00:00:00
null
2012-01-01
2016-09-01 00:00:00
null
2012-01-01
2016-10-01 00:00:00
null
2012-01-01
2016-11-01 00:00:00
null
2012-01-01
2016-12-01 00:00:00
null
2012-01-01
2017-01-01 00:00:00
null
2012-01-01
2017-02-01 00:00:00
null
2012-01-01
2017-03-01 00:00:00
null
2012-01-01
2017-04-01 00:00:00
null
2012-01-01
2017-05-01 00:00:00
null
2012-01-01
2017-06-01 00:00:00
null
2012-01-01
2017-07-01 00:00:00
null
2012-01-01
2017-08-01 00:00:00
null
2012-01-01
2017-09-01 00:00:00
null
2012-01-01
2017-10-01 00:00:00
null
2012-01-01
2017-11-01 00:00:00
null
2012-01-01
2017-12-01 00:00:00
null
2012-01-01
2018-01-01 00:00:00
null
2012-01-01
2018-02-01 00:00:00
null
2012-01-01
2018-03-01 00:00:00
null
2012-01-01
2018-04-01 00:00:00
null
2012-01-01
2018-05-01 00:00:00
null
2012-01-01
2018-06-01 00:00:00
null
2012-01-01
2018-07-01 00:00:00
null
2012-01-01
2018-08-01 00:00:00
null
2012-01-01
2018-09-01 00:00:00
null
2012-01-01
2018-10-01 00:00:00
null
2012-01-01
2018-11-01 00:00:00
null
2012-01-01
2018-12-01 00:00:00
null
2012-01-01
2019-01-01 00:00:00
null
2012-01-01
2019-02-01 00:00:00
null
2012-01-01
2019-03-01 00:00:00
null
2012-01-01
2019-04-01 00:00:00
null
2012-01-01
2019-05-01 00:00:00
null
2012-01-01
2019-06-01 00:00:00
null
2012-01-01
2019-07-01 00:00:00
null
2012-01-01
2019-08-01 00:00:00
null
2012-01-01
2019-09-01 00:00:00
null
2012-01-01
2019-10-01 00:00:00
null
2012-01-01
2019-11-01 00:00:00
null
2012-01-01
2019-12-01 00:00:00
null
2012-01-01
2020-01-01 00:00:00
null
2012-01-01
2020-02-01 00:00:00
null
2012-01-01
2020-03-01 00:00:00
null
2012-01-01
2020-04-01 00:00:00
null
2012-01-01
2020-05-01 00:00:00
null
2012-01-01
2020-06-01 00:00:00
null
2012-01-01
2020-07-01 00:00:00
null
2012-01-01
2020-08-01 00:00:00
null
2012-01-01
2020-09-01 00:00:00
null
2012-01-01
2020-10-01 00:00:00
null
2012-01-01
2020-11-01 00:00:00
null
2012-01-01
2020-12-01 00:00:00
null
2012-01-01
2021-01-01 00:00:00
null
2012-01-01
2021-02-01 00:00:00
null
2012-01-01
2021-03-01 00:00:00
null
2012-01-01
2021-04-01 00:00:00
null
2012-01-01
2021-05-01 00:00:00
null
2012-01-01
2021-06-01 00:00:00
null
2012-01-01
2021-07-01 00:00:00
null
2012-01-01
2021-08-01 00:00:00
null
2012-01-01
2021-09-01 00:00:00
null
2012-01-01
2021-10-01 00:00:00
null
2012-01-01
2021-11-01 00:00:00
null
2012-01-01
2021-12-01 00:00:00
null
2012-01-01
2022-01-01 00:00:00
null
2012-01-01
2022-02-01 00:00:00
null
2012-01-01
2022-03-01 00:00:00
null
2012-01-01
2022-04-01 00:00:00
null
2012-01-01
2022-05-01 00:00:00
null
2012-01-01
2022-06-01 00:00:00
null
2012-01-01
2022-07-01 00:00:00
null
2012-01-01
2022-08-01 00:00:00
null
2012-01-01
2022-09-01 00:00:00
null
2012-01-01
2022-10-01 00:00:00
null
2012-01-01
2022-11-01 00:00:00
null
2012-01-01
2022-12-01 00:00:00
null
2020-01-01
2020-01-01 00:00:00
5
2020-01-01
2020-02-01 00:00:00
null
2020-01-01
2020-03-01 00:00:00
null
2020-01-01
2020-04-01 00:00:00
null
2020-01-01
2020-05-01 00:00:00
null
2020-01-01
2020-06-01 00:00:00
null
2020-01-01
2020-07-01 00:00:00
null
2020-01-01
2020-08-01 00:00:00
null
2020-01-01
2020-09-01 00:00:00
null
2020-01-01
2020-10-01 00:00:00
null
2020-01-01
2020-11-01 00:00:00
null
2020-01-01
2020-12-01 00:00:00
null
2020-01-01
2021-01-01 00:00:00
null
2020-01-01
2021-02-01 00:00:00
null
2020-01-01
2021-03-01 00:00:00
null
2020-01-01
2021-04-01 00:00:00
null
2020-01-01
2021-05-01 00:00:00
null
2020-01-01
2021-06-01 00:00:00
null
2020-01-01
2021-07-01 00:00:00
null
2020-01-01
2021-08-01 00:00:00
null
2020-01-01
2021-09-01 00:00:00
null
2020-01-01
2021-10-01 00:00:00
null
2020-01-01
2021-11-01 00:00:00
null
2020-01-01
2021-12-01 00:00:00
null
2020-01-01
2022-01-01 00:00:00
null
2020-01-01
2022-02-01 00:00:00
null
2020-01-01
2022-03-01 00:00:00
null
2020-01-01
2022-04-01 00:00:00
null
2020-01-01
2022-05-01 00:00:00
null
2020-01-01
2022-06-01 00:00:00
null
2020-01-01
2022-07-01 00:00:00
null
2020-01-01
2022-08-01 00:00:00
null
2020-01-01
2022-09-01 00:00:00
null
2020-01-01
2022-10-01 00:00:00
null
2020-01-01
2022-11-01 00:00:00
null
2020-01-01
2022-12-01 00:00:00
null
SELECT 168
fiddle
Related
I have one table as below in SQL Server like below.
SELECT * FROM OverlappingDateRanges
Id startDate EndDate
10001 2020-04-01 00:00:00.000 2020-05-25 00:00:00.000
10001 2020-05-26 00:00:00.000 2020-07-15 00:00:00.000
10001 2020-07-17 00:00:00.000 2020-08-15 00:00:00.000
10001 2020-08-16 00:00:00.000 2020-10-15 00:00:00.000
10001 2020-10-16 00:00:00.000 2020-12-31 00:00:00.000
10002 2020-05-01 00:00:00.000 2020-05-29 00:00:00.000
10002 2020-05-30 00:00:00.000 2020-07-08 00:00:00.000
10002 2020-07-09 00:00:00.000 2020-10-01 00:00:00.000
10002 2020-10-03 00:00:00.000 2020-12-31 00:00:00.000
I want output like below where if there is no date difference between end date and next start date of same id, then then date will continue & its should break if end date and next start date is not in continue.
Output should be:
id startDate endDate
10001 2020-04-01 00:00:00.000 2020-07-15 00:00:00.000
10001 2020-07-17 00:00:00.000 2020-12-31 00:00:00.000
10002 2020-05-01 00:00:00.000 2020-10-01 00:00:00.000
10002 2020-10-03 00:00:00.000 2020-12-31 00:00:00.000
This is a type of gaps-and-islands problem. Identify where each output row starts by looking at the previous end. Then do a cumulative sum and aggregate:
select id, min(startdate), max(enddate)
from (select t.*,
sum(case when prev_enddate >= dateadd(day, -1, startdate) then 0 else 1
end) over (partition by id order by startdate) as grp
from (select t.*,
lag(enddate) over (partition by id order by startdate) as prev_enddate
from t
) t
) t
group by id, grp;
Here is a db<>fiddle.
I have got the following table.
date2 Group number
2020-28-05 00:00:00 A 55
2020-28-05 00:00:00 B 1.09
2020-28-05 00:00:00 C 1.8
2020-29-05 00:00:00 A 68
2020-29-05 00:00:00 B 1.9
2020-29-05 00:00:00 C 1.19
2020-01-06 00:00:00 A 10
2020-01-06 00:00:00 B 15
2020-01-06 00:00:00 C 0.88
2020-02-06 00:00:00 A 22
2020-02-06 00:00:00 B 15
2020-02-06 00:00:00 C 13
2020-03-06 00:00:00 A 66
2020-03-06 00:00:00 B 88
2020-03-06 00:00:00 C 99
As you can see between dates 2020-30-05 and 2020-31-05 are missing in this table. So it is necessary to fill these dates with 2020-29-05 information grouped by GROUP. As a result the final output should be like that:
date2 Group number
2020-28-05 00:00:00 A 55
2020-28-05 00:00:00 B 1.09
2020-28-05 00:00:00 C 1.8
2020-29-05 00:00:00 A 68
2020-29-05 00:00:00 B 1.9
2020-29-05 00:00:00 C 1.19
2020-30-05 00:00:00 A 68
2020-30-05 00:00:00 B 1.9
2020-30-05 00:00:00 C 1.19
2020-31-05 00:00:00 A 68
2020-31-05 00:00:00 B 1.9
2020-31-05 00:00:00 C 1.19
2020-01-06 00:00:00 A 10
2020-01-06 00:00:00 B 15
2020-01-06 00:00:00 C 0.88
2020-02-06 00:00:00 A 22
2020-02-06 00:00:00 B 15
2020-02-06 00:00:00 C 13
2020-03-06 00:00:00 A 66
2020-03-06 00:00:00 B 88
2020-03-06 00:00:00 C 99
I tried to do in the following way:
create a temporary table (table B) with only dates for period 2020-28-05 till 2020-03-06 and then use left merge, thus making these new dates as null (in order to then insert a CASE when null, so fill in last_value). However, it does not work, because when merging I got nulls only for one date (but should be 3 times one date(because of groups). This is only part of the larger dataset, can you help how can I get the necessary output?
PS I use Vertica
It's Vertica. And Vertica has the TIMESERIES clause, which seems to exactly match with what you need:
Out of a time series - like you have one - with irregular intervals between the rows, or with longer gaps in an otherwise regular time series, it creates a regular time series, with the same interval between each row pair as you specify in the AS sub-clause of the TIMESERIES clause itself. TS_FIRST_VALUE() and TS_LAST_VALUE() are functions that rely on that clause and return the right value deduced from the input rows at the generated time stamp. This right value can be obtained 'const', that is from the row in the original row set closest to the generated time stamp, or 'linear', that is, interpolated from the original row just before and the original row just after the generated timestamp. For your needs, you would use the constant value. See here:
WITH
-- your input ....
input(tmstmp,grp,nbr) AS (
SELECT TIMESTAMP '2020-05-28 00:00:00','A',55
UNION ALL SELECT TIMESTAMP '2020-05-28 00:00:00','B',1.09
UNION ALL SELECT TIMESTAMP '2020-05-28 00:00:00','C',1.8
UNION ALL SELECT TIMESTAMP '2020-05-29 00:00:00','A',68
UNION ALL SELECT TIMESTAMP '2020-05-29 00:00:00','B',1.9
UNION ALL SELECT TIMESTAMP '2020-05-29 00:00:00','C',1.19
UNION ALL SELECT TIMESTAMP '2020-06-01 00:00:00','A',10
UNION ALL SELECT TIMESTAMP '2020-06-01 00:00:00','B',15
UNION ALL SELECT TIMESTAMP '2020-06-01 00:00:00','C',0.88
UNION ALL SELECT TIMESTAMP '2020-06-02 00:00:00','A',22
UNION ALL SELECT TIMESTAMP '2020-06-02 00:00:00','B',15
UNION ALL SELECT TIMESTAMP '2020-06-02 00:00:00','C',13
UNION ALL SELECT TIMESTAMP '2020-06-03 00:00:00','A',66
UNION ALL SELECT TIMESTAMP '2020-06-03 00:00:00','B',88
UNION ALL SELECT TIMESTAMP '2020-06-03 00:00:00','C',99
)
-- real query here ...
SELECT
ts AS tmstmp
, grp
, TS_FIRST_VALUE(nbr,'const') AS nbr
FROM input
TIMESERIES ts AS '1 DAY' OVER(PARTITION BY grp ORDER BY tmstmp)
ORDER BY 1,2
;
-- out tmstmp | grp | nbr
-- out ---------------------+-----+-------
-- out 2020-05-28 00:00:00 | A | 55.00
-- out 2020-05-28 00:00:00 | B | 1.09
-- out 2020-05-28 00:00:00 | C | 1.80
-- out 2020-05-29 00:00:00 | A | 68.00
-- out 2020-05-29 00:00:00 | B | 1.90
-- out 2020-05-29 00:00:00 | C | 1.19
-- out 2020-05-30 00:00:00 | A | 68.00
-- out 2020-05-30 00:00:00 | B | 1.90
-- out 2020-05-30 00:00:00 | C | 1.19
-- out 2020-05-31 00:00:00 | A | 68.00
-- out 2020-05-31 00:00:00 | B | 1.90
-- out 2020-05-31 00:00:00 | C | 1.19
-- out 2020-06-01 00:00:00 | A | 10.00
-- out 2020-06-01 00:00:00 | B | 15.00
-- out 2020-06-01 00:00:00 | C | 0.88
-- out 2020-06-02 00:00:00 | A | 22.00
-- out 2020-06-02 00:00:00 | B | 15.00
-- out 2020-06-02 00:00:00 | C | 13.00
-- out 2020-06-03 00:00:00 | A | 66.00
-- out 2020-06-03 00:00:00 | B | 88.00
I need to create new interval rows based on a start datetime column and an end datetime column.
My statement looks like this currently
select id,
startdatetime,
enddatetime
from calls
result looks like this
id startdatetime enddatetime
1 01/01/2020 00:00:00 01/01/2020 04:00:00
I would like a result like this
id startdatetime enddatetime Intervals
1 01/01/2020 00:00:00 01/01/2020 03:00:00 01/01/2020 00:00:00
1 01/01/2020 00:00:00 01/01/2020 03:00:00 01/01/2020 01:00:00
1 01/01/2020 00:00:00 01/01/2020 03:00:00 01/01/2020 02:00:00
1 01/01/2020 00:00:00 01/01/2020 03:00:00 01/01/2020 03:00:00
Thanking you in advance
p.s. I'm new to SQL
You can use a recursive sub-query factoring clause to loop and incrementally add an hour:
WITH times ( id, startdatetime, enddatetime, intervals ) AS (
SELECT id,
startdatetime,
enddatetime,
startdatetime
FROM calls c
UNION ALL
SELECT id,
startdatetime,
enddatetime,
intervals + INTERVAL '1' HOUR
FROM times
WHERE intervals + INTERVAL '1' HOUR <= enddatetime
)
SELECT *
FROM times;
outputs:
ID | STARTDATETIME | ENDDATETIME | INTERVALS
-: | :------------------ | :------------------ | :------------------
1 | 2020-01-01 00:00:00 | 2020-01-01 04:00:00 | 2020-01-01 00:00:00
1 | 2020-01-01 00:00:00 | 2020-01-01 04:00:00 | 2020-01-01 01:00:00
1 | 2020-01-01 00:00:00 | 2020-01-01 04:00:00 | 2020-01-01 02:00:00
1 | 2020-01-01 00:00:00 | 2020-01-01 04:00:00 | 2020-01-01 03:00:00
1 | 2020-01-01 00:00:00 | 2020-01-01 04:00:00 | 2020-01-01 04:00:00
db<>fiddle here
You can use the hierarchy query as following:
SQL> WITH CALLS (ID, STARTDATETIME, ENDDATETIME)
2 AS ( SELECT 1,
3 TO_DATE('01/01/2020 00:00:00', 'dd/mm/rrrr hh24:mi:ss'),
4 TO_DATE('01/01/2020 04:00:00', 'dd/mm/rrrr hh24:mi:ss')
5 FROM DUAL)
6 -- Your query starts from here
7 SELECT
8 ID,
9 STARTDATETIME,
10 ENDDATETIME,
11 STARTDATETIME + ( COLUMN_VALUE / 24 ) AS INTERVALS
12 FROM
13 CALLS C
14 CROSS JOIN TABLE ( CAST(MULTISET(
15 SELECT LEVEL - 1
16 FROM DUAL
17 CONNECT BY LEVEL <= TRUNC(24 *(ENDDATETIME - STARTDATETIME))
18 ) AS SYS.ODCINUMBERLIST) )
19 ORDER BY INTERVALS;
ID STARTDATETIME ENDDATETIME INTERVALS
---------- ------------------- ------------------- -------------------
1 01/01/2020 00:00:00 01/01/2020 04:00:00 01/01/2020 00:00:00
1 01/01/2020 00:00:00 01/01/2020 04:00:00 01/01/2020 01:00:00
1 01/01/2020 00:00:00 01/01/2020 04:00:00 01/01/2020 02:00:00
1 01/01/2020 00:00:00 01/01/2020 04:00:00 01/01/2020 03:00:00
SQL>
Cheers!!
Date from Date to
2018-12-11 2019-01-08
2019-01-08 2019-02-09
2019-02-10 2019-03-14
2019-03-17 2019-04-11
2019-04-15 2019-05-16
2019-05-16 2019-06-13
output will be like this
Date from Date to Days
2018-12-11 2019-01-08 0
2019-01-08 2019-02-09 1
2019-02-10 2019-03-14 3
2019-03-17 2019-04-11 4
2019-04-15 2019-05-16 0
2019-05-16 2019-06-13 -
To return the difference between two date values in days you could use the DATEDIFF() Function, something like:
SELECT DATEDIFF(DAY, DayFrom, DayTo) AS 'DaysBetween'
FROM DateTable
You want lead() and a date diff function:
select
date_from,
date_to,
datediff(day, date_to, lead(date_from) over(order by date_from)) days
from mytable
datediff() is a SQLServer function. There are equivalents in other RDBMS.
Side note: I would recommend againts using a string value (-) for records that do not have a next record, since other values are numeric (the datatypes in a column must be consistant). null is good enough for this (which the above query will produce).
Demo on DB Fiddle:
date_from | date_to | days
:------------------ | :------------------ | ---:
11/12/2018 00:00:00 | 08/01/2019 00:00:00 | 0
08/01/2019 00:00:00 | 09/02/2019 00:00:00 | 1
10/02/2019 00:00:00 | 14/03/2019 00:00:00 | 3
17/03/2019 00:00:00 | 11/04/2019 00:00:00 | 4
15/04/2019 00:00:00 | 16/05/2019 00:00:00 | 0
16/05/2019 00:00:00 | 13/06/2019 00:00:00 | null
I need to SELECT a row in which issue_date = maturity_date of another row with same id, and same amount_usd.
I tried with self join, but I do not get right result.
Here is a simplified version of my table:
ID ISSUE_DATE MATURITY_DATE AMOUNT_USD
1 2010-01-01 00:00:00.000 2015-12-01 00:00:00.000 5000
1 2010-01-01 00:00:00.000 2001-09-19 00:00:00.000 700
2 2014-04-09 00:00:00.000 2019-04-09 00:00:00.000 400
1 2015-12-01 00:00:00.000 2016-12-31 00:00:00.000 5000
5 2015-02-24 00:00:00.000 2015-02-24 00:00:00.000 8000
4 2012-11-29 00:00:00.000 2015-11-29 00:00:00.000 10000
3 2015-01-21 00:00:00.000 2018-01-21 00:00:00.000 17500
2 2015-02-02 00:00:00.000 2015-12-05 00:00:00.000 12000
1 2015-01-12 00:00:00.000 2018-01-12 00:00:00.000 18000
2 2015-12-05 00:00:00.000 2016-01-10 00:00:00.000 12000
Result should be:
ID ISSUE_DATE MATURITY_DATE AMOUNT_USD
1 2015-12-01 00:00:00.000 2016-12-31 00:00:00.000 5000
2 2015-12-05 00:00:00.000 2016-01-10 00:00:00.000 12000
Thanks in advance!
Do following: http://sqlfiddle.com/#!6/c0a02/1
select a.id, a.issue_date, a.maturity_date, a.amount_usd
from tbl a
inner join tbl b
on a.id = b.id
and a.maturity_date = b.issue_date
-- added to prevent same maturity date and issue date
where a.maturity_date <> a.issue_date
Output:
| id | issue_date | maturity_date | amount_usd |
|----|----------------------------|----------------------------|------------|
| 1 | January, 01 2010 00:00:00 | December, 01 2015 00:00:00 | 5000 |
| 2 | February, 02 2015 00:00:00 | December, 05 2015 00:00:00 | 12000 |