Sql function for statement - sql

I have table below:
Now I need the table like:
Here:If TransType='Deposit' then it need in Debit and if TransType='Withdraw' then it need to be in Credit, And Debit-Credit from first to Last as Statement(Balance). Can you give any solution?
Please ask me for further clarification.

The balance is a bit tricky. The rest is a simple case:
select t.*,
(case when TransType = 'Deposit' then amount end) as credit,
(case when TransType = 'Withdraw' then amount end) as debit,
sum(case when TransType = 'Deposit' then amount
when TransType = 'Withdraw' then - amount
end) over (partition by BankName order by TransDate
) as balance
from t;

As far as I am getting the question you need 2 queries with 'where' clause.
For exemple "Select BankName, TransDate, TransMiti, Description from TableName where TransType = "Deposit""
same goes for WithDraw.
Here is the link to to learn more.
https://www.w3schools.com/sql/sql_where.asp

Related

Advanced SQL with window function

I have Table a(Dimension table) and Table B(Fact table) stores transaction shopper history.
Table a : shopped id(surrogate key) created for unique combination(any of column 2,colum3,column4 repeated it will have same shopper id)
Table b is transaction data.
I am trying to identify New customers and repeated customers for each week, expected output is below.
I am thinking following SQL Statement
Select COUNT(*) OVER (PARTITION BY shopperid,weekdate) as total_new_shopperid for Repeated customer,
for Identifying new customer(ie unique) in same join condition, I am stuck on window function..
thanks,
Sam
You can use the DENSE_RANK analytical function along with aggregate function as follows:
SELECT WEEK_DATE,
COUNT(DISTINCT CASE WHEN DR = 1 THEN SHOPPER_ID END) AS TOTAL_NEW_CUSTOMER,
SUM(CASE WHEN DR = 1 THEN AMOUNT END) AS TOTAL_NEW_CUSTOMER_AMT,
COUNT(DISTINCT CASE WHEN DR > 1 THEN SHOPPER_ID END) AS TOTAL_REPEATED_CUSTOMER,
SUM(CASE WHEN DR > 1 THEN AMOUNT END) AS TOTAL_REPEATED_CUSTOMER_AMT
FROM
(
select T.*,
DENSE_RANK() OVER (PARTITION BY SHOPPER_ID ORDER BY WEEK_DATE) AS DR
FROM YOUR_TABLE T);
GROUP BY WEEK_DATE;
Cheers!!
Tejash's answer is fine (and I'm upvoting it).
However, Oracle is quite efficient with aggregation, so two levels of aggregation might have better performance (depending on the data):
select week_date,
sum(case when min_week_date = week_date then 1 else 0 end) as new_shoppers,
sum(case when min_week_date = week_date then amount else 0 end) as new_shopper_amount,
sum(case when min_week_date > week_date then 1 else 0 end) as returning_shoppers,
sum(case when min_week_date > week_date then amount else 0 end) as returning_amount
from (select shopper_id, week_date,
sum(amount) as amount,
min(week_date) over (partition by shopper_id) as min_week_date
from t
group by shopper_id, week_date
) sw
group by week_date
order by week_date;
Note: If this has better performance, it is probably due to the elimination of count(distinct).

multiple where statements in proc sql

I'm not a frequent SQL user so be patient:) What I'm trying to do is create multiple summary columns using SQL proc; I know the below is incorrect as I can't use where statement like this but how do I rearrange this?
proc sql ;
create table totals as
select
Account_number,
sum(charge) as total_payments,
sum(charge) as total_cash (where transactioncode in ('CASH')),
sum(charge) as total_intt (where transactioncode in ('INTT')),
calculated total_payments/12 as avg_monthly_payments,
calculated total_cash/12 as avg_cash_payments,
calculated total_intt/12 as avg_INTT_payments
from tabe_1
group by Account_number
; quit ;
You can use CASE...WHEN statement, something like this
create table totals as
select
Account_number,
sum(charge) as total_payments,
sum(CASE WHEN transactioncode ='CASH' THEN charge ELSE 0 END) as total_cash,
sum(CASE WHEN transactioncode ='INTT' THEN charge ELSE 0 END) as total_intt,
sum(charge)/12 as avg_monthly_payments,
sum(CASE WHEN transactioncode ='CASH' THEN charge ELSE 0 END)/12 avg_cash_payments,
sum(CASE WHEN transactioncode ='INIT' THEN charge ELSE 0 END)/12 as avg_INTT_payments
from tabe_1
group by Account_number

How to write this SQL query to find each account balance?

I have a transaction table (shown in picture)
https://i.ibb.co/7pdYxxm/hhhhhh.jpg
There's the transaction type (debit/credit)
I need a SQL query that calculates the balance of each account (sum of credits - sum of debts)
So we group by account_id ... but how can we sum the credits alone and the debits alone?
I am on PostgreSQL! Thank you!
This is an easy method to for you to achieve this:
select account_id, sum((case when transaction_type = 'C' then 1 else 0 end)*transaction_amount) as sum_of_credit,sum((case when transaction_type = 'D'then 1 else 0 end) * transaction_amount) as sum_of_debit from YourTableNameHere group by account_id;
Sample Data
Sample Output for the query
I assume the amount must be substracted when the type = 'C'.
select account_id, sum((case when transaction_type = 'C' then -1 else 1 end) * transaction_amount)
from trans
group by account_id
base on the transaction type the amount is multiplied by 1 or -1.
Try This:
Select sum(Transaction_Amount),
Account_Id,
Transaction_Type
From Table_Name
Group By
Account_Id,
Transaction_Type

Subtract two value by difference criteria in same row

In this table we see different transaction in same date some of Transaction Dr and some transaction Cr. I need Cr - Dr as Transaction Amount per date.
Please see screenshot 1st Table
Result will be-
Result
you could use a case when and group by
select date, sum(case when drcr = 'CR' then amount
when drcr = 'DR' then -amount
else 0 end)
from my_table
group by date
as #scaisEdge but you need a sum.
select date, sum(case when drcr = 'CR' then amuont else (-1 * amount) end) as tran_amt
from my_table
group by date
This should be clear, but the way it works is to add credits and substract debits to the total for each date.

Issues with MIN & MAX when using Case statement

I am trying to generate a summary report using various aggregate functions: MIN, MAX, SUM, etc. The issue I have is when I try to get a MIN and MAX of a field when I am also using the case statement. I am unable to get the MIN value of a field when I am using the case statement. I can best explain it with sample data and the sql statement:
Fields: AccountNumber, Symbol, TradeDate, TransactionType, Price, Quantity, Amount
Table: Trades
AccountNumber, Symbol, TradeDate, TransactionType, Price, Quantity, Amount
123,"XYZ",1/2/2011,"Buy",15,100,1500
123,"XYZ",1/2/2011,"Buy",10,50,500
123,"XYZ",1/2/2011,"Sell",20,100,2000
456,"ABC",1/3/2011,"Buy",10,20,200
456,"ABC",1/3/2011,"Buy",15,30,450
789,"DEF",1/4/2011,"Sell",30,100,3000
Query:
SELECT AccountNumber,
Symbol,
SUM(case when TransactionType = "Buy" then 1 else 0) as TotalBuys,
SUM(case when TransactionType = "Sell" then 1 else 0) as TotalSells,
MIN(case when TransactionType = "Buy" then Price else 0) as MinBuy,
MAX(case when TransactionType = "Buy" then Price else 0) as MaxBuy,
MIN(case when TransactionType = "Sell" then Price else 0) as MinSell,
MAX(case when TransactionType = "Sell" then Price else 0) as MaxSell,
MIN(Price) as MinPrice,
MAX(Price) as MaxPrice
FROM Trades
Group By AccountNumber, Symbol
What I am expecting is the following results:
AccountNumber, Symbol, TotalBuys, TotalSells, MinBuy, MaxBuy, MinSell, MaxSell, MinPrice, MaxPrice
123,"XYZ",2,1,10,15,20,20,10,20
456,"ABC",2,0,10,15,0,0,10,15
789,"DEF",0,1,0,0,30,30,30,30
However, I am getting the following results:
AccountNumber, Symbol, TotalBuys, TotalSells, MinBuy, MaxBuy, MinSell, MaxSell, MinPrice, MaxPrice
123,"XYZ",2,1,**0**,15,**0**,20,**0**,20
456,"ABC",2,0,10,15,0,0,10,15
789,"DEF",0,1,0,0,30,30,30,30
When there are two different TransactionTypes for each grouping, the Min fields (MinBuy,MinSell, and MinPrice) are coming out as 0 as opposed to what is expected. What am I doing wrong on the sql statement? Is there another way to get the desired results?
Min between 0 and a positive number is 0, you should change:
MIN(case when TransactionType = "Buy" then Price else 0)
by
MIN(case when TransactionType = "Buy" then Price else Null)
Null don't compute in an aggregation function.
Thats all.
Edited 6 years later:
As P5Coder says, it is enough without else clause, also I guess the end is mandatory on some database brands. Here it is:
MIN(case when TransactionType = "Buy" then Price end)