How can I use the loanAmount and amountPaid to get the balance.
SELECT (SELECT SUM(loanRepayment.amount) FROM `loanRepayment` WHERE loanRepayment.loanNumber='MMSE22062311' AND loanRepayment.transactionType ='DR')loanAmount,
(SELECT SUM(loanRepayment.amount) FROM `loanRepayment` WHERE loanRepayment.loanNumber='MMSE22062311' AND loanRepayment.transactionType ='CR')amountPaid,
(loanAmount-amountPaid)balance
SELECT SUM(CASE WHEN transactionType = 'DR'
THEN amount
ELSE 0
END) loanAmount,
SUM(CASE WHEN transactionType = 'CR'
THEN amount
ELSE 0
END) amountPaid,
SUM(CASE WHEN transactionType = 'DR'
THEN amount
ELSE -amount
END) balance
FROM loanRepayment
WHERE loanNumber='MMSE22062311'
AND transactionType IN ('DR', 'CR')
Related
With the following MySQL table containing debit or credit "actions" with associated amounts and currency, how is it possible to select all CLIENT_IDs with a non-zero "balance" of each currency? I have tried but something isn't working correctly.
CLIENT_ID ACTION_TYPE ACTION_AMOUNT Currency
1 debit 1000 USD
1 credit 100 USD
1 credit 500 DR
2 debit 1000 EURO
2 credit 1200 DR
3 debit 1000 EURO
3 credit 1000 USD
4 debit 1000 USD
My MySQL query that doesn't work:
SELECT client_id,
SUM(if(action_type='credit',action_amount,0)) as credit,
SUM(if(action_type='debit',action_amount,0 )) as debit,
SUM(if(currency='USD' ,action_type='credit'- action_type='debit' )) as USD_balance,
SUM(if(currency='EURO',action_type='credit'- action_type='debit' )) as EURO_balance,
SUM(if(currency='DR' ,action_type='credit'- action_type='debit' )) as DR_balance
FROM my_table
GROUP BY client_id,currency
The result I am expecting is something like:
CLIENT_ID USD_Balance EURO_Balance DR_Balance
1 -900 0 500
2 0 -1000 1200
3 1000 -1000 0
4 -1000 0 0
I have no idea what else to try. Any help would be great.
You can Group By client_id including Conditional Aggregation as below
SELECT client_id,
SUM(case when action_type = 'credit' then action_amount else 0 end) as credit,
SUM(case when action_type = 'debit' then action_amount else 0 end) as debit,
SUM(case
when currency = 'USD' and action_type = 'credit' then
action_amount else 0
end) - SUM(case
when currency = 'USD' and action_type = 'debit' then
action_amount
else 0
end) as USD_balance,
SUM(case
when currency = 'EURO' and action_type = 'credit' then
action_amount else 0
end) - SUM(case
when currency = 'EURO' and action_type = 'debit' then
action_amount
else 0
end) as EUR_balance,
SUM(case
when currency = 'DR' and action_type = 'credit' then
action_amount
else 0
end) - SUM(case
when currency = 'DR' and action_type = 'debit' then
action_amount
else 0
end) as DR_balance
FROM my_table
GROUP BY client_id;
Demo
SELECT client_id
, currency
, SUM(CASE WHEN action_type = 'debit' THEN action_amount * -1 ELSE action_amount END) balance
FROM my_table
GROUP
BY client_id
, currency
The rest of the problem can (and should) be resolved in application code
Do a GROUP BY on client_id only, as you are trying to "pivot" on multiple currencies in different columns. You will need to use two levels of If() statements to compute balance.
Try the following:
SELECT CLIENT_ID,
SUM(IF(Currency = 'USD', IF(ACTION_TYPE = 'credit', ACTION_AMOUNT, -ACTION_AMOUNT), 0)) AS USD_Balance,
SUM(IF(Currency = 'EURO', IF(ACTION_TYPE = 'credit', ACTION_AMOUNT, -ACTION_AMOUNT), 0)) AS EURO_Balance,
SUM(IF(Currency = 'DR', IF(ACTION_TYPE = 'credit', ACTION_AMOUNT, -ACTION_AMOUNT), 0)) AS DR_Balance
FROM my_table
GROUP BY CLIENT_ID
SELECT tbl_trans.trans_username,
Sum(Case When tbl_trans.trans_type <> 'REWARD' AND tbl_trans.trans_winlose <> 'QB' then
trans_winloseamount Else 0 End) BETHOST,
Sum(Case When tbl_trans.trans_winlose = 'QB' then
trans_winloseamount Else 0 End) QB,
Sum(Case When tbl_trans.trans_type = 'REWARD' then
trans_winloseamount Else 0 End) reward ,
Sum(sum(BETHOST)+sum(QB)+sum(reward)) totalsum
FROM tbl_trans
GROUP BY trans_username
i need to totalsum of BETHOST,QB and reward
how to sum of this Sum(sum(BETHOST)+sum(QB)+sum(reward)) totalsum
pls try this
sum(SELECT(BETHOST))+sum(SELECT(QB))+sum(SELECT(reward)) AS totalsum
select cust_code,occu_name
, SUM(CASE WHEN VoucherType = 'S' THEN Amount ELSE 0 END) AS salesSum
,SUM(CASE WHEN VoucherType = 'I' THEN Amount ELSE 0 END) AS interestSum
,SUM(CASE WHEN VoucherType = '' THEN Amount ELSE 0 END) AS interest_sum
,SUM(CASE WHEN VoucherType = 'P' THEN Amount ELSE 0 END) AS chequereturn_sum
,SUM(CASE WHEN VoucherType = 'R' THEN Amount ELSE 0 END) AS receipt_sum
,SUM(CASE WHEN VoucherType = 'R' THEN Amount ELSE 0 END) AS receipt_sum
,SUM(CASE WHEN VoucherType = 'N' THEN Amount ELSE 0 END) AS credit_sum
,SUM(CASE WHEN (DrCrType = 'DR' and VoucherDate <= '2012-04-01') THEN Amount ELSE (Amount*-1)) AS opening_sum
from bmwregistration, ledger_transactions
where bmwregistration.ledger_id = ledger_transactions.OccupierID and
VoucherDate >= '2012-04-01' and
VoucherDate <= '2013-02-01'
group by cust_code
How to check two conditions in sum function SUM(CASE WHEN (DrCrType = 'DR' and VoucherDate <= '2012-04-01') THEN Amount ELSE (Amount*-1)) AS opening_sum
You are doing it in correct way.
See this Example
You just did a little mistake. All you need is to add END after that conditions like this:
SUM(CASE WHEN DrCrType = 'DR'
and VoucherDate <= '2012-04-01'
THEN Amount
ELSE (Amount*-1) END) AS opening_sum
^^^
So your whole query should be like this:
select cust_code,occu_name
, SUM(CASE WHEN VoucherType = 'S' THEN Amount ELSE 0 END) AS salesSum
,SUM(CASE WHEN VoucherType = 'I' THEN Amount ELSE 0 END) AS interestSum
,SUM(CASE WHEN VoucherType = '' THEN Amount ELSE 0 END) AS interest_sum
,SUM(CASE WHEN VoucherType = 'P' THEN Amount ELSE 0 END) AS chequereturn_sum
,SUM(CASE WHEN VoucherType = 'R' THEN Amount ELSE 0 END) AS receipt_sum
,SUM(CASE WHEN VoucherType = 'R' THEN Amount ELSE 0 END) AS receipt_sum
,SUM(CASE WHEN VoucherType = 'N' THEN Amount ELSE 0 END) AS credit_sum
SUM(CASE WHEN DrCrType = 'DR'
and VoucherDate <= '2012-04-01'
THEN Amount
ELSE (Amount*-1) END) AS opening_sum
from bmwregistration, ledger_transactions
where bmwregistration.ledger_id = ledger_transactions.OccupierID and
VoucherDate >= '2012-04-01' and
VoucherDate <= '2013-02-01'
group by cust_code
WHEN PAT_DrCrType = 'DR' and VoucherDate <= '2012-04-01' THEN Amount ELSE (Amount-1)) AS opening_sum
try this way
SUM(CASE WHEN DrCrType = 'DR' AND VoucherDate <= '2012-04-01' THEN Amount ELSE (Amount-1) END)
See the SQL query below, it work fine. It calculate the number of Yes, NOT, Other and the number of matching mobile number [Sales field] (D.MobileNo = S.mobile)
SELECT D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END) as Not,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S on D.MobileNo = S.mobile
WHERE source = 'Network'
AND UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
GROUP BY D.Username
ORDER BY TOTAL DESC
I want to exclude WHERE for the Sales field - it should not be part from CheckDate. Meaning it should check any record in the dairy table without CheckDate for the Sales field.
How can that be done?
If you really want those results in only one query, this might do the trick.
SELECT D.Username,
SUM(CASE WHEN D.type = 'Yes' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'Not' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Not,
SUM(CASE WHEN D.type = '' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL,
SUM(CASE WHEN UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) AS TOTALINCHECKDATE
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S on D.MobileNo = S.mobile
WHERE source = 'Network'
GROUP BY D.Username
ORDER BY TOTAL DESC
Note that "TOTAL" will count all rows (including those who where not within your CheckDate range), TOTALINCHECKDATE return the same value as in your previous query.
Obviously, this can still be optimized.
Assuming username exists also in SALES table
SELECT Username,SUM(Yes) As Yes, SUM(`Not`) As `Not`
, SUM(Other) As Other, SUM(sales) Sales, SUM(total)
FROM (
-- get diary data
SELECT username,mobileNo As mobile,
CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END as Yes,
CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END as `Not`,
CASE WHEN D.type = '' THEN 1 ELSE 0 END as Other,
0 As sales, 1 as total
FROM dairy as D
WHERE source = 'Network'
AND UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
UNION ALL
-- get all sales
SELECT DISTINCT username,mobile, 0 as Yes, 0 as `Not`, 0 As Other, 1 As sales, 0 As total
FROM sales
WHERE UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
) AS a
GROUP BY Username
Also try your original query with date addition
SELECT D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END) as Not,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales WHERE UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 ) as S on D.MobileNo = S.mobile
WHERE source = 'Network'
AND UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
GROUP BY D.Username
ORDER BY TOTAL DESC
I am using SUM ( CASE WHEN ) to count number of Yes and No, it work fine.
I am havin problem counting number of matching mobile number from two tables. It dont seem to be counting correctly.
There is a MobileNO field in dairy table and mobile field in the sales table
SELECT
D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
(SELECT SUM(CASE WHEN D.MobileNo = S.mobile THEN 1 ELSE 0 END) from sales as S) as Sales,
COUNT(*) as TOTAL FROM dairy as D
WHERE source = 'Company' AND UNIX_TIMESTAMP(CheckDate) >= 1293840000 AND UNIX_TIMESTAMP(CheckDate) <= 1322697600
group by D.Username order by TOTAL DESC
SELECT
D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S ON D.MobileNo = S.mobile
WHERE source = 'Company' AND UNIX_TIMESTAMP(CheckDate) >= 1293840000 AND UNIX_TIMESTAMP(CheckDate) <= 1322697600
group by D.Username order by TOTAL DESC