I have bank transaction data to select two functions in a single query.
I have data like this
|ID | name | amount |
|----|--------|--------|
| 1 | xxx | 1000 |
| 2 | yyy | -500 |
| 3 | qqq | 1500 |
| 4 | ggg | -1000 |
------------------------
I want like this
-------------------------------------------
|ID | name | amount | sell | buy | cum |
|----|--------|--------|------|-----|-----|
| 1 | xxx | 1000 | 1000 | n/a |1000 |
| 2 | yyy | -500 | n/a |-500 | 500 |
| 3 | qqq | 1500 | 1500 | n/a | 2000|
| 4 | ggg | -1000 | n/A |-1000| 1000|
------------------------------------------
My code is.
SELECT ID, name, amount,
CASE WHEN amount >=0 THEN amount END AS sell,
CASE WHEN amount <=0 THEN amount END AS buy,
From bank
Join (select #csum := #csum + amount as csum from bank
Join (select #csum := 0) r order by ID);
Ther no need for multiple select statement.
SELECT ID, name, amount,
CASE WHEN amount >=0 THEN amount END AS sell,
CASE WHEN amount <=0 THEN amount END AS buy,
#csum := #csum + amount as csum from bank
Join (select #csum := 0) r order by ID);
The correct way to write this query uses window functions:
SELECT ID, name, amount,
(CASE WHEN amount >= 0 THEN amount END) AS sell,
(CASE WHEN amount <= 0 THEN amount END) AS buy,
SUM(AMOUNT) OVER (ORDER BY id) as cumulative
FROM bank;
Related
if I have transactions table like that:
+----+--------+------------+-------------+--------+
| id | userID | debitAccID | creditAccID | amount |
+----+--------+------------+-------------+--------+
| 1 | 1 | 1 | 2 | 500 |
| 2 | 1 | 1 | 3 | 600 |
| 3 | 1 | 3 | 1 | 200 |
+----+--------+------------+-------------+--------+
how what query to use to get a table for account with id 1 like that:
+----+--------+------------+-------------+--------+
| debit | credit |balance |
+----+--------+------------+-------------+--------+
| | 500 | | 500 |
| | 600 | | 1100 |
| | | 200| 900 |
+----+--------+------------+-------------+--------+
900
Assuming the id column shows the correct order of transactions, you can use case and window with the default of rows between unlimited preceding and current row to get your output:
select id, user_id,
case when user_id = debit_acc_id then amount else 0 end as debit,
case when user_id = credit_acc_id then amount else 0 end as credit,
sum(case when user_id = debit_acc_id then amount else 0 end) over w
- sum(case when user_id = credit_acc_id then amount else 0 end) over w as balance
from transactions
where user_id = 1
window w as (partition by user_id order by id)
order by user_id, id;
db<>fiddle here
query which calculates the total amount in dollars of stolen goods for each month for restricted and neutral items.
I have 2 tables
first
| UPC | item | in_stock | price | ship_day | class |
1 | 101 | 'generator' | 16 | 5999 | '12-1-2065'| 'restricted'
2 | 102 | 'blank tape' | 30 | 3000 | '12-1-2065'| 'neutral'
second
| UPC | unit_stolen |
1 | 101 | 4 |
1 | 401 | 2 |
If I understand correctly, this is basically a join and group by:
select date_trunc('mon', f.ship_day) as yyyymm,
sum(f.price * s.unit_stolen) filter (where f.class = 'restricted'),
sum(f.price * s.unit_stolen) filter (where f.class = 'neutral')
from first f join
second s
on f.upc = s.upc
group by date_trunc('mon', f.ship_day)
Accounts (Master List of Accounts with link to their parent (same table) )
(Accounts table is maintained using nested_set plugin, so the left, right, depth are available in the table and are maintained while adding/editing accounts)
| id | name | parent_id |
|----|----------------|-----------|
| 1 | Assets | null |
| 2 | Current Assets | 1 |
| 3 | Fixed Assets | 1 |
| 4 | Bank | 2 |
| 5 | Bank One | 4 |
| 6 | Bank Two | 4 |
| 7 | Revenue | null |
| 8 | Sales | 7 |
| 9 | Expenses | null |
| 10 | Rent | 9 |
Entries (where the date and description of each transaction stored)
| entry_id | date | description |
|----------|------------|--------------------|
| 1 | Mar 3 2020 | Cash Sales |
| 2 | Mar 3 2020 | Cash Paid For Rent |
| 3 | Apr 1 2020 | Owner Withdrawal |
Amounts (where the double entry transactions are stored)
| entry_id | account_id | type | amount |
|----------|------------|--------|--------|
| 1 | 5 | debit | 10000 |
| 1 | 8 | credit | 10000 |
| 2 | 10 | debit | 1000 |
| 2 | 5 | credit | 1000 |
| | | | |
Given the above structure, here is my requirements
Arrange the accounts in Tree(hierarchical) structure and calculate the individual account balances (balances can be debit_balance or credit_balance)
Hierarchical account balances, rolling up child balances to the parent accounts
PS:
I do have the solution for the req 1 above using a combination of
WITH RECURSIVE sql function on the accounts table and arranging the rows hierarchially and then joining the result set with amounts table that is summed up on amount column (after grouping on type) for each accounts.
I am keen to see how the folks over here will solve this. (lemme know if you would like to see what I got so far though)
here is the query that gets me the first result set. (i've omitted the details like normal_credit_blance flag etc for brevity in the original question)
select id, parent_id, name, newdepth as depth, debit_amount, credit_amount, type,
CASE WHEN normal_credit_balance = true THEN credit_amount - debit_amount END as credit_balance,
CASE WHEN normal_credit_balance = false THEN debit_amount - credit_amount END as debit_balance
from
(
WITH RECURSIVE children AS (
SELECT id, parent_id, display_name, lft, rgt, type, normal_credit_balance, 0 as newdepth
FROM accounts
WHERE parent_id is null
UNION
SELECT op.id, op.parent_id, op.display_name, op.lft, op.rgt, op.type, op.normal_credit_balance, newdepth + 1
FROM accounts op
JOIN children c ON op.parent_id = c.id
)
SELECT *
FROM children
) accounts_tbl
left join
( SELECT account_id,
SUM( CASE WHEN am.type = 'debit' THEN COALESCE( AMOUNT , 0.0 ) ELSE 0.0 END ) AS debit_amount ,
SUM( CASE WHEN am.type = 'credit' THEN COALESCE( AMOUNT , 0.0 ) ELSE 0.0 END ) AS credit_amount
FROM amounts am
join accounts ac on ac.id = am.account_id
group by account_id, ac.name, ac.type )
as amount_tbl
on accounts_tbl.id = amount_tbl.account_id order by lft
sample result based on the amounts table entries, the rollup should look like this:
| id | name | balance |
|----|----------------|-----------|
| 1 | Assets | 9000 |
| 2 | Current Assets | 9000 |
| 3 | Fixed Assets | 0 |
| 4 | Bank | 9000 |
| 5 | Bank One | 9000 |
| 6 | Bank Two | 0 |
| 7 | Revenue | 10000 |
| 8 | Sales | 10000 |
| 9 | Expenses | 1000 |
| 10 | Rent | 1000 |
I would start by computing the "direct" balance of each account, with a left join and aggregation. Then goes the recursive query: you just need to traverse the tree from the leafs to the root, conslidating the balance as you go. The final step is aggregation.
with recursive
data (id, name, parent_id, balance) as (
select
ac.*,
coalesce(sum(case am.type when 'debit' then - amount when 'credit' then amount end), 0) balance
from accounts ac
left join amounts am on am.account_id = ac.id
group by ac.id
),
cte (id, name, parent_id, balance) as (
select d.* from data d
union all
select d.id, d.name, d.parent_id, d.balance + c.balance
from cte c
inner join data d on d.id = c.parent_id
)
select id, name, sum(balance) from cte group by id, name
I don't get how all the accounts in your resultset end up with a positive balance, while some have more debits than credit (and vice-versa). The query treats debits as negative amounts and credits as positive.
Demo on DB Fiddle:
id | name | sum
-: | :------------- | ----:
1 | Assets | -9000
2 | Current Assets | -9000
3 | Fixed Assets | 0
4 | Bank | -9000
5 | Bank One | -9000
6 | Bank Two | 0
7 | Revenue | 10000
8 | Sales | 10000
9 | Expenses | -1000
10 | Rent | -1000
If you were using a closure table instead of nested sets (like I do in https://stackoverflow.com/a/38701519/5962802) then you could use simple JOINs like
SELECT
accounts.id,
accounts.title,
SUM(COALESCE(debits.amount,0)) AS debit,
SUM(COALESCE(credit.amount,0)) AS credit
FROM account_tree
LEFT JOIN accounts ON ancestor_id = accounts.id
LEFT JOIN balances AS debits ON account_id = child_id AND type = 'debit'
LEFT JOIN balances AS credits ON account_id = child_id AND type = 'credit'
GROUP BY accounts.id,accounts.title
As a side note I would recommend you to keep debits and credits on the same row - as 2 columns in table BALANCES.
need help for below.
Table1
+------------+-------------------+----------+
| Shipmnt | Costtype | cost |
+------------+-------------------+----------+
|22 | toll | 100 |
|23 | parking | 111 |
|25 | tax | 25 |
+------------+-------------------+----------+
Expected Result
+----------+--------+-----------+-------+
| shipmnt | toll | parking | tax |
+----------+--------+-----------+-------+
| 22 | 100 | 0 | 0 |
| 23 | 0 | 111 | 0 |
| 25 | 0 | 0 | 25 |
+----------+--------+-----------+-------+
i m trying to use case when but not able to get column.
You didn't state your DBMS, the following is standard SQL and works on all DBMS I know of.
select shipmt,
sum(case when costtype = 'toll' then cost end) as toll_costs,
sum(case when costtype = 'parking' then cost end) as parking_costs,
sum(case when costtype = 'tax' then cost end) as tax_costs
from shipments
group by shipmt;
Select distinct a.shipment,
ISNULL((Select cost from Table1 where Costtype='troll' and shipmnt=a.shipmnt),0) as troll,
ISNULL((Select cost from Table1 where Costtype='parking' and shipmnt=a.shipmnt),0) as parking,
ISNULL((Select cost from Table1 where Costtype='tax' and shipmnt=a.shipmnt),0) as tax
from Table1 a
QUERY
SELECT shipmnt,
MAX(IF(costType='toll',cost,0)) AS toll,
MAX(IF(costType='parking',cost,0)) AS parking,
MAX(IF(costType='tax',cost,0)) AS tax
FROM tbl1
GROUP BY shipmnt;
FIND FIDDLE HERE
I have the following table structures for which I am trying to obtain a totals and subtotals and show a rollup of the values.
ChartOfAccounts(AccountNumber, AccountDescription, ParentAccountNumber, IsControlAccount)
Ledger(LedgerId, JournalId, AccountNumber, IsDebit, Amount)
I have managed to use CTE to obtain the required Parent-Child relationships but am unsure how to use this get control account balances which rollup into parent accounts.
So far, I have managed to put the following query together which is not entirely what I want --> SQL Fiddle. The current query does not seem to rollup and group the parent-child totals correctly. (I have excluded the year,month columns from the fiddle)
Another way to describe the problem, would be to say that all control accounts should have the total of it's child accounts.
My required output is the following
(year, month, AccountNumber, AccountDescription, DebitBalance, CreditBalance, Balance)
|Account#|Acc Desc | DR | CR | BAL |
|1000 |Accounts Receivable |10000 |5000 |5000 |
|1200 |Buyer Receivables |5000 |0 |5000 |
|12001 |Buyer Receivables - Best Buy |5000 |0 |5000 |
|1500 |Offers |5000 |5000 |0 |
|4000 |Accounts Payable | |4475.06 |4475.06 |
|4100 |Supplier Invoice Payables | |4475.06 |4475.06 |
|41002 |Supplier Invoice Payables - Knechtel | |4475.06 |4475.06 |
|6000 |Revenue | |524.93 |524.93 |
|6100 |Membership Fees Revenue | | |0 |
|6200 |Processing Fees Revenue | |100 |100 |
|62002 |Processing Fees Revenue - Knechtel | |100 |100 |
|6300 |Fees Revenue | |424.93 |424.93 |
|63002 |Fees Revenue - Knechtel | |424.93 |424.93 |
Here is what I came up with and was able to get really close to matching your desired output
WITH CTEAcc
AS
(
SELECT
coa.accountDescription,coa.accountnumber,coa.accountnumber as parentaccount
FROM ChartOfAccounts coa
where iscontrolaccount=1
union all select c.accountdescription, coa.accountnumber, c.ParentAccount
from chartofaccounts coa
inner join cteacc c on coa.ParentAccountNumber=c.accountnumber
)
select parentaccount as [Account#], accountdescription as [Acc Desc],
sum(case when isdebit=1 then amount else 0 end) as DR,
sum(case when isdebit=0 then amount else 0 end) as CR,
sum(case when isdebit=1 then amount else 0 end)-sum(case when isdebit=0 then amount else 0 end) as BAL
from (select c.accountdescription, c.accountnumber,
c.parentaccount, l.isdebit, l.amount
from cteacc c
left join ledger l
on c.accountnumber=l.accountnumber
union all select c.accountdescription,
c.accountnumber, c.accountnumber as parentaccount,
l.isdebit, l.amount
from ChartOfAccounts c
inner join ledger l
on c.accountnumber=l.accountnumber where amount<>0) f
group by parentaccount, accountdescription
order by parentaccount
Here is the sql fiddle: http://www.sqlfiddle.com/#!3/d94bc/106
Yet another variation. Kept the hierarchy and iscontrol fields in just for reference. First it associates the account hierarchy with each account (the recursive cte). Then, for each account, computes sums of the ledger items for the account based on the hierarchy position (and whether it is a control account or not). Finally, wraps in another query to compute the balance of and strip off unused accounts from the output.
WITH AccountHierarchy AS (
SELECT AccountNumber
,AccountDescription
,CAST(AccountNumber AS VARCHAR(MAX))
+ '/' AS AccountHierarchy
,IsControlAccount
FROM ChartOfAccounts
WHERE ParentAccountNumber IS NULL
UNION ALL
SELECT c.AccountNumber
,c.AccountDescription
,CAST(h.AccountHierarchy AS VARCHAR(MAX))
+ CAST(c.AccountNumber AS VARCHAR(MAX))
+ '/' AS AccountHierarchy
,c.IsControlAccount
FROM ChartOfAccounts c
INNER JOIN AccountHierarchy h ON (c.ParentAccountNumber = h.AccountNumber)
WHERE ParentAccountNumber IS NOT NULL
)
SELECT AccountNumber
,AccountDescription
,AccountHierarchy
,IsControlAccount
,DR
,CR
,CASE WHEN (DR IS NULL AND CR IS NULL) THEN NULL
ELSE COALESCE(DR, 0) - COALESCE(CR, 0)
END AS BAL
FROM (SELECT h.AccountNumber
,h.AccountDescription
,h.AccountHierarchy
,h.IsControlAccount
,(SELECT SUM(l.Amount)
FROM Ledger l
INNER JOIN AccountHierarchy hd ON (l.AccountNumber = hd.AccountNumber)
WHERE l.IsDebit = 1
AND ( (h.IsControlAccount = 1 AND hd.AccountHierarchy LIKE h.AccountHierarchy + '%')
OR hd.AccountHierarchy = h.AccountHierarchy)
) AS DR
,(SELECT SUM(l.Amount)
FROM Ledger l
INNER JOIN AccountHierarchy hd ON (l.AccountNumber = hd.AccountNumber)
WHERE l.IsDebit = 0
AND ( (h.IsControlAccount = 1 AND hd.AccountHierarchy LIKE h.AccountHierarchy + '%')
OR hd.AccountHierarchy = h.AccountHierarchy)
) AS CR
FROM AccountHierarchy h
) x
WHERE NOT(CR IS NULL AND DR IS NULL)
ORDER BY AccountHierarchy
I used this question for a hierarchy example.
Output:
| ACCOUNTNUMBER | ACCOUNTDESCRIPTION | ACCOUNTHIERARCHY | ISCONTROLACCOUNT | DR | CR | BAL |
|----------------------|------------------------------------|-----------------------------------------------------------------|------------------|--------|-----------|------------|
| 1000 | Accounts Receivable | 1000 / | 1 | 10000 | 5000 | 5000 |
| 1200 | Buyer Receivables | 1000 /1200 / | 1 | 5000 | (null) | 5000 |
| 12001 | Buyer Receivables - Best Buy | 1000 /1200 /12001 / | 0 | 5000 | (null) | 5000 |
| 1500 | Offers | 1000 /1500 / | 0 | 5000 | 5000 | 0 |
| 4000 | Accounts Payable | 4000 / | 1 | (null) | 4475.0685 | -4475.0685 |
| 4100 | Supplier Payables | 4000 /4100 / | 1 | (null) | 4475.0685 | -4475.0685 |
| 41002 | Supplier Payables - Knechtel | 4000 /4100 /41002 / | 0 | (null) | 4475.0685 | -4475.0685 |
| 6000 | Revenue | 6000 / | 1 | (null) | 524.9315 | -524.9315 |
| 6200 | Processing Fees Revenue | 6000 /6200 / | 1 | (null) | 100 | -100 |
| 62002 | Processing Fees Revenue - Knechtel | 6000 /6200 /62002 / | 0 | (null) | 100 | -100 |
| 6300 | Fees Revenue | 6000 /6300 / | 1 | (null) | 424.9315 | -424.9315 |
| 63002 | Fees Revenue - Knechtel | 6000 /6300 /63002 / | 0 | (null) | 424.9315 | -424.9315 |
Starting with your desired output I came up with the following query that groups child accounts based on the ParentAccountNumber. The subquery is only needed since I assumed you want to convert any NULL value to 0 before summing up (in SQL, NULL + 42 = NULL).
with preresult as
(
select acc.ParentAccountNumber as AccountNumber,
acc.AccountDescription as "Acc Desc",
ISNULL(ld.Amount, 0) as DR,
ISNULL(lc.Amount, 0) as CR
from ChartOfAccounts acc
left outer join Ledger ld
on (ld.AccountNumber = acc.AccountNumber AND ld.IsDebit = 1)
left outer join Ledger lc
on (lc.AccountNumber = acc.AccountNumber AND lc.IsDebit = 0)
where acc.ParentAccountNumber is not null
)
select c.AccountNumber as "ACC",
c.AccountDescription as "ACC DESC",
sum(DR) as DR,
sum(CR) as CR,
sum(DR) - sum(CR) AS BL
from preresult p
join ChartOfAccounts c on (c.AccountNumber = p.AccountNumber)
group by c.AccountNumber, c.AccountDescription;
The sqlfiddle can be found here: http://www.sqlfiddle.com/#!3/d94bc/81/0
This seems to give you what you want:
;WITH recurs
AS
(
SELECT C.AccountNumber, C.IsControlAccount, C.ParentAccountNumber, C.AccountDescription,
COALESCE((SELECT SUM(Amount) FROM Ledger WHERE AccountNumber = C.AccountNumber and IsDebit = 1), 0) AS DR,
COALESCE((SELECT SUM(Amount) FROM Ledger WHERE AccountNumber = C.AccountNumber and IsDebit = 0), 0) AS CR,
COALESCE((SELECT SUM(CASE WHEN IsDebit = 0 THEN Amount * -1 ELSE Amount END) FROM Ledger WHERE AccountNumber = C.AccountNumber), 0) AS BAL
FROM ChartOfAccounts C
WHERE IsControlAccount = 0
UNION ALL
SELECT C.AccountNumber, C.IsControlAccount, C.ParentAccountNumber, C.AccountDescription,
r.DR, r.CR, R.BAL
FROM ChartOfAccounts C
INNER JOIN recurs r
ON r.ParentAccountNumber = c.AccountNumber
)
SELECT R.AccountNumber, R.AccountDescription, SUM(R.DR) AS DR, SUM(R.CR) AS CR, SUM(R.BAL) AS BAL
FROM recurs R
WHERE NOT (R.DR = 0 AND R.CR = 0 AND R.BAL = 0)
GROUP BY R.AccountNumber, R.AccountDescription
ORDER BY AccountNumber
SQL Fiddle here
Results:
| ACCOUNTNUMBER | ACCOUNTDESCRIPTION | DR | CR | BAL |
|----------------------|------------------------------------|-------|-----------|------------|
| 1000 | Accounts Receivable | 10000 | 5000 | 5000 |
| 1200 | Buyer Receivables | 5000 | 0 | 5000 |
| 12001 | Buyer Receivables - Best Buy | 5000 | 0 | 5000 |
| 1500 | Offers | 5000 | 5000 | 0 |
| 4000 | Accounts Payable | 0 | 4475.0685 | -4475.0685 |
| 4100 | Supplier Payables | 0 | 4475.0685 | -4475.0685 |
| 41002 | Supplier Payables - Knechtel | 0 | 4475.0685 | -4475.0685 |
| 6000 | Revenue | 0 | 524.9315 | -524.9315 |
| 6200 | Processing Fees Revenue | 0 | 100 | -100 |
| 62002 | Processing Fees Revenue - Knechtel | 0 | 100 | -100 |
| 6300 | Fees Revenue | 0 | 424.9315 | -424.9315 |
| 63002 | Fees Revenue - Knechtel | 0 | 424.9315 | -424.9315 |