sql DB calculation moving summary‏‏‏‏‏ - sql

I would like to calculate moving summary‏‏‏‏‏:
Total amount:100
first receipt: 20
second receipt: 10
the first row in calculation column is a difference between total amount and the first receipt: 100-20=80
the second row in calculation column is a difference between the first calculated_row and the first receip: 80-10=70
The presentation is supposed to present receipt_amount, balance:
receipt_amount | balance
20 | 80
10 | 70
I'll be glad to use your help
Thanks :-)

You didn't really give us much information about your tables and how they are structured.
I'm assuming that there is an orders table that contains the total_amount and a receipt_table that contains each receipt (as a positive value):
As you also didn't specify your DBMS, this is ANSI SQL:
select sum(amount) over (order by receipt_nr) as running_sum
from (
select total_amount as amount
from orders
where order_no = 1
union all
select -1 * receipt_amount
from the_receipt_table
where order_no =
) t

First of all- thanks for your response.
I work with Cache DB which can be used both SQL and ORACLE syntax.
Basically, the data is locaed in two different tables, but I have them in one join query.
Couple of rows with different receipt amounts and each row (receipt) has the same total amount.
Foe example:
Receipt_no Receipt_amount Total_amount Balance
1 20 100 80
1 10 100 70
1 30 100 40
2 20 50 30
2 10 50 20
So, the calculation is supposed to be in a way that in the first receipt the difference calculation is made from the total_amount and all other receipts (in the same receipt_no) are being reduced from the balance
Thanks!

Related

sharing cash with priority to creditors in sql

I have a table in sql 2014 with name "tblPaymentPlan" like this:
Creditors PlanToPay َAmount
----------------------------------
A 2017-01-20 2000
A 2017-02-20 1500
A 2017-03-20 3000
B 2017-01-25 3000
B 2017-02-25 1000
and also another table with name "tblPaid" like following:
Creditors Paid َ
-----------------
A 4500
B 3500
and the result that I expect:
Creditors PlanToPay َRemain
----------------------------------
A 2017-01-20 0
A 2017-02-20 0
A 2017-03-20 2000
B 2017-01-25 0
B 2017-02-25 500
I have no idea for doing this job at all! Would you please to help me to perform this job. Please informed that I have a lot of records in my tables.
I need this query for budget planing. (We can use numbers for defining priority instead of dates)
What you want is a running total of what is owing, from that you can subtract what has been paid.
SELECT Creditors, PlanToPay, IIF(ABS(Remain)!=Remain,0,IIF(Remain<Amount,Remain,Amount)) as Remain
FROM (SELECT pp.Creditors, pp.PlanToPay, pp.Amount,
SUM(pp.Amount) OVER(PARTITION BY pp.Creditors ORDER BY pp.PlanToPay ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)-tp.paid AS Remain
FROM tblPaymentPlan pp
JOIN (SELECT creditors, sum(paid) as paid from tblpaid group by creditors) tp
ON pp.creditors = tp.creditors) ss
ORDER By Creditors, PlanToPay
SQLFiddle
In the windowing function (SUM OVER) the PARTITION separates the creditors, the ORDER determines how the rows are arranged (by date), and the ROWS clause tells it to use all the rows in the partition before this row and include this row in the running total. We then subtract the sum of everything paid to that creditor from this running total.
This of course gives us alot of negative numbers, so we do it in a subquery. The main query checks if the absolute value of that remaining is equal to the value, true if it's positive, false if it is not, and returns the value remaining if true, or 0 if not.
UPDATE - added handling for multiple rows with value still owing
You can subtract the running total from amount in paid table and if it is less than 0, set remain to 0 else the difference of amount from the running total.
select pp.creditors,pp.plantopay,
case when sum(pp.amount) over(partition by pp.creditors order by pp.plantopay)-coalesce(pd.paid,0) <= 0 then 0
else sum(pp.amount) over(partition by pp.creditors order by pp.plantopay)-coalesce(pd.paid,0) end as remain
from tblpaymentplan pp
left join tblPaid pd on pp.creditors=pd.creditors

Replicate the effect of the first. function in SAS within SQL

What is the simplest way (in terms of easily understandable code, not code length or efficiency) to replicate the effect of the first. function in SAS?
Select sum(amount), order_id,
From tablename
Group By order_id
/* Pseudo code below */
Having first.amount = $100
Order by date
What I'm trying to do above is to get the total amount for each order but exclude all order_id for which the first transaction date has an amount > 100. If there are multiple amounts for the same date, then only 1 needs to fit the criteria for the whole order to be removed.
For example, the following orders should be removed
Order_ID Date Amount
1 1/1 45
1 1/1 100
1 1/2 32
2 1/1 100
The following orders should NOT be removed
Order_ID Date Amount
3 1/1 99.99
3 1/2 100
4 1/1 9
4 1/2 100
I see. You are referring to the first variables in a by-processing loop. Your question is pretty unclear.
This is a bit complicated, because the "first" appears to be needed after the aggregation, but it only applies to the first date. I'm not sure what this code would look like in SAS, but the following should have the same effect SQL:
select order_id, sum(amount)
from t
where (select top 1 sum(amount) as first_amount
from t
where t2.order_id = t.order_id
group by date
order by date
) < 100
group by order_id;

How can I make this query recursive Sql Server?

I have this table structure for Balances table:
And this is the view:
I have also this structure for Amounts table:
This is the view mode for Amounts table:
First of all I need to get the amount value for a specific day in Amounts Table:
with this query I get the amount 300 in date 07/07/2016.
Once achieved this figure, I need to make a recursive query with Balances table.
The end result should be like this:
Name abstractAmount addAmount Balance
----- -------------- --------- -------
Josep 100 400
Maria 50 350
George 60 410
Julianne 25 385
what is this? This result is achieved taking the 300 from the Amounts table, and for each row in Balance table I see:
If the abstracAmount in the first row is not empty, I make this mathematical calculation: balance = (300 - abstractAmount), in case is empty and the addAmount column has values I make this mathematical calculation balance = (300 + addAmount)
In the rest of rows I do the same but the calculation is not on 300, is on the last row balance:
For example:
In the first row the balance is 400 because the addamount has value so I make this calculation : 300 + 100 = 400
In the second row the balance is 350 because the abstractAmount is not empty so I take the balance value for the last row and make this calculation : 400 - 50 = 350.
And the same thing for the rest of rows, only the first row takes the balance value for the amounts table.
Notes:
1. Always the column abstractAmount subtracts values, and the addAmount column sum values.
Always one of this columns (abstractAmount | addAmount) will be empty .
Only the first row takes the value to make the mathematical calculation for the Amounts table, the rest of rows takes the value for the row before.
How can I get this final result? :
Name abstractAmount addAmount Balance
----- -------------- --------- -------
Josep 100 400
Maria 50 350
George 60 410
Julianne 25 385
I accept suggestions, thanks.
Instead of recursion, you can use window functions. More specifically a sum over rows unbounded preceding to get a running total (+ the start balance):
select *,300 + sum(isnull(addAmount,0) - ISNULL(abstractAmount,0)) over (order by id rows unbounded preceding) Balance
from Balances
The isnull(addAmount,0) - ISNULL(abstractAmount,0) is simply the mutation for every row. The over (order by id rows unbounded preceding) scopes the sum to the current row and all preceding rows according to id.
To get the base from the amounts table, you can have simply have the (select ...where date..) as a value instead of '300' or a bit more nifty: with a cross join to the amounts table:
select b.*, a.dateInsertion,a.amount, a.amount + sum(isnull(addAmount,0) - ISNULL(abstractAmount,0)) over (order by b.id rows unbounded preceding) Balance
from Balances b
cross join Amounts a
where a.dateInsertion = '20160707'
With the cross join without the where, you would get all possible balances

osclass Counting number of Sales of specifed id products

I have this table item_log:
fk_i_item_id product amount currency status
$sales = $conn->osc_dbFetchResult("SELECT COUNT(status) FROM %st_item_log WHERE fk_i_item_id = '%s' AND status = 'COMPLETED'", DB_TABLE_PREFIX,osc_logged_user_id());
echo $sales['COUNT(status)']
I want to count total number of sales of an product in the same echo.
ex
***********************************************************
fk_i_item_id product amount currency status
***********************************************************
20 Book 1 10 USD COMPLETED
21 Book 2 12 USD COMPLETED
20 Book 1 10 USD COMPLETED
23 Book 3 11 USD COMPLETED
I want an query to count the number or records.
*******************************************************************
fk_i_item_id product amount currency status Sales
*******************************************************************
20 Book 1 10 USD COMPLETED 2
21 Book 2 12 USD COMPLETED 1
23 Book 3 11 USD COMPLETED 1
You already appear to know how to use aggregate functions such as count(), albeit over the entire table.
It's also possible to use these aggregate functions over groups of rows within the table. For example, to get the number of rows for each item ID, it's a simple matter of:
select item_id,
count(*)
from some_table
group by item_id
So all you really need to do is use group by to specify a finer level of grouping since, without that, it will group over all the selected rows.
With regard to your desired output, it would probably make more sense to sum the amount (so that id 20 has an amount of 20 rather than 10) though this may well be complicated by the fact you appear to allow multiple currencies.
I'm not going to tell you how to fix that other than to state that you may need to convert the amounts to a baseline currency before summing them.
So, as a first cut, I'd be looking at something like:
select fk_i_item_id,
max(product) as product,
sum(amount) as amount,
max(currency) as currency,
max(status) as status,
count(*) as sales
from st_item_log
where status = 'COMPLETED'
group by fk_i_item_id
Note the use of the "dummy" max aggregate function on the fields you're not grouping by. This is to ensure that the rows still group on the item ID even if they have different product descriptions, currencies, or status values (though the last is impossible given the where clause).

SQL query to choose row before two consecutive true values

Here is my table.
Price Volume
------------------
60 0
70 10
80 0
90 0
100 40
200 40
300 40
400 0
500 50
600 60
It is ordered by Price.
I need to choose all rows before two consecutive zeros in Volume column. So the result should look like
Price Volume
------------------
100 40
200 40
300 40
400 0
500 50
600 60
I really do not know where to start with this type of query. I could only think go pulling data into C# and the loading back into table.
Regards,
You didn't state your DBMS so this is ANSI SQL:
with flagged as (
select price,
volume,
case
when volume + lag(volume) over (order by price) = 0 then 1
else 0
end as rn
from prices
), counted as (
select price,
volume,
sum(rn) over (order by price) as cn,
rn
from flagged
)
select price, volume
from counted
where cn > 0 and rn = 0
order by price
Here is a SQLFiddle example: http://sqlfiddle.com/#!6/b2434/4
Standard SQL cannot handle this. You need a cursor to cycle through the ordered rows and decide what to output, which requires the use of some variables.
Depending on the RDBMS you are using, you could write a stored procedure that reads the rows and filters them. Otherwise you must have the application do the filtering.
In sql server 2012+ you can do this with a couple LAG statements.
Lag for the 2 previous values and where when they are both 0.