query Get Balances in Oracle table stock - sql

i have sample data in oracle data table namely STOKREWARD table, i wanna get balance between quantity IN and quantity OUT.
i wanna get value BALANCES like this:
i try using query like this, but the result doesn't i want
SELECT STOKREWARD.NO, STOKREWARD.DODATE, STOKREWARD.REWARDNAME, STOKREWARD.NOTES,
STOKREWARD.QTYIN, STOKREWARD.QTYOUT,
(STOKREWARD.QTYIN- STOKREWARD.QTYOUT) AS BALANCES
FROM STOKREWARD
ORDER BY STOKREWARD.NO ASC
this query gives result:
anyone can help me? thanks

Using SUM() OVER:
SELECT NO, DODATE, CODE, REWARDNAME, NOTES, QTYIN, QTYOUT,
SUM (QTYIN - QTYOUT) OVER (ORDER BY DODATE ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS BALANCES
FROM STOKREWARD

Related

Find sum based on another column and use aggregate function in SQL

I am working on SQL and I want to achieve this output in SQL using HIVE, here is the problem statement
I have columns Item, Cost and price.
The output I am expecting to have a following columns
25% cost- Which is the 25% value of cost.
total_price- Its a sum of price values based on Item.
cumulative_price-Based on item it's a sum of cumulative price.
I want to flag those records which has cumulative_price>25%cost value
this code will work on sql so try it.
with cte as
(select *,(cost*0.25) as costx,
sum(price) over (PARTITION BY item order by item) AS total_price,
sum(price) over (PARTITION BY item order by item,price) cumulative_price from dept)
select *,case when cumulative_price<costx then 0 else 1 end as flag from cte;
its work on me.

Hive query to get count from previous day

Table A
Year(String), Month(String), and Day(String) are partition columns.
I want to get the count for the previous day.
Note - here no Date formate like 2022-06-01 in any columns
I tried the below query.
Select count(*) FROM Table A where Day='03' and Month='06' and Year='2022' GROUP BY city;
But I don't want to hard-coded value.
I think I got the solution -
select count(*) from Table A where cast(concat_ws('-',year,month,day) as date)=date_sub(current_date, 1);
this is giving me the correct results.

How to get correct cumulative balance?

I have this simple query
select ArticleID, Prix, Qte, InfStock
, SUM(Qte*InfStock) OVER (Partition BY ArticleID ORDER BY DateDocument) AS CUMUL
FROM Balance
Please look the result (Line 4)
Here is the backup file(zipped)
You need to add to the end of the OVER clause ROWS UNBOUNDED PRECEDING.
SUM defaults to RANGE UNBOUNDED PRECEDING which can cause issues like this.
See here for example, for further explanation.

Finding the Total transactions done by an user in each month

There is a user table(usr) and a transactions(txn) table .The transactions table contains the following fields :- Usr_id, tscn_id,tscn_amt,tscn,date.
I am not able to write the SQL query to find the total transaction amount for each user calculated for each month. Could someone please help me with this ?
Can you try the following:
SELECT
sum(tscn_amt) as total,
Usr_id,to_char(date,'MM') as month
FROM txn
GROUP BY Usr_id, to_char(date,'MM');
You can try something like this :
select sum(tscn_amt) as sum,Usr_id,month(date) from `txn` group by Usr_id, month(date)
Check out the date and time functions in MySQL.

analyze range and if true tell me

I want to see if the price of a stock has changed by 5% this week. I have data that captures the price everyday. I can get the rows from the last 7 days by doing the following:
select price from data where date(capture_timestamp)>date(current_timestamp)-7;
But then how do I analyze that and see if the price has increased or decreased 5%? Is it possible to do all this with one sql statement? I would like to be able to then insert any results of it into a new table but I just want to focus on it printing out in the shell first.
Thanks.
It seems odd to have only one stock in a table called data. What you need to do is bring the two rows together for last week's and today's values, as in the following query:
select d.price
from data d cross join
data dprev
where cast(d.capture_timestamp as date = date(current_timestamp) and
cast(dprev.capture_timestamp as date) )= cast(current_timestamp as date)-7 and
d.price > dprev.price * 1.05
If the data table contains the stock ticker, the cross join would be an equijoin.
You may be able to use query from the following subquery for whatever calculations you want to do. This is assuming one record per day. The 7 preceding rows is literal.
SELECT ticker, price, capture_ts
,MIN(price) OVER (PARTITION BY ticker ORDER BY capture_ts ROWS BETWEEN 7 PRECEDING AND CURRENT ROW) AS min_prev_7_records
,MAX(price) OVER (PARTITION BY ticker ORDER BY capture_ts ROWS BETWEEN 7 PRECEDING AND CURRENT ROW) AS max_prev_7_records
FROM data