Oracle sql set end date based on previous start date - oracle

I have one table where I need to add new column endDate for future implementation but since we have currently only start date for all records I need to set endDate which should be equal to start date from previous record that are connected by userId and if it is only one record for that user than end date will have some value in future.
For example:
Table structure:
ID | USER_ID | START_DATE | END_DATE
-------------------------------------
1 | 1 | 01.01.2015 |
2 | 1 | 01.01.2016 |
3 | 1 | 01.07.2018 |
4 | 1 | 01.08.2021 |
5 | 2 | 01.01.2015 |
6 | 3 | 01.01.2016 |
7 | 3 | 01.07.2018 |
8 | 4 | 01.08.2021 |
Expected result should be like this
ID | USER_ID | START_DATE | END_DATE
-------------------------------------
1 | 1 | 01.01.2015 | 01.01.2016
2 | 1 | 01.01.2016 | 01.07.2018
3 | 1 | 01.07.2018 | 01.08.2021
4 | 1 | 01.08.2021 | 01.01.2050
5 | 2 | 01.01.2015 | 01.01.2050
6 | 3 | 01.01.2016 | 01.07.2018
7 | 3 | 01.07.2018 | 01.01.2050
8 | 4 | 01.08.2021 | 01.01.2050
Can someone help me with how query in oracle databse should look to update it like this?
I've tried something with for loop but not sure how to continue from this step
DECLARE
CURSOR c_contract
IS
SELECT
USER_ID
FROM
CONTRACT
ORDER_BY START_DATE

BEGIN
FOR r_contract IN c_contract
LOOP

dbms_output.put_line( r_contract.USER_ID );
END LOOP;

END;

Use the LEAD analytic function with the default date as the third argument:
SELECT t.*,
LEAD( start_date, 1, DATE '2050-01-01') OVER (
PARTITION BY user_id
ORDER BY start_date
) AS end_date
FROM table_name t
Which, for the sample data:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
CREATE TABLE table_name ( ID, USER_ID, START_DATE ) AS
SELECT 1, 1, DATE '2015-01-01' FROM DUAL UNION ALL
SELECT 2, 1, DATE '2016-01-01' FROM DUAL UNION ALL
SELECT 3, 1, DATE '2018-07-01' FROM DUAL UNION ALL
SELECT 4, 1, DATE '2021-08-01' FROM DUAL UNION ALL
SELECT 5, 2, DATE '2015-01-01' FROM DUAL UNION ALL
SELECT 6, 3, DATE '2016-01-01' FROM DUAL UNION ALL
SELECT 7, 3, DATE '2018-07-01' FROM DUAL UNION ALL
SELECT 8, 4, DATE '2021-08-01' FROM DUAL;
Outputs:
ID
USER_ID
START_DATE
END_DATE
1
1
2015-01-01 00:00:00
2016-01-01 00:00:00
2
1
2016-01-01 00:00:00
2018-07-01 00:00:00
3
1
2018-07-01 00:00:00
2021-08-01 00:00:00
4
1
2021-08-01 00:00:00
2050-01-01 00:00:00
5
2
2015-01-01 00:00:00
2050-01-01 00:00:00
6
3
2016-01-01 00:00:00
2018-07-01 00:00:00
7
3
2018-07-01 00:00:00
2050-01-01 00:00:00
8
4
2021-08-01 00:00:00
2050-01-01 00:00:00
If you want to add a column then:
ALTER TABLE table_name ADD (end_date DATE);
MERGE INTO table_name dst
USING (
SELECT ROWID AS rid,
LEAD( start_date, 1, DATE '2050-01-01') OVER (
PARTITION BY user_id
ORDER BY start_date
) AS end_date
FROM table_name
) src
ON (dst.ROWID = src.rid)
WHEN MATCHED THEN
UPDATE SET end_date = src.end_date;
fiddle

Related

how to check length of "concatenate" string

Has anybody an idea how to write a query in Oracle to get length of characters:
| user | action |
-------------------------
| mary | aaa | # 3 characters from action
| mary | bbbbb | # 5 characters from action
| mary | c | # 1 character from action
| adam | xx | # 2 characters from action
| adam | yyyy | # 4 characters from action
| adam | zzzzzzz | # 7 characters from action
So in result should be sum of characters for each:
| mary | 9 |
| adam | 13 |
Thanks.
SUM + LENGTH along with GROUP BY user. Sample data in lines #1 - 14; query you need begins at line #15.
SQL> WITH
2 test (cuser, action)
3 AS
4 (SELECT 'mary', 'aaa' FROM DUAL
5 UNION ALL
6 SELECT 'mary', 'bbbbb' FROM DUAL
7 UNION ALL
8 SELECT 'mary', 'c' FROM DUAL
9 UNION ALL
10 SELECT 'adam', 'xx' FROM DUAL
11 UNION ALL
12 SELECT 'adam', 'yyyy' FROM DUAL
13 UNION ALL
14 SELECT 'adam', 'zzzzzzz' FROM DUAL)
15 SELECT cuser, SUM (LENGTH (action))
16 FROM test
17 GROUP BY cuser;
CUSE SUM(LENGTH(ACTION))
---- -------------------
mary 9
adam 13
SQL>
Use the LENGTH function in your aggregation:
SELECT "USER",
SUM( LENGTH( action ) ) AS total_length
FROM table_name
GROUP BY "USER"
Which, for your sample data:
CREATE TABLE table_name( "USER", action ) AS
SELECT 'mary', 'aaa' FROM DUAL UNION ALL
SELECT 'mary', 'bbbbb' FROM DUAL UNION ALL
SELECT 'mary', 'c' FROM DUAL UNION ALL
SELECT 'adam', 'xx' FROM DUAL UNION ALL
SELECT 'adam', 'yyyy' FROM DUAL UNION ALL
SELECT 'adam', 'zzzzzzz' FROM DUAL;
Outputs:
USER | TOTAL_LENGTH
:--- | -----------:
mary | 9
adam | 13
db<>fiddle here

Get the sales count within a month

I have monthly agents' sales data. I need to get the first 25 days sales count and last 5 days columns.
How to get in separately.
I have below table
Agent_ID Date Device
2343 1/1/2019 33330
3245 1/1/2019 43554
2343 5/1/2019 46665
3245 10/1/2019 78900
2343 15/1/2019 55678
2343 26/1/2019 45678
3245 28/1/2019 48900
2343 30/1/2019 56710
5645 12/1/2019 33067
5645 15/1/2019 44890
2121 31/1/2019 55810
I need to get this below output table
Agent_ID first_25days_sale_count Last_5days_sale_count
2343 3 2
3245 2 1
5645 2 0
2121 0 1
Some months have 28, 29 or 31 days so naively using "first 25 days" and "last 5 days" may lead to either double counting (i.e. days 24 and 25 when February has 28 days) or not counting some days (i.e. day 26 when the the month has 31 days). You should decide whether you want to count:
The first 25 days and then the remaining 3-6 days after that; or
The last 5 days and then the 23-26 days before that.
Whichever you chose, you can use conditional aggregation:
SELECT agent_id,
COUNT(
CASE
WHEN EXTRACT( DAY FROM "Date" ) <= 25
THEN 1
END
) AS first_25days_sale_count,
COUNT(
CASE
WHEN EXTRACT( DAY FROM "Date" ) > 25
THEN 1
END
) AS after_first_25days_sale_count,
COUNT(
CASE
WHEN "Date" < TRUNC( LAST_DAY( "Date" ) ) - INTERVAL '4' DAY
THEN 1
END
) AS not_last_5days_sale_count,
COUNT(
CASE
WHEN "Date" >= TRUNC( LAST_DAY( "Date" ) ) - INTERVAL '4' DAY
THEN 1
END
) AS last_5days_sale_count
FROM your_table
GROUP BY agent_id;
So, for your sample data:
CREATE TABLE your_table ( Agent_ID, "Date", Device ) AS
SELECT 2343, DATE '2019-01-01', 33330 FROM DUAL UNION ALL
SELECT 3245, DATE '2019-01-01', 43554 FROM DUAL UNION ALL
SELECT 2343, DATE '2019-01-05', 46665 FROM DUAL UNION ALL
SELECT 3245, DATE '2019-01-10', 78900 FROM DUAL UNION ALL
SELECT 2343, DATE '2019-01-15', 55678 FROM DUAL UNION ALL
SELECT 2343, DATE '2019-01-26', 45678 FROM DUAL UNION ALL
SELECT 3245, DATE '2019-01-29', 48900 FROM DUAL UNION ALL
SELECT 2343, DATE '2019-01-30', 56710 FROM DUAL UNION ALL
SELECT 5645, DATE '2019-01-12', 33067 FROM DUAL UNION ALL
SELECT 5645, DATE '2019-01-15', 44890 FROM DUAL UNION ALL
SELECT 2121, DATE '2019-01-31', 55810 FROM DUAL;
This outputs:
AGENT_ID | FIRST_25DAYS_SALE_COUNT | AFTER_FIRST_25DAYS_SALE_COUNT | NOT_LAST_5DAYS_SALE_COUNT | LAST_5DAYS_SALE_COUNT
-------: | ----------------------: | ----------------------------: | ------------------------: | --------------------:
3245 | 2 | 1 | 2 | 1
2121 | 0 | 1 | 0 | 1
5645 | 2 | 0 | 2 | 0
2343 | 3 | 2 | 4 | 1
db<>fiddle here

Month End Date Range where most recent day = system date

I am trying to generate a range of 'last day of the month' dates from a given start date (01/01/2019) to the system date (sysdate).
I am able to generate the date range for this using the below query:
select last_day(add_months (trunc (to_date('01/01/2019','MM/DD/YYYY'), 'MM'), Level - 1))
Month FROM Dual
CONNECT BY Level <= MONTHS_BETWEEN(sysdate, to_date('01/01/2019','MM/DD/YYYY')) + 1
order by month
I am trying to get the last record to equal the sysdate so I can calculate running balances - how can I go about adding this to the above query?
Example output of the solution:
+------------------------+
| MONTH |
+------------------------+
| 1/31/2019 12:00:00 AM |
| 2/28/2019 12:00:00 AM |
| 3/31/2019 12:00:00 AM |
| 4/30/2019 12:00:00 AM |
| 5/31/2019 12:00:00 AM |
| 6/30/2019 12:00:00 AM |
| 7/31/2019 12:00:00 AM |
| 8/31/2019 12:00:00 AM |
| 9/30/2019 12:00:00 AM |
| 10/31/2019 12:00:00 AM |
| 11/30/2019 12:00:00 AM |
| 12/31/2019 12:00:00 AM |
| 1/31/2020 12:00:00 AM |
| 2/25/2020 12:00:00 AM |
+------------------------+
If I understood you correctly, you'd want today's date in the last line. If so, use CASE (lines #13 - 19).
Lines #1 - 12 represent your current query.
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> WITH your_data
2 AS ( SELECT LAST_DAY (
3 ADD_MONTHS (
4 TRUNC (TO_DATE ('01/01/2019', 'MM/DD/YYYY'), 'MM'),
5 LEVEL - 1))
6 Month
7 FROM DUAL
8 CONNECT BY LEVEL <=
9 MONTHS_BETWEEN (
10 SYSDATE,
11 TO_DATE ('01/01/2019', 'MM/DD/YYYY'))
12 + 1)
13 SELECT CASE
14 WHEN TRUNC (month, 'mm') = TRUNC (SYSDATE, 'mm')
15 THEN
16 TRUNC (SYSDATE)
17 ELSE
18 month
19 END
20 month
21 FROM your_data
22 ORDER BY month;
MONTH
-------------------
31.01.2019 00:00:00
28.02.2019 00:00:00
31.03.2019 00:00:00
30.04.2019 00:00:00
31.05.2019 00:00:00
30.06.2019 00:00:00
31.07.2019 00:00:00
31.08.2019 00:00:00
30.09.2019 00:00:00
31.10.2019 00:00:00
30.11.2019 00:00:00
31.12.2019 00:00:00
31.01.2020 00:00:00
18.02.2020 00:00:00
14 rows selected.
SQL>
When I first saw this I immediately thought a recursive CTE would be perfect. The problem I kept getting a type mismatch on the recursion that I could not resolve. It turned out Oracle 11g has a bug in recursive CTE with recurson with dates. This finally got me to updating at long last. So thanks for the question. Anyway for future viewers a recursive CTE does work.
alter session set nls_date_format = 'yyyy-mm-dd';
with date_list (dte) as
( select last_day(date '2019-01-01') from dual
union all
select least(add_months(dte,1), trunc(sysdate))
from date_list
where dte<trunc(sysdate)
)
select dte from date_list;

How to get latest active record

Here is my data. Member can be enroll multiple times and can change plan anytime,I need to get C_level data for latest near to getdate that is "8/1/2017" for ID 1, For ID 2 doesn't have latest data, that case we have to show 12/31/2016 record.
ID Start_Date End_Date C_Level
1 1/1/2016 12/31/2016 1
1 1/1/2017 8/1/2017 2
1 9/1/2017 12/31/2017 3
1 1/1/2018 12/31/2018 0
2 1/1/2015 12/31/2015 2
2 1/1/2016 12/31/2016 3
If I understand your requirements right, then this query should give what you want:
WITH current_running AS (
SELECT to_date( '8/1/2017','mm/dd/rrrr') As Current_running_date
FROM dual
)
SELECT * FROM (
SELECT t.*,
row_number() Over (partition by id order by end_date desc ) As rn,
c.Current_running_date
FROM Table1 t
JOIN current_running c
ON c.Current_running_date >= ANY( t.Start_Date, t.End_Date )
)
WHERE rn = 1
;
Demo: http://sqlfiddle.com/#!4/33de0/10
For current_running_date = 2017-08-01 it gives:
| ID | START_DATE | END_DATE | C_LEVEL | RN | CURRENT_RUNNING_DATE |
|----|-----------------------|-----------------------|---------|----|----------------------|
| 1 | 2017-01-01 00:00:00.0 | 2017-08-01 00:00:00.0 | 2 | 1 | 2017-08-01T00:00:00Z |
| 2 | 2016-01-01 00:00:00.0 | 2016-12-31 00:00:00.0 | 3 | 1 | 2017-08-01T00:00:00Z |
while for current_running_date = 2016-07-15
| ID | START_DATE | END_DATE | C_LEVEL | RN | CURRENT_RUNNING_DATE |
|----|-----------------------|-----------------------|---------|----|----------------------|
| 1 | 2016-01-01 00:00:00.0 | 2016-12-31 00:00:00.0 | 1 | 1 | 2016-07-15T00:00:00Z |
| 2 | 2016-01-01 00:00:00.0 | 2016-12-31 00:00:00.0 | 3 | 1 | 2016-07-15T00:00:00Z |

List all days between two dates in Oracle

I am converting a postgres app to an Oracle app.
I came across this query:
WITH cost AS (SELECT
well_schedules.id,
generate_series(well_schedules.start_date::timestamp, well_schedules.end_date, '1 Day') AS "Date",
(well_schedules.drilling_engineering_estimate * well_schedules.well_estimated_working_interest)/((well_schedules.end_date - well_schedules.start_date) + 1) AS "Cost Per Day"
FROM
well_schedules
)
SELECT date_trunc('quarter', "Date"), COUNT("Cost Per Day"), id
FROM cost
GROUP BY id, date_trunc('quarter', "Date")
ORDER BY date_trunc('quarter', "Date")
The part I am struggling with is the generate_series line.
That line takes a start_date and end_date and lists all days between those two dates. We need that information to compile per day/week/month/quarter/year reports (or at least we assume we need that info).
Our data looks like this:
well_schedules
| id | start_date | end_date | cost |
| 1 | '2015-01-01' | '2015-03-20' | 100 |
We assume cost_per_day is equal across all days, so we'd like to generate a report that lets us look at cost_per_day, cost_per_week, cost_per_month, cost_per_year, and cost_per_quarter. cost_per_week/month/quarter/year is calculated by grouping the days by week/month/quarter/year and summing the associated cost_per_days
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE well_schedules ( id, start_date, end_date, cost ) AS
SELECT 1 , DATE '2015-01-01', DATE '2015-01-20', 100 FROM DUAL;
Query 1:
SELECT ID,
COLUMN_VALUE AS Day,
COST / ( end_date - start_date + 1 ) AS Cost_per_day
FROM well_schedules,
TABLE (
CAST(
MULTISET(
SELECT start_date + LEVEL - 1
FROM DUAL
CONNECT BY start_date + LEVEL - 1 <= end_date
)
AS SYS.ODCIDATELIST
)
)
Results:
| ID | DAY | COST_PER_DAY |
|----|---------------------------|--------------|
| 1 | January, 01 2015 00:00:00 | 5 |
| 1 | January, 02 2015 00:00:00 | 5 |
| 1 | January, 03 2015 00:00:00 | 5 |
| 1 | January, 04 2015 00:00:00 | 5 |
| 1 | January, 05 2015 00:00:00 | 5 |
| 1 | January, 06 2015 00:00:00 | 5 |
| 1 | January, 07 2015 00:00:00 | 5 |
| 1 | January, 08 2015 00:00:00 | 5 |
| 1 | January, 09 2015 00:00:00 | 5 |
| 1 | January, 10 2015 00:00:00 | 5 |
| 1 | January, 11 2015 00:00:00 | 5 |
| 1 | January, 12 2015 00:00:00 | 5 |
| 1 | January, 13 2015 00:00:00 | 5 |
| 1 | January, 14 2015 00:00:00 | 5 |
| 1 | January, 15 2015 00:00:00 | 5 |
| 1 | January, 16 2015 00:00:00 | 5 |
| 1 | January, 17 2015 00:00:00 | 5 |
| 1 | January, 18 2015 00:00:00 | 5 |
| 1 | January, 19 2015 00:00:00 | 5 |
| 1 | January, 20 2015 00:00:00 | 5 |
I will suggest the code below that consider the first and last day of the month from two dates:
Example:
Date Initial: 01/10/2014
Date Final: 12/21/2018
The code will return:
01/01/2014
02/01/2014
03/01/2014
04/01/2014
...
12/28/2018
12/29/2018
12/30/2018
12/31/2018
The Code:
SELECT
CAL.DT AS "Date"
,TO_NUMBER(TO_CHAR(CAL.DT,'DD')) AS "Day"
,TO_NUMBER(TO_CHAR(CAL.DT,'MM')) AS "Month"
,TO_NUMBER(TO_CHAR(CAL.DT,'YY')) AS "YearYY"
,TO_NUMBER(TO_CHAR(CAL.DT,'YYYY')) AS "YearYYYY"
,TO_CHAR(CAL.DT,'day') AS "Description_Day"
,TO_CHAR(CAL.DT,'dy') AS "Description_Day_Abrev"
,TO_CHAR(CAL.DT,'Month') AS "Description_Month"
,TO_CHAR(CAL.DT,'Mon') AS "Description_Month_Abrev"
,TO_CHAR(CAL.DT,'dd month yyyy') AS "Date_Text"
FROM (
SELECT
(
TO_DATE(SEQ.MM || SEQ.YYYY, 'MM/YYYY')-1
) + SEQ.NUM AS "DT"
FROM
(
SELECT RESULT NUM,
TO_CHAR(( -- Minimum Date
TO_DATE('01/01/2014', 'DD/MM/YYYY')
) , 'MM') AS "MM",
TO_CHAR(( -- Minimum Date
TO_DATE('01/01/2014', 'DD/MM/YYYY')
) , 'YYYY') AS "YYYY"
FROM
(
SELECT ROWNUM RESULT FROM DUAL CONNECT BY LEVEL <= (
(
-- Maximum Date
LAST_DAY(TO_DATE('31/12/2018', 'DD/MM/YYYY')) -- Always Last Day
-
-- Maximum Date
TRUNC(TO_DATE('01/01/2014', 'DD/MM/YYYY')) -- Always First Day of Month
) + 1 -- Because the First Day (RESULT) don't begin at zero
)
) -- How many sequences (RESULT) to generate
) SEQ
) CAL
;

Resources