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
I need to generate an aging report using the below sample data from tblAccount.
+---------+----------+---------------+-------------------+---------------------+-------------+-----------+---------+
| Loan ID | Account | Name | Amortization Date | Amortized Principal | Paid Amount | Date Paid | Balance |
+---------+----------+---------------+-------------------+---------------------+-------------+-----------+---------+
| 2 | A0007787 | JIMMY NEUTRON | 3/9/2020 | 3823.53 | 3823.53 | 3/9/2020 | 0 |
| 2 | A0007787 | JIMMY NEUTRON | 4/9/2020 | 3823.53 | 500 | 3/9/2020 | 3323.53 |
| 2 | A0007787 | JIMMY NEUTRON | 5/9/2020 | 3823.53 | 0 | NULL | 3823.53 |
+---------+----------+---------------+-------------------+---------------------+-------------+-----------+---------+
Zero (0) paid amount and NULL Date Paid means that the amortized principal is not yet paid. Below is my desired output for specific Aging dates.
Aging A - Aging on March 12 Payment has been made on March 12 for March 9 schedule
+---------+----------+---------------+------------+------------+---------+---------+-------+-------+-----
+---------+----------+---------------+------------+------------+---------+---------+-------+-------+-------+---------------+
| Loan ID | Account | Name | Due | Payment | Balance | Current | 30 | 60 | 90 | 120 and Above |
+---------+----------+---------------+------------+------------+---------+---------+-------+-------+-------+---------------+
| 2 | A0007787 | JIMMY NEUTRON | 3,823.53 | 3,823.53 | 0 | - | - | - | - | - |
+---------+----------+---------------+------------+------------+---------+---------+-------+-------+-------+---------------+
Aging B - May 10 500.00 payment has been made on April 9 for April 9 schedule
+---------+----------+---------------+------------+----------+------------+------------+-------+-------+-------+-----------------+
| Loan ID | Account | Name | Due | Payment | Balance | Current | 30 | 60 | 90 | 120 and Above |
+---------+----------+---------------+------------+----------+------------+------------+-------+-------+-------+-----------------+
| 2 | A0007787 | JIMMY NEUTRON | 3,823.53 | 500.00 | 3,323.53 | 3,323.53 | - | - | - | - |
+---------+----------+---------------+------------+----------+------------+------------+-------+-------+-------+-----------------+
Aging C - June 10 There is remaining balance for April 9 schedule and no payment made for May 9
schdeule
+---------+----------+---------------+------------+---------+------------+-----------+------------+------------+-------+-----------------+
| Loan ID | Account | Name | Due | Payment | Balance | Current | 30 | 60 | 90 | 120 and Above |
+---------+----------+---------------+------------+---------+------------+-----------+------------+------------+-------+-----------------+
| 2 | A0007787 | JIMMY NEUTRON | 7,147.06 | 500 | 7,147.06 | - | 3,823.53 | 3,323.53 | - | - |
+---------+----------+---------------+------------+---------+------------+-----------+------------+------------+-------+-----------------+
I have looked and tried suggested solutions here, made tweeks, but it seems that it does not fit to what I wanted to produce.
Currently, I have this kind of Stored Procedure:
INSERT INTO #Aging (
Loan, ID, Account, Name, AmortizationSchedule, AmortizedPrincipal, PaidAmount, DatePaid, Balance)
SELECT Loan, ID, Account, Name, AmortizationSchedule, AmortizedPrincipal, PaidAmount, DatePaid, Balance
FROM tblAccount
Select DISTINCT Loan, ID, Account, Name, AmortizationSchedule, AmortizedPrincipal, PaidAmount, DatePaid, Balance
(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,#DateAsOf )) < 1 then balance else 0 end) as [Current],
(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,#DateAsOf )) between 1 and 30 then balance else 0 end) as [DueTo30],
(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,#DateAsOf )) between 31 and 60 then balance else 0 end) as [DueTo60],
(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,#DateAsOf )) between 61 and 90 then balance else 0 end) as [DueTo90],
(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,#DateAsOf )) between 91 and 120 then balance else 0 end) as [DueTo120],
(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,#DateAsOf )) > 120 then balance else 0 end) as [Over120]
from #Aging where balance<>0.00
order by Account
But it gives the following result,
+---------+----------+---------------+-------------+------------+-------------+-----------+-------+-------------+----+-----------------+
| Loan ID | Account | Name | Due | Payment | Balance | Current | 30 | 60 | 90 | 120 and Above |
+---------+----------+---------------+-------------+------------+-------------+-----------+-------+-------------+----+-----------------+
| 2 | A0007787 | JIMMY NEUTRON | 90,000.00 | 8,647.06 | 14,294.12 | - | - | 14,294.12 | 0 | 0 |
+---------+----------+---------------+-------------+------------+-------------+-----------+-------+-------------+----+-----------------+
It seems that it duplicate the value of due, payment and balance on the number of amortization schedule which is three (March, April, May)
Hoping for a positive response. TIA!
Your query gives the correct results once group by and sum used. Am I missing something?
Select DISTINCT [Loan ID], Account, [Name]
,sum(AmortizedPrincipal) Due, sum(PaidAmount) Payment, sum(Balance) Balance,
sum(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,#DateAsOf)) < 1 then balance else 0 end) as [Current],
sum(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,#DateAsOf)) between 1 and 30 then balance else 0 end) as [DueTo30],
sum(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,#DateAsOf)) between 31 and 60 then balance else 0 end) as [DueTo60],
sum(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,#DateAsOf)) between 61 and 90 then balance else 0 end) as [DueTo90],
sum(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,#DateAsOf)) between 91 and 120 then balance else 0 end) as [DueTo120],
sum(case when DATEDIFF(day,convert(date,AmortizationSchedule),convert(date,#DateAsOf)) > 120 then balance else 0 end) as [Over120]
from #Aging where balance<>0.00
GROUP BY [Loan ID], Account, [Name]
order by Account
Please check the db<>fiddle here.
I request your collaboration because pivot on a table and separating the records by null, but still leave the fields at 0 with NVL
Table
product | value
----------+-------
Shirts | 1200
Caps | 0
Stocks | 0
Glasses | 100
Shoes | 0
Código pivot
select * from products
PIVOT (sum(value)
for titles in ('product', 'value')) AS pivot_product
Result:
product | Shirts | Caps | Stocks | Glasses | Shoes
---------+-----------+--------+-------------+---------+----------
value | NULL | NULL | NULL | 100 | NULL
value | 1200 | NULL | NULL | NULL | NULL
Expected result:
product | Shirts | Caps | Stocks | Glasses | Shoes
---------+-----------+--------+-------------+-------+----------
valor | 1200 | NULL | NULL | 100 | NULL
Optional
product | Shirts | Caps | Stocks | Glasses | Shoes
---------+-----------+--------+-------------+-------+----------
valor | 1200 | 0 | 0 | 100 | 0
You need to put the column values in the pivot list:
Oracle Setup:
CREATE TABLE test_data ( product, value ) AS
SELECT 'Shirts', 1200 FROM DUAL UNION ALL
SELECT 'Caps', 0 FROM DUAL UNION ALL
SELECT 'Stocks', 0 FROM DUAL UNION ALL
SELECT 'Glasses', 100 FROM DUAL UNION ALL
SELECT 'Shoes', 0 FROM DUAL
Query:
SELECT 'value' AS product,
p.*
FROM test_data
PIVOT ( SUM( value ) FOR product IN (
'Shirts' AS Shirts,
'Caps' AS Caps,
'Stocks' AS Stocks,
'Glasses' AS Glasses,
'Shoes' AS Shoes
) ) p
Output:
PRODUCT | SHIRTS | CAPS | STOCKS | GLASSES | SHOES
:------ | -----: | ---: | -----: | ------: | ----:
value | 1200 | 0 | 0 | 100 | 0
db<>fiddle here
Just use conditional aggregation. It is more flexible:
select 'valor' as product,
sum(case when product = 'Shirts' then value end) as shirts,
sum(case when product = 'Caps' then value end) as caps,
sum(case when product = 'Stocks' then value end) as stockes,
sum(case when product = 'Shirts' then value end) as shirts,
sum(case when product = 'Glasses' then value end) as glasses,
sum(case when product = 'Shoes' then value end) as shoes
from test_data;
I am trying to use the case statement on one column and calculate an aggregate of another column if the condition is satisfied.
A sample data table (for a hotel, for example) that I am querying looks like so:
| date | customer_name | customer_id | expense_type | billed_amount |
| 08-02 | John Doe | 1 | personal | 120.00 |
| 08-03 | John Doe | 1 | personal | 80.00 |
| 08-04 | John Doe | 1 | corporate | 205.00 |
| 08-02 | Adam Smith | 2 | corporate | 400.00 |
| 08-03 | Adam Smith | 2 | personal | 300.00 |
| 08-06 | Adam Smith | 2 | corporate | 150.00 |
Below is the SQL query I wrote:
Select
customer_name
, customer_id
, case when expense_type = 'personal' then sum(billed_amount) else 0 end as personal_bill
, case when expense_type = 'corporate' then sum(billed_amount) else 0 end as corporate_bill
From payments
Where date > '08-01'
Group by 1, 2
The error message I get is:
Column "expense_type" must appear in the GROUP BY clause or be used in an aggregate function
When I try to group column 3 as well (along with 1 and 2), I get this error message:
Aggregates not allowed in GROUP BY clause
Finally, below illustrates the desired resulting table:
| customer name | customer_id | personal_bill | corporate_bill |
| John Doe | 1 | 200.00 | 205.00 |
| Adam Smith | 2 | 300.00 | 550.00 |
One solution I can think of is creating two different subqueries that constrain the "expense_type" in the Where section (i.e. where expense_type = 'personal') and then querying them in the master query, but that's a lot of similar code with one line of difference. Could you please help me write this query in an efficient manner? Thank you!
Use filter:
select
customer_name,
customer_id,
sum(billed_amount) filter (where expense_type = 'personal') as personal_bill,
sum(billed_amount) filter (where expense_type = 'corporate') as corporate_bill
from payments
where date > '08-01'
group by 1, 2
customer_name | customer_id | personal_bill | corporate_bill
---------------+-------------+---------------+----------------
Adam Smith | 2 | 300.00 | 550.00
John Doe | 1 | 200.00 | 205.00
(2 rows)
Sum the entire CASE expression:
select
customer_name,
customer_id,
sum(case when customer_type = 'personal'
then billed_amount else 0 end) as personal_bill
sum(case when customer_type = 'corporate'
then billed_amount else 0 end) as corporate_bill
from payments
where date > '08-01'
group by 1, 2
Try This :-
Select MIN(customer_name) AS customer_name, customer_id,
SUM(case when expense_type = 'personal' then billed_amount::double precision else 0 end) as personal_bill,
SUM(case when expense_type = 'corporate' then billed_amount::double precision else 0 end) as corporate_bill
From payments
Where date > '08-01'
Group by customer_id
ORDER BY customer_id;
my table, I want to create three columns in single-column, reprinted now.
id | date | type | total
------ | ------ | ------ | -----
1 | 01.10.2016| Paypal | 50
2 | 03.10.2016| credit | 40
3 | 05.10.2016| Cash | 50
4 | 06.10.2016| payment| 100
5 | 07.10.2016| Cash | 20
6 | 15.10.2016| Skrill | 10
7 | 18.10.2016| payment| 20
8 | 19.10.2016| Paypal | 10
9 | 19.10.2016| payment| 20
10 | 22.10.2016| Cash | 40
11 | 23.10.2016| Skrill | 10
my table, I want to create three columns in single-column, reprinted now.
SELECT id,date,type,total
(select (
sum(case when type="Paypal" then total else 0 end)+
sum(case when type="credit" then total else 0 end))+
sum(case when type="Cash" then total else 0 end) ) as receiv,
(Select(
sum(case when type="payment" then total else 0 end)) AS payment,
(Select sum(receiv -payment) FROM totals t2
WHERE (t2.date <= t1.date) and (t2.id <= t1.id) order by t1.date) AS remainder
FROM totals t1
group by date, type
order by id,date
--
The following query for the sql code?
Type = "Paypal, credit, Cash" sums "receiv" sums and Type = "payment" sums will be added to the "remainder" column.
id | date | type | receiv| payment| remainder
------ | ------ | ------ | ------| ------ | ------
1 | 01.10.2016| Paypal | 50 | 0 | 50
2 | 03.10.2016| credit | 40 | 0 | 90
3 | 05.10.2016| Cash | 50 | 0 | 140
4 | 06.10.2016| payment| 0 | 100 | 40
5 | 07.10.2016| Cash | 20 | 0 | 60
6 | 15.10.2016| Skrill | 10 | 0 | 70
7 | 18.10.2016| payment| 0 | 20 | 50
8 | 19.10.2016| Paypal | 10 | 0 | 60
9 | 19.10.2016| payment| 0 | 20 | 40
10 | 22.10.2016| Cash | 40 | 0 | 80
11 | 23.10.2016| Skrill | 10 | 0 | 90
Running total is easier in other databases which have analytical functions. In MySQL, you can do this with a correlated sub-query.
select id,dt,type,
case when type <> 'payment' then total else 0 end receiv,
case when type = 'payment' then total else 0 end payment,
case when type <> 'payment' then total else 0 end
- case when type = 'payment' then total else 0 end
+ coalesce((select sum(case when type <> 'payment' then total else 0 end)
- sum(case when type = 'payment' then total else 0 end)
from yourtable where id < y.id),0)
from yourtable y
Sample Demo