multiple where statements in proc sql - 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

Related

tsql - pivot sum of employee

I have here a sample table data which i want to use pivot and make my DedCode data into column.
the AMount example is already computed to its total but how can i count the number of employee if it is same with branch,deptcode and emptype.
my sample table data.
expected output:
You can use conditional aggregation:
select branch, deptcode, emptype, sum(empcount) as empcount,
sum(case when dedcode = 'PHIC' then amount else 0 end) as phic,
sum(case when dedcode = 'SLOAN' then amount else 0 end) as sloan,
sum(case when dedcode = 'VLOAN' then amount else 0 end) as vloan
from t
group by branch, deptcode, emptype;

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.

SQL select grouping and subtract

i have table named source table with data like this :
And i want to do query that subtract row with status plus and minus to be like this group by product name :
How to do that in SQL query? thanks!
Group by the product and then use a conditional SUM()
select product,
sum(case when status = 'plus' then total else 0 end) -
sum(case when status = 'minus' then total else 0 end) as total,
sum(case when status = 'plus' then amount else 0 end) -
sum(case when status = 'minus' then amount else 0 end) as amount
from your_table
group by product
There is another method using join, which works for the particular data you have provided (which has one "plus" and one "minus" row per product):
select tplus.product, (tplus.total - tminus.total) as total,
(tplus.amount - tminus.amount) as amount
from t tplus join
t tminus
on tplus.product = tminus.product and
tplus.status = 'plus' and
tplus.status = 'minus';
Both this and the aggregation query work well for the data you have provided. In other words, there are multiple ways to solve this problem (each has its strengths).
you can query as below:
select product , sum (case when [status] = 'minus' then -Total else Total end) as Total
, sum (case when [status] = 'minus' then -Amount else Amount end) as SumAmount
from yourproduct
group by product

Sql function for statement

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