How to add/subtract from next row in SQL - sql

How do I take the following table:
and make it so the Amount 3 column subtracts from the remaining amount in the row above?
Basically, I know I can do Amount 1 - Amount 2 to get the difference, but if I have multiple values I am trying to subtract from an original value, how can I write a SQL function so the Amount 2 column is added to the cumulative remaining balance in the above Amount 3 column to have a new cumulative remaining amount?
I'm assuming it's some sort of LAG function, but I still need help.

Related

SQL LAG function

I tried using the LAG function to calculate the value of previous weeks, but there are gaps in the data due to the fact that certain weeks are missing.
This is the table:
The problem is that the LAG functions takes the previous found week in the table. But I would like it to be zero if the previous week is not consecutive previous week.
This is what I would like it to be:
I'm open to any solutions.
Thank you in advance
Your example data is baffling. You have multiple rows per time frame. The first column looks like a string, which doesn't really make sense for the comparison.
So, let me answer based on a simpler data mode. The answer is to use range. If you had an integer column that specified the time frame:
ordering sales
1 10
2 20
3 30
5 50
Then you would phrase this as:
select max(sales) over (order by ordering range between 1 preceding and 1 preceding)
This would return the value from the "previous" row as defined by the first column. The value would be in a separate column, not a separate row.

IIF statement is showing a 0 where I know there is a value

I have a dataset with values that I'm am trying to put into an SSRS report. Here is some example data:
Fund#
last week amount
YTD Total
1
$100
$1500
2
$0
$300
3
$500
$500
4
$0
$0
The first column in my SSRS report is a fixed list of fund names in the certain order that I want them to appear and I want to insert the amounts from the dataset into the row with the corresponding fund name and into the right column.
I tried to get the amount for Fund 1 from last week ($100) to appear in the report using the following expression:
=iif(Fields!fund_no.Value=1,fields!last_wk_amt.Value,0)
However, this is returning a 0 value but I know it should read 100. This seems pretty simple, but I just can't figure it out what I'm doing wrong.
Thanks for the help.
It sounds like your field is an aggregate of the dataset. The IIF statement works on a ROW basis. In your expression, it finds the first row (which is apparently not 1) and returns the evaluation.
I believe it would work correctly if you SUM the IIF:
=SUM(IIF(Fields!fund_no.Value = 1, Fields!last_wk_amt.Value, 0))
If your AMT field is not a decimal value, you'd need to convert the zero to a decimal to avoid an error about aggregating mixed data types:
=SUM(IIF(Fields!fund_no.Value = 1, Fields!last_wk_amt.Value, CDEC(0)))

Counting Datediff between 2 rows from 2 columns

I have a table as below.
Somehow I want to calculate the idle time between End Date and Start Date.
Example: Idle time between 11:38:30 with 11:40:08, 11:49:35 with 12:00:19.
The problem is the station index number 5. It has a null value in 2 columns so I want to calculate base on the previous row
If you only need one value for each station, than grouping will work:
select
StationIndex,
max(Statoin_End_Date) - min(Station_Start_Date) as Duration
from table_name
group by StationIndex
Otherwise please explain logic in greater details.

Trying to create a well count to compare to BOE using the on production date and comparing it to Capital spends and total BOE

I have data that includes the below columns:
Date
Total Capital
Total BOED
On Production Date
UWI
I'm trying to create a well count based on the unique UWI for each On Production Date and graph it against the Total BOED/Total Capital with Date as the x-axis.
I've tried unique count by UWI but it then populates ALL rows of that UWI with the same well count total, so when it is summed the numbers are multiplied by the row count.
Plot Xaxis as Date and Y with Total BOED and Well Count.
Add a calculated column to create a row id using the rowid() function. Then, in the calculation you already have, the one that populates all rows of the UWI with the same well count, add the following logic...
if([rowid] = min([rowid]) over [UWI], uniquecount([UWI]) over [Production Date], null)
This will make it so that the count only populates once.

Use row number in aggregate sum over UNBOUNDED FOLLOWING SQL

I would like to add a discount rate when summing Cashflows over a number of period. To do this I need to multiply each of the remaining cashflows by the discount rate, consummate with this period. I could do this, if I knew the row number of each period, but I can't use it with the window calc I am using. The example below shows the column 'Remaining Interest' which is what I am trying to calculate based on raw data of period and interest.
select Period,RemainingInterest = SUM(PeriodInterestPaid)
OVER (PARTITION BY Name ORDER BY period ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
FROM CF A
Period Interest Remaining Interest(Query) Remaining Interest(Required)
1 1000 1000+2000 1000/1.02^1+2000/1.02^2
2 2000 2000 2000/1.02^1
hi i hope i understand Well ---
you need to get the sum of value based on the period that what i under stand from the query but u said that you need a multiply
So there's no need to make a window function just group by
select Period, SUM(PeriodInterestPaid) as RemainingInterest
FROM CF A
and if u want a multiplay you will make group by also but u will use anther exp :
Pls explan what exactly u need