Select next subsequent change of certain column in a new column - sql

I have a table with a unique index on Contracts of Customers that live in Houses. I want to know the days per house how long it takes when someone moves out (Contract end date) and a new contracts starts. For that I want to know what the first next contract will be in that house, but on the same row as the old contract for a (potentially different) customer.
This how the table currently looks like, I select the top 10 here:
SELECT TOP 10
PMCCONTRACT.ACCOUNTNUM --Customer
,PMCCONTRACT.RENTALOBJECTID --House
,PMCCONTRACT.CONTRACTID --Contract & Unique index of the table
,PMCCONTRACT.VALIDFROM --Contract Start Date
,PMCCONTRACT.VALIDTO --Contract End Date
FROM PMCCONTRACT
Then this rolls out:
ACCOUNTNUM RENTALOBJECTID CONTRACTID VALIDFROM VALIDTO
101852 2488 HC000001 1994-03-01 00:00:00.000 NULL
101136 2489 HC000002 1920-01-01 00:00:00.000 NULL
101352 2491 HC000003 1996-09-16 00:00:00.000 NULL
100687 2492 HC000004 1984-11-01 00:00:00.000 NULL
105160 2499 HC000005 1975-05-02 00:00:00.000 2018-01-31 00:00:00.000
102821 2501 HC000006 1997-09-16 00:00:00.000 NULL
100731 2506 HC000007 1920-01-01 00:00:00.000 2018-11-15 00:00:00.000
102797 2508 HC000008 1998-02-01 00:00:00.000 NULL
102155 2512 HC000009 1981-09-01 00:00:00.000 NULL
102563 2515 HC000010 1965-10-17 00:00:00.000 2017-06-30 00:00:00.000
And what I want is that based on the RENTALOBJECTID it will show what the First Next contract on that house was (so it is important that the CONTRACTID remains unique in this table).
Below is the code I use to get it, however, it shows all the following contract changes for that specific RENTALOBJECTID (House).
SELECT --TOP 1000
PMCCONTRACT.CONTRACTID
,PMCCONTRACT.RENTALOBJECTID
,PMCCONTRACT.VALIDFROM
,PMCCONTRACT.VALIDTO
,P2.CONTRACTID AS 'FirstNextContractId'
,P2.VALIDFROM
,P2.VALIDTO
FROM PMCCONTRACT
LEFT JOIN PMCCONTRACT P2
ON PMCCONTRACT.RENTALOBJECTID = P2.RENTALOBJECTID
LEFT JOIN
(SELECT
RENTALOBJECTID,
MAX(CONTRACTID) AS CONTRACTID
FROM PMCCONTRACT
GROUP BY RENTALOBJECTID) X ON X.CONTRACTID = P2.CONTRACTID
WHERE P2.VALIDFROM > PMCCONTRACT.VALIDTO
This is what I get when I select only ContractID HC000028, it shows 2 rows, while I want it to show only the first row.
CONTRACTID RENTALOBJECTID VALIDFROM VALIDTO FirstNextContractId VALIDFROM2 VALIDTO2
HC000028 75 1995-01-01 00:00:00.000 2016-04-30 00:00:00.000 HC009990 2016-05-01 00:00:00.000 2018-11-25 00:00:00.000 --<< Only row I want to show
HC000028 75 1995-01-01 00:00:00.000 2016-04-30 00:00:00.000 HC025218 2018-11-26 00:00:00.000 1900-01-01 00:00:00.000 --Too far in the future
Kind regards,
Igor

It looks like a simple LEAD window function is enough. It returns the next row, as defined by partitioning and ordering clauses.
SELECT TOP 10
PMCCONTRACT.ACCOUNTNUM --Customer
,PMCCONTRACT.RENTALOBJECTID --House
,PMCCONTRACT.CONTRACTID --Contract & Unique index of the table
,PMCCONTRACT.VALIDFROM --Contract Start Date
,PMCCONTRACT.VALIDTO --Contract End Date
,LEAD(CONTRACTID) OVER (PARTITION BY RENTALOBJECTID ORDER BY VALIDFROM) AS NextContractID
,LEAD(VALIDFROM) OVER (PARTITION BY RENTALOBJECTID ORDER BY VALIDFROM) AS NextVALIDFROM
,LEAD(VALIDTO) OVER (PARTITION BY RENTALOBJECTID ORDER BY VALIDFROM) AS NextVALIDTO
FROM PMCCONTRACT
;

Related

Create interval from discrete dates

I have a function which saves the current status of several objects and writes it in a table, which looks like something like this:
ObjectId StatusId Date
1 10 2020-04-04 00:00:00.000
2 10 2020-04-04 00:00:00.000
1 11 2020-04-05 00:00:00.000
2 10 2020-04-05 00:00:00.000
1 10 2020-04-06 00:00:00.000
2 10 2020-04-06 00:00:00.000
I would like to make it an interval grouped by ObjectId and StatusId.
So for the above the preferred output would look like this:
ObjectId StatusId StartDate EndDate
1 10 2020-04-04 00:00:00.000 2020-04-04 00:00:00.000
1 11 2020-04-05 00:00:00.000 2020-04-05 00:00:00.000
1 10 2020-04-06 00:00:00.000 2020-04-06 00:00:00.000
2 10 2020-04-04 00:00:00.000 2020-04-06 00:00:00.000
Note one object can have the same status on multiple occasions but if it had a different status it needs to be in a separate interval. So simple group by and max(Date) doesn't work in my case.
Thanks in advance.
This is a form of gaps-and-islands. For this purpose, the difference of row numbers is probably the simplest method:
select objectid, status, min(date), max(date)
from (select t.*,
row_number() over (partition by objectid order by date) as seqnum,
row_number() over (partition by objectid, status order by date) as seqnum_2
from t
) t
group by objectid, status, (seqnum - seqnum_2);
Why this works can be a little cumbersome to explain. However, if you look at the results of the subquery, you will see how the difference is constant for the groups you want to identify.

create a row index column beginning at -1

I need to create a row index column that begins at -1 so i can query the previous day's balance. My current query:
select TRANSDATE, sum(convert(float,AMOUNTMST-SETTLEAMOUNTMST)) as Balance
from [AX2cTestStage].[dbo].[CUSTTRANS_V]
group by TRANSDATE
order by TRANSDATE asc
TRANSDATE Balance
2019-04-12 00:00:00.000 -22591.47
2019-04-15 00:00:00.000 -394.95
2019-04-25 00:00:00.000 -1776
2019-04-26 00:00:00.000 -11973.84
2019-04-29 00:00:00.000 -24230.16
2019-05-02 00:00:00.000 -10695.39
This is what i need:
TRANSDATE Balance Row Index
2019-04-12 00:00:00.000 -22591.47 -1
2019-04-15 00:00:00.000 -394.95 0
2019-04-25 00:00:00.000 -1776 1
2019-04-26 00:00:00.000 -11973.84 2
2019-04-29 00:00:00.000 -24230.16 3
2019-05-02 00:00:00.000 -10695.39 4
I have tried to declare a variable as the row index
declare #row_num as int = -1
select TRANSDATE, sum(convert(float,AMOUNTMST-SETTLEAMOUNTMST)) as Balance, #row_num += 1 as Row Index
from [AX2cTestStage].[dbo].[CUSTTRANS_V]
group by TRANSDATE
i receive this error:
A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.
after declaring a variable for each field I still receives errors. Is there an easier way to accomplish this? thanks
You can use ROW_NUMBER(). For example:
select
TRANSDATE,
sum(convert(float,AMOUNTMST-SETTLEAMOUNTMST)) as Balance,
row_number() over(order by TRANSDATE) - 2 as Row Index
from [AX2cTestStage].[dbo].[CUSTTRANS_V]
group by TRANSDATE

Subtracting from different rows

I am trying to subtract the startdate from the enddate on different rows, but only for the same code.
For example:
I want to do startdate in row 2 for C002 (2012-07-01) minus enddate in row 1 for C002 (2012-06-30).
The result should be 1 (day) for row 2. No data should be in row 1.
Row 4 should show 1 (day) as well.
How can I go about doing this?
row code startdate enddate
1 C002 2011-07-01 00:00:00.000 2012-06-30 00:00:00.000
2 C002 2012-07-01 00:00:00.000 2013-06-30 00:00:00.000
3 C003 2011-07-01 00:00:00.000 2012-06-30 00:00:00.000
4 C003 2012-07-01 00:00:00.000 2013-06-30 00:00:00.000
select max(row),code,datediff(day,max(startdate),min(enddate)) as ouputtt
from table
group by
code
Try this-
Select x.code, y.startdate-x.enddate
From table1 x left outer join table1 y on
X.code=Y.code
Where
X.enddate<y.startdate
You could use this query, which adds the requested value as an additional column:
select row, code, startdate, enddate,
datediff('d', lag(enddate) over (partition by code order by row1), startdate) df
from mytable

sql-query that change all validTo dates to the next validFrom date minus one Day

I have to modify a big pricelist table so that there is only one valid price for every article.
Sometimes the sales employees insert new prices and forgot to change the old infinite validTo dates.
So I have to write a sql-query to change all validTo dates to the next validFrom date minus one day, when the validTo date has infinite validity (9999-12-31).
But I have no idea how can i reach this with only SQL (Oracle 12).
anr price validFrom validTo
1 447.1 2015-06-01 9999-12-31 <
1 447.2 2015-06-16 2015-06-16
1 447.3 2015-06-17 2015-06-17
1 447.4 2015-06-22 2015-06-22
1 447.5 2015-07-06 9999-12-31 <
1 395.0 2015-07-20 2015-07-20
1 447.6 2015-08-03 9999-12-31 <
1 447.7 2015-08-17 9999-12-31 <
1 447.8 2015-08-24 9999-12-31 <
1 395.0 2015-09-07 2015-09-07
1 450.9 2015-11-15 9999-12-31 < no change because it is the last entry
after updating the the table, the result should look like
anr price validFrom validTo
1 447.1 2015-06-01 2015-06-15 <
1 447.2 2015-06-16 2015-06-16
1 447.3 2015-06-17 2015-06-17
1 447.4 2015-06-22 2015-06-22
1 447.5 2015-07-06 2015-07-19 <
1 395.0 2015-07-20 2015-07-20
1 447.6 2015-08-03 2015-08-16 <
1 447.7 2015-08-17 2015-08-23 <
1 447.8 2015-08-24 2015-09-06 <
1 395.0 2015-09-07 2015-09-07
1 450.9 2015-11-15 9999-12-31 <
In order to update an end date you can simply select the minimum of all higher start dates.
update mytable upd
set enddate = coalesce(
(
select min(startdate) - 1
from mytable later
where later.startdate > upd.startdate
and later.anr = upd.anr -- same product
), date'9999-12-31') -- coalesce for the case there is no later record
where enddate = date'9999-12-31';
I have taken anr to be the product id. If it isn't then change the statement accordingly.
Oracle provides an analytic function LEAD that references the current-plus-n-th record given a sort criterion. This function may serve the purpose of selecting the proper date value in an update statement as follows ( let test_prices be the table name, ppk its PK ):
update test_prices p
set p.validTo = (
select ps.vtn
from (
select lead ( p1.validFrom, 1 ) over ( order by p1.validFrom ) - 1 vtn
, ppk
from test_prices p1
) ps
where ps.ppk = p.ppk
)
where to_char(p.validTo, 'YYYY') = '9999'
and p.validFrom != ( select max(validFrom) from test_prices )
;
UPDATE VALID_DATES v
SET validTo = (
SELECT validTo
FROM (
SELECT anr,
validFrom,
COALESCE(
LEAD( validFrom - 1, 1 ) OVER ( PARTITION BY anr ORDER BY validFrom ),
validTo
) AS validTo
FROM valid_dates
) u
WHERE v.anr = u.anr
AND v.validFrom = u.validFrom
)
WHERE validTo = DATE '9999-12-31';
There are two possibilities:
1. Explicit time spans
price validFrom validTo
90.99 2016-01-01 9999-12-31
80.00 2016-01-16 2016-01-17
The first price would be valid both before January 16 and after January 17, whereas the second price was only valid on two days in January.
It would then be a very bad idea to change the first validTo.
2. Implicit time spans
price validFrom
90.99 2016-01-01
80.00 2016-01-16
90.99 2016-01-18
This data represents the same as in the explicit time spans example. The first price is valid before January 16, then the second price is valid until January 17, and afterwards the next price (which equals the first price again) is valid. Here you don't need an EndDate, because it's implicit. Of course the first price is only valid until January 15, because from January 16 there is another price valid (record #2).
So: Either remove the EndDate column completely or let it untouched. Don't simply update it, as you have intended. If you updated your records to next date minus one, you would actually hold data redundantly, which might lead to problems later.

How to make a status by comparing the dates

Using SQL Server 2005
Leave Table
ID StartDate EndDate
001 04/01/2010 04/02/2010
002 04/02/2010 04/03/2010
…
Event Table
ID Date PresentDate Status
001 03/30/2010 03/30/2010 Present
001 03/31/2010 null absent
001 04/01/2010 null Leave
001 04/02/2010 null Leave
001 04/03/2010 null absent
001 04/04/2010 04/04/2010 Present
….
All the Datecolumn datatype is datetime
In the Status Column, if Present Date is null then it will display as “absent”, if not null then it will display as “present”. Now if we apply a leave for the date then it will display as “Leave” in status column.
Query
Select
id, date, present date
, CASE WHEN t2.id IS NULL THEN t1.Status ELSE ‘Leave’ END AS status
from event table as t1
left outer join leave table as t2 on
t1.id = t2.id and t1.date between t2.startdate and t2.enddate
The above method is working, but I need to add one more condition.
Once if we applied the leave for the particular employee in the Leave Table then it should compare the Present Date column, if Present Date Column is empty then it should display as “leave”
Expected Output
ID Date PresentDate Status
001 03/30/2010 03/30/2010 Present
001 03/31/2010 null absent
001 04/01/2010 null Leave
001 04/02/2010 null Leave
001 04/03/2010 null Leave (Expect this value)
001 04/04/2010 04/04/2010 Present
….
From the above output Leave is starting from 04/01/2010 to 04/02/2010, then next column of present date is null then status should display as a “Leave”, once present date is not null then it should display as “Present.
Method
We can display as "Leave" in status column from Start Date to end date of leave table, after that leave date end then we can compare with PresentDate column, if PresentDate column is null then it should display as "Leave", once data is available in present column then status should display with normal condition.
How to make a query for the above condition.
Need Query Help
select E.id, E.date, E.presentdate, *,
case
when E.presentdate is not null then 'Present'
when E2.presentdate is not null then 'Absent'
when L.ID is not null then 'Leave'
else 'Absent'
end
from Event E
outer apply (
select top 1 *
from Leave L
where E.presentdate is null and E.date >= L.startdate
AND e.ID = L.ID
order by L.startDate desc) L
outer apply (
select top 1 *
from Event E2
where E.presentdate is null
and E2.presentdate is not null
and E.date >= E2.date and E2.date > L.startdate
AND e2.ID = e.ID
order by E2.presentdate desc) E2
order by E.date
Leave table
ID StartDate EndDate
----------- ----------------------- -----------------------
1 2010-04-01 00:00:00.000 2010-04-02 00:00:00.000
1 2010-04-02 00:00:00.000 2010-04-03 00:00:00.000
1 2010-04-05 00:00:00.000 2010-04-05 00:00:00.000
Output
id date presentdate
----------- ----------------------- ----------------------- -------
1 2010-03-30 00:00:00.000 2010-03-30 00:00:00.000 Present
1 2010-03-31 00:00:00.000 NULL Absent
1 2010-04-01 00:00:00.000 NULL Leave
1 2010-04-02 00:00:00.000 NULL Leave
1 2010-04-03 00:00:00.000 NULL Leave -**
1 2010-04-04 00:00:00.000 2010-04-04 00:00:00.000 Present
1 2010-04-05 00:00:00.000 NULL Leave
1 2010-04-06 00:00:00.000 NULL Leave -**
1 2010-04-07 00:00:00.000 NULL Leave -**
1 2010-04-08 00:00:00.000 2010-04-08 00:00:00.000 Present
1 2010-04-09 00:00:00.000 NULL Absent
1 2010-04-10 00:00:00.000 NULL Absent
1 2010-04-11 00:00:00.000 2010-04-11 00:00:00.000 Present
The ones marked -** are not covered by Leave records, but they show leave because they follow a Leave period, correct? 2010-04-09 for example remains "Absent" because it follows a Present record (without actually being present).