Home > Blockchain >  Subtract value of a column from previous value of another column in sql
Subtract value of a column from previous value of another column in sql

Time:03-02

I want to have column where the value of a subtraction will get stored. Subtraction should happen between value of a column and previous value of another column.

Below is the example:

Customer ID Ship date Supplydate Gap
76868773 10/15/2018 0:00 12/10/2018 0:00 -4
76868773 12/06/2018 0:00 01/31/2019 0:00 21
76868773 02/21/2019 0:00 04/18/2019 0:00 7
76868773 04/25/2019 0:00 06/20/2019 0:00 7
76868773 06/27/2019 0:00 08/22/2019 0:00 7
76868773 08/29/2019 0:00 10/24/2019 0:00 33
76868773 11/26/2019 0:00 01/21/2020 0:00 9
76868773 01/30/2020 0:00 03/26/2020 0:00 11
76868773 04/06/2020 0:00 06/01/2020 0:00 0
76868773 06/01/2020 0:00 07/27/2020 0:00 2
76868773 07/29/2020 0:00 09/23/2020 0:00 8

Here we have customer ID, for each customer ID the gap should be calculated like shipdate-previous supplydate.. just like 2nd ship date 12/6/2018 minus previous supply date that is 12/10/2018, so gap should be -4.

You can refer to the above link to get the sample data..

CodePudding user response:

You can use lead() windows function. Assuming that you are using SQL Server.

Create table and insert statements:

 create table customer_shipping(Customer_ID int, Ship_date date, Supply_date date);
 insert into customer_shipping values(76868773  ,'10/15/2018 0:00','12/10/2018 0:00');
 insert into customer_shipping values(76868773  ,'12/06/2018 0:00','01/31/2019 0:00');
 insert into customer_shipping values(76868773  ,'02/21/2019 0:00','04/18/2019 0:00');
 insert into customer_shipping values(76868773  ,'04/25/2019 0:00','06/20/2019 0:00');
 insert into customer_shipping values(76868773  ,'06/27/2019 0:00','08/22/2019 0:00');
 insert into customer_shipping values(76868773  ,'08/29/2019 0:00','10/24/2019 0:00');
 insert into customer_shipping values(76868773  ,'11/26/2019 0:00','01/21/2020 0:00');
 insert into customer_shipping values(76868773  ,'01/30/2020 0:00','03/26/2020 0:00');
 insert into customer_shipping values(76868773  ,'04/06/2020 0:00','06/01/2020 0:00');
 insert into customer_shipping values(76868773  ,'06/01/2020 0:00','07/27/2020 0:00');
 insert into customer_shipping values(76868773  ,'07/29/2020 0:00','09/23/2020 0:00');

Query:

 select *, datediff(day,supply_date,lead(ship_date)over(partition by customer_id order by ship_date))gap 
 from customer_shipping

Output:

Customer_ID Ship_date Supply_date gap
76868773 2018-10-15 2018-12-10 -4
76868773 2018-12-06 2019-01-31 21
76868773 2019-02-21 2019-04-18 7
76868773 2019-04-25 2019-06-20 7
76868773 2019-06-27 2019-08-22 7
76868773 2019-08-29 2019-10-24 33
76868773 2019-11-26 2020-01-21 9
76868773 2020-01-30 2020-03-26 11
76868773 2020-04-06 2020-06-01 0
76868773 2020-06-01 2020-07-27 2
76868773 2020-07-29 2020-09-23 null

db<>fiddle here

CodePudding user response:

If your database doesn't support LEAD and LAG (earlier versions of MS SQL don't) then you can always use the RANK() OVER() function to give each a unique RANK number and then join the table to itself using an offset on the RANK of 1

  •  Tags:  
  • sql
  • Related