Update column based on lead function in oracle - oracle19c

I have valid_from and valid_to columns in a table.
I need to update the valid_to column based on the next row of valid_from column.
Please help me on this.
Current
Runid
Valid_from
valid_to
1
1-Jan-21
10-Jan-21
1
11-Jan-21
11-Jan-21
1
15-Jan-21
17-Jan-21
1
18-Jan-21
1-Jan-00
Desired
Runid
Valid_from
valid_to
1
1-Jan-21
11-Jan-21
1
11-Jan-21
15-Jan-21
1
15-Jan-21
18-Jan-21
1
18-Jan-21
1-Jan-00

You can use the LEAD function and correlate on the ROWID pseudo-column:
UPDATE table_name t
SET valid_to = (SELECT next_from
FROM (
SELECT LEAD(valid_from, 1, valid_to)
OVER (PARTITION BY runid ORDER BY valid_from) AS next_from
FROM table_name
) x
WHERE x.ROWID = t.ROWID
)
Which, for the sample data:
CREATE TABLE table_name (Runid, Valid_from, valid_to) AS
SELECT 1, DATE '2021-01-01', DATE '2021-01-10' FROM DUAL UNION ALL
SELECT 1, DATE '2021-01-11', DATE '2021-01-11' FROM DUAL UNION ALL
SELECT 1, DATE '2021-01-15', DATE '2021-01-17' FROM DUAL UNION ALL
SELECT 1, DATE '2021-01-18', DATE '4000-01-11' FROM DUAL;
Updates the table to:
RUNID
VALID_FROM
VALID_TO
1
2021-01-01 00:00:00
2021-01-11 00:00:00
1
2021-01-11 00:00:00
2021-01-15 00:00:00
1
2021-01-15 00:00:00
2021-01-18 00:00:00
1
2021-01-18 00:00:00
4000-01-01 00:00:00
db<>fiddle here

Related

How to distinct count an ID only on the first date in DAX?

I have a Table where an ID can occur over multiple dates due to having different states.
ID
State
DATE
A
a
2022-01-01
A
b
2022-01-02
A
c
2022-01-03
B
d
2022-01-01
B
e
2022-01-02
C
f
2022-01-03
I would like to create ONE measure to distinct count the IDs.
This measure is needed for KPI cards, Line Charts and table visuals.
This is easily done with:
Count = DISTINCTCOUNT('Table'[ID])
Displaying this measure in a table visual split by Date.
Date
Count
2022-01-01
2
2022-01-02
2
2022-01-03
2
Total
3
A count will appear for each date where this ID occurred. The Total will always be correct.
However, I do not want to distinct count for each date. Just the first date at which the ID occurred.
Date
Count
2022-01-01
2
2022-01-02
0
2022-01-03
1
Total
3
First create a measured column :
Dates = calculate ( min('Table'[DATE]),
filter('Table','Table'[ID ] = EARLIER('Table'[ID ])))
then use your measure with the above column
Count = DISTINCTCOUNT('Table'[ID ])
This one will works fast. Just replace your measure with this one and get a result.
MyMeasure =
VAR t =
CALCULATETABLE( --new
VALUES('table'[ID]) --new
,FILTER(
'table'
,VAR dateInRow=[DATE]
RETURN
dateInRow=CALCULATE(
min('table'[DATE])
,ALLEXCEPT('table','table'[ID])
)
)
) -- new
VAR result = COUNTROWS(t)
RETURN
IF(
ISBLANK(result)
,0
,result
)

GCP Bigquery WORKDAY function

How do I go about calculating number of workdays in a MONTH based on a date in another column?
Example:
Column 1 - 2020-06-30
Column 2 (Calculated) - 22 (i.e number of workdays in the month of June Mon to Friday)
Does BQ have a WORKDAY function?
You can use below approach
create temp function workdays(input date) as ((
select count(*)
from unnest(generate_date_array(date_trunc(input, month), last_day(input, month ))) day
where not extract(dayofweek from day) in (1, 7)
));
select column1,
workdays(column1) as column2
from your_table
if applied to sample data in your question - output is

How to calculate working days on ORACLE APEX?

I have 2 tables; employee and time. For time table, I want to take id information automatically from employee table and subtracting every employees' start date of work from employee table from system date. If result = 1 year plus, result will be automatically added to the time table.
employee.hire_date date /
time.year number
I want it to do this (time.year = sysdate - employee.hire_date) process automatically.
For example, the employee started on 21.01.2019. Today(21.01.2020) write '1' on time table year column automatically.
Thanks.
Create times as a view on the employees table.
Oracle Setup:
CREATE TABLE employees ( id, start_date ) AS
SELECT 1, DATE '2019-01-21' FROM DUAL UNION ALL
SELECT 2, DATE '2019-06-21' FROM DUAL UNION ALL
SELECT 3, DATE '2015-01-01' FROM DUAL;
Create View:
CREATE VIEW times ( id, years ) AS
SELECT id,
FLOOR( MONTHS_BETWEEN( SYSDATE, start_date ) / 12 )
FROM employees;
Output:
SELECT *
FROM times;
gives:
ID | YEARS
-: | ----:
1 | 1
2 | 0
3 | 5
db<>fiddle here

How to pluck the oldest child record from a join in BigQuery SQL where ordered by date and limit 1

I'm analyzing Shopify Schema Data with BiqQuery in GCP and trying to join the Shopify Customers data to the child record that is Orders data so that I can find the very first Order for each Customer created in a certain time, but limit that to 1...like order by created_at asc, limit 1. I'm not really able to figure this out
Select
customers.created_at as customer,
customers.orders_count,
orders.created_at,
orders.order_number
FROM `shopxxx.shopify.customers` as customers
Join `shopxxx.shopify.orders` as orders
on customer = orders.customer
WHERE
customers.orders_count != 0
AND customers.created_at > "2018-09-12 00:00:00 UTC" and customers.created_at < "2019-09-12 23:59:59 UTC"
and orders.source_name != 'web'
order by customers.created_at desc
Below is for BigQuery Standard SQL
#standardSQL
SELECT
customers.customer_id,
customers.created_at AS customer,
customers.orders_count,
ARRAY_AGG(STRUCT(orders.created_at AS created_at, orders.order_number AS order_number) ORDER BY orders.created_at LIMIT 1)[OFFSET(0)].*
FROM `shopxxx.shopify.customers` AS customers
JOIN `shopxxx.shopify.orders` AS orders
ON customers.customer_id = orders.customer_id
WHERE customers.orders_count != 0
AND customers.created_at > "2018-09-12 00:00:00 UTC" AND customers.created_at < "2019-09-12 23:59:59 UTC"
AND orders.source_name != 'web'
GROUP BY 1, 2, 3
ORDER BY customers.created_at DESC
or different approach
#standardSQL
SELECT
customers.customer_id,
customers.created_at AS customer,
customers.orders_count,
orders.created_at AS created_at,
orders.order_number AS order_number
FROM `shopxxx.shopify.customers` AS customers
JOIN (
SELECT customer_id, ARRAY_AGG(STRUCT(created_at, order_number) ORDER BY created_at LIMIT 1)[OFFSET(0)].*
FROM `shopxxx.shopify.orders`
WHERE source_name != 'web' GROUP BY customer_id
) AS orders
ON customers.customer_id = orders.customer_id
WHERE customers.orders_count != 0
AND customers.created_at > "2018-09-12 00:00:00 UTC" AND customers.created_at < "2019-09-12 23:59:59 UTC"
ORDER BY customers.created_at DESC

Calculate latest date for every index with DAX

I have a dataset in Power BI like this
ID DATE
1 06/24/2016
1 06/24/2017
1 06/24/2018
2 08/08/2017
2 08/08/2016
3 12/12/2015
I would like to create a calculated column with DAX, in which i have to calculate the latest date for every index, but I am only able to get the latest date of all the dataset or the same date for every row.
The output should be this:
ID DATE MAXDATE
1 06/24/2016 06/24/2018
1 06/24/2017 06/24/2018
1 06/24/2018 06/24/2018
2 08/08/2017 08/08/2017
2 08/08/2016 08/08/2017
3 12/12/2015 12/12/2015
MAXDATE = CALCULATE(MAX(Table1[Date]);FILTER(Table1;Table1[ID]=EARLIER(Table1[ID])))