How to create a calculated row in sql or power bi? - mysql

I am trying to do a calculation on 2 rows on a SQL I wrote so I can have a 3 row that will be Profit and show the amount is it possible?
This dummy data not true to any company!
see below :
SELECT a.pcg_type GL_Acoount_Group,
Abs(sum(b.debit-b.credit)) GL_Amount
FROM dolibarr.llx_accounting_account a
JOIN dolibarr.llx_accounting_bookkeeping b
ON a.account_number = b.numero_compte
WHERE a.pcg_type IN ('INCOME', 'EXPENSE')
ANDa.fk_pcg_version = 'PCG99-BASE'
GROUP BY a.pcg_type
Results:
Group. Amt
INCOME 379200
EXPENSE 65700
Expected Results:
Group. Amt
INCOME 379200
EXPENSE 65700
PROFIT 313500

Use ROLLUP for adding an extra row and use CASE statement inside SUM() function for treating expense value as negative for calculation
--MySQL
SELECT COALESCE(acc_type, 'Profit') "Group"
, ABS(SUM(CASE WHEN acc_type = 'EXPENSE' THEN -amount ELSE amount END)) amt
FROM test
GROUP BY acc_type WITH ROLLUP
Another way by using UNION ALL
SELECT acc_type "Group"
, SUM(amount) Amt
FROM test
GROUP BY acc_type
UNION ALL
SELECT 'Profit' AS "Group"
, SUM(CASE WHEN acc_type = 'EXPENSE' THEN -amount ELSE amount END) Amt
FROM test
Please check this url https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=f859036ffcb3d808330bce5346daee1e

Related

Sum on same column with two different conditions mysql

I have a table named Order with schema as
user_id, state amount
11 success 100
11 FAILED 10
11 FAILED 10
11 success 17
state can have two values (Success/Failed).
I want to fetch sum(amount) when state = "SUCCESS" - sum(amount) when state = "FAILED"
means difference total amount when success - total amount when failed.
I can solve this problem in 2 queries.
A = select id, sum(amount) when state = "SUCCESS"
B = select id, sum(amount) when state = "FAILED"
And solution will be A-B.
Is there any way I can achieve this in single sql query?
use case when
select user_id,sum(case when state = 'SUCCESS' then amount else 0 end)-sum(case when state = 'FAILED' then amount else 0 end)
from table group by user_id
Use conditional aggregation:
select id,
sum(case when state = 'SUCCESS' then amount else - amount end) as total
from t
where state in ('SUCCESS', 'FAILED')
group by id;
I assume that you want this sum per id and not overall in the table.
select sum(case when state = "SUCCESS" then amount else o end) -
sum(case when state = "FAILED" then amount else o end)
from tbl
group by userid

Query to get column totals and Subtract MySQL

I have a transaction table
I am able to get all the debit and credit transaction totals by property separately using these queries
SELECT property, SUM(amount) as creditamount FROM transactions WHERE transactions.type="CREDIT" GROUP BY property
SELECT property, SUM(amount) as debitamount FROM transactions WHERE transactions.type="DEBIT" GROUP BY property
I am facing difficulty in having these two queries done in a single query so that i can subtract credit amount and debit amount for each row and list it according to the property
I tried using sub queries but multiple rows are returned.
Any way to achieve this?
Conditional aggregation may be what you are looking for.
SELECT property,
SUM(case when type = 'Credit' then amount else 0 end) as creditamount,
SUM(case when type = 'Debit' then amount else 0 end) as debitamount,
SUM(case when type = 'Credit' then amount else 0 end) -
SUM(case when type = 'Debit' then amount else 0 end) as diff
FROM transactions
GROUP BY property
Use self join
select debitamount , creditamount
from (select sum(amount) income
from transactions
WHERE transactions.type="CREDIT"
GROUP BY property) i
JOIN (select sum(rate) debitamount
from transactions
WHERE transactions.type="DEBIT"
GROUP BY property) e
Refer this question

mysql returning 2 different columns from the same field but with different conditions

How can I return 2 columns from the same field with different conditions, these are the two conditions:
SELECT CITY,REGION,SALES FROM SALESREPS WHERE SALES < QUOTA;
SELECT CITY,REGION,SALES FROM SALESREPS WHERE SALES > QUOTA;
I mean some say that I just need to put 'AND'. Well what I need is a 1 line code from that example that will return 2 different sales columns. 1st column will be the sales < quota and the other column is the sales > quota. All of these are from the same table.
You can use a mySql if statement like the following:
SELECT
CITY,
REGION,
SALES,
IF (SALES < QUOTA,SALES,null) as BELOW_QUOTA,
IF (SALES > QUOTA,SALES,null) as ABOVE_QUOTA
FROM SALESREPS;
SELECT CITY,REGION,IF(SALES < QUOTA, SALES) AS LessThanColumn, IF(Sales > QUOTA, Sales) AS GreaterThanColumn FROM SALESREPS
If you simply want 4 columns (CITY, REGION, LOW_SALES, HIGH_SALES), with data for every row but only one of LOW_SALES and HIGH_SALES have a value (the other is null), then you can:
select CITY
, REGION
, case when SALES < QUOTA then SALES end as LOW_SALES
, case when SALES > QUOTA then SALES end as HIGH_SALES
from SALESREPS;
The SQL statement uses database agnostic syntax, so it'll work on databases other than MySQL.
This can also be done with a union:
select CITY, REGION, SALES as LOW_SALES, null as HIGH_SALES
from SALESREPS
where SALES < QUOTA
union all
select CITY, REGION, null as LOW_SALES, SALES as HIGH_SALES
from SALESREPS
where SALES > QUOTA;
Neither makes much sense to me, e.g. what about rows with SALES = QUOTA?
Now, if you want totals for CITY/REGION, that makes more sense to me (including exact equal in HIGH_SALES):
select CITY
, REGION
, sum(case when SALES < QUOTA then SALES else 0 end) as LOW_SALES
, sum(case when SALES >= QUOTA then SALES else 0 end) as HIGH_SALES
from SALESREPS
group by CITY
, REGION;
If you just want counts instead:
select CITY
, REGION
, sum(case when SALES < QUOTA then 1 else 0 end) as LOW_SALES
, sum(case when SALES >= QUOTA then 1 else 0 end) as HIGH_SALES
from SALESREPS
group by CITY
, REGION;

mysql table data output in a single row based on the type of the data

I have the following data in mysql table called wallet_txns.
wlt_name wlt_txn_type wlt_txn_amount
A Income 200
A Expense 100
B Income 100
B Income 500
B Expense 200
I trying to get the output of the data like below ( the sum of income and expense in a single row)
Wlt_name Expense Income
A 100 200
B 200 600
I have used the following query, But i am not getting the output as expected, (the income and expense in getting in seperate rows) Please help...
select
wlt_name,
if(wlt_txn_type = 'Expense', wlt_txn_amount, 0) as Expense,
if(wlt_txn_type = 'Income', wlt_txn_amount, 0) as Income
from wallet_txns
;
Maybe you can help this part of the code: SUM(IF(wlt_txn_type = "Income", wlt_txn_amount, 0)) AS IncomeTotal.
You already almost wrote correct query, but you forget about sum and group. Here query:
select
wlt_name,
sum(if(wlt_txn_type = 'Expense', wlt_txn_amount, 0)) as Expense,
sum(if(wlt_txn_type = 'Income', wlt_txn_amount, 0)) as Income
from wallet_txns
group by wlt_name
;
You can use conditional sum and then group by
select
wlt_name,
sum(
case when wlt_rxn_type ='Income' then wlt_txn_amount else 0 end
) as `Income`,
sum(
case when wlt_rxn_type ='Expense' then wlt_txn_amount else 0 end
) as `Expense`
from wallet_txns group by wlt_name;
You could do something like this:
SELECT
Wlt_name,
SUM(CASE WHEN wlt_txn_type='Expense' THEN wlt_txn_amount ELSE 0 END) AS Expense,
SUM(CASE WHEN wlt_txn_type='Income' THEN wlt_txn_amount ELSE 0 END) AS Income
FROM
wallet_txns
GROUP BY
Wlt_name
I'm not a mysql user, but TSQL has little secrets to me. Check for PIVOT - UNPIVOT alternatives on the WWW.

SQL - sum of some rows, minus sum of other rows

I have a table in mySQL with the following columns:
CUSTOMER_CODE | TRANS_TYPE | TRANS_VALUE
TRANS_TYPE could be either "DRINV" (a sale) or "DRCDT" (a credit).
I want to get the total sales per customer, so my query so far is:
SELECT CUSTOMER_CODE, SUM(TRANS_VALUE) as SALES FROM DR_TRANS GROUP BY CUSTOMER_CODE
Problem is this is totaling the sales and credits, instead of giving be sales minus credits. I want the results to be
SUM(TRANS_VALUE) where TRANS_TYPE = "DRINV" - SUM(TRANS_VALUE) where TRANS_TYPE = "DRCDT".
Is it possible to do this in a SQL query?
select customer_code,
sum(case
when trans_type = 'DRINV' then
trans_value
else
-trans_value
end) as net_sales
from dr_trans
group by customer_code
Yes, it is possible. Use CASE caluse:
SELECT CUSTOMER_CODE,
SUM(CASE WHEN TRANS_TYPE = 'DRINV' THEN TRANS_VALUE ELSE (- TRANS_VALUE) END ) as SALES
FROM DR_TRANS GROUP BY CUSTOMER_CODE