get balance from entities for all customers - sql

I have table account
id - name
1 - Lab
2 - Sara
3 - Jone
table entities
id - credit - debit - value
1 1 3 10
1 1 2 20
1 3 1 5
1 2 1 3
I want the balance for all customers the output :
Name - balance
Sara 17
Jone 5

select account.name, (plus.balance - minus.balance) as balance
from accounts inner join
(select credit as id, sum(value) as balance from entities group by credit) as plus
inner join
(select debit as id, sum(value) as balance from entities group by debit) as minus
on plus.id = minus.id and accounts.id = plus.id
explanation- I'm making two copies from entities table- one to sum the credit (per id) and one to sum the debit, and then joins both of them with the accounts table to get the name. Not sure what you do with the id column in the entity table, seems redundant.

didn't work the errur invalid token ')' at line 6
select "Accounts".name, (plus.balance - minus.balance) as balance
from
"Accounts" inner join
(select public.entities.accounts_id_credit as id, sum(value) as balance from public.entities group by public.entities.accounts_id_credit) as plus
inner join
(select public.entities.accounts_id_debit as id, sum(value) as balance from public.entities group by public.entities.accounts_id_debit) as minus
on plus.id = minus.id and "Accounts".id = plus.id

Related

SQL get table1 names with a count of table2 and table3

I have three tables, table1 is connected to table2 and table3, but table2 and table3 are not connected. I need an output count of table2 and table3 for each table1 row. I have to use joins and a group by table1.name
SELECT Tb_Product.Name, count(TB_Offers.Prod_ID) 'Number of Offers', count(Tb_Requests.Prod_ID) 'Number of Requests'
FROM Tb_Product LEFT OUTER JOIN
Tb_Requests ON Tb_Product.Prod_ID = Tb_Requests.Prod_ID LEFT OUTER JOIN
TB_Offers ON Tb_Product.Prod_ID = TB_Offers.Prod_ID
GROUP BY Tb_Product.Name
I need to combine these queries:
SELECT Tb_Product.[Name], count(TB_Offers.Prod_ID) 'Number of Offers'
FROM Tb_Product LEFT OUTER JOIN
TB_Offers ON Tb_Product.Prod_ID = TB_Offers.Prod_ID
GROUP BY Tb_Product.[Name]
SELECT Tb_Product.[Name], count(Tb_Requests.Prod_ID) 'Number of Requests'
FROM Tb_Product LEFT OUTER JOIN
Tb_Requests ON Tb_Product.Prod_ID = Tb_Requests.Prod_ID
GROUP BY Tb_Product.[Name]
Results:
Name Number of Offers
Airplane 6
Auto 5
Bike 3
Camera 0
Computer 12
Milk 4
Oil 4
Orange 6
Telephone 0
Truck 6
TV 4
Name Number of Requests
Airplane 1
Auto 5
Bike 0
Camera 2
Computer 6
Milk 4
Oil 5
Orange 6
Telephone 0
Truck 1
TV 5
My results for offers and requests are the same value. I am not sure what I am doing wrong with the joins. Do I need to somehow join product to request and separately join product to offers? This needs to be done in one query.
This is for a class. Explanation would also be appreciated.
The simplest way to do this is to count the distinct values of each column:
SELECT
Tb_Product.Name,
count(distinct TB_Offers.Prod_ID) 'Number of Offers',
count(distinct Tb_Requests.Prod_ID) 'Number of Requests'
FROM
Tb_Product
LEFT OUTER JOIN
Tb_Requests ON Tb_Product.Prod_ID = Tb_Requests.Prod_ID
LEFT OUTER JOIN
TB_Offers ON Tb_Product.Prod_ID = TB_Offers.Prod_ID
GROUP BY
Tb_Product.Name
This is necessary because of the way joins work consecutively to produce a rowset that is a combination of all the input relations. COUNT() normally performs a count of non-null values in a column.
You can also do something like this, which aggregates the counts from the child tables independently and then joins them to the base table:
SELECT
p.Name,
o.cnt as Offer_Count,
r.cnt as Request_Count
FROM
TB_Product p
LEFT OUTER JOIN
(SELECT Prod_ID, COUNT(1) cnt FROM TB_Offers GROUP BY Prod_ID) o
LEFT OUTER JOIN
(SELECT Prod_ID, COUNT(1) cnt FROM TB_Requests GROUP BY Prod_ID) r
More explanation...
Let's say you have two products:
Prod_ID
Name
1
Widget
2
Gizmo
And two offers, one for each product:
Offer_ID
Prod_ID
100
1
200
2
And two requests for each product:
Request_ID
Prod_ID
1001
1
1002
1
2001
2
2002
2
Now you join Product relation to Offer relation on Prod_ID, you get a result like this:
Prod_ID
Name
Offer_ID
Prod_ID
1
Widget
100
1
2
Gizmo
200
2
Now when you join that relation to Requests on Prod_ID, you get something like this:
Prod_ID
Name
Offer_ID
Prod_ID
Request_ID
Prod_ID
1
Widget
100
1
1001
1
1
Widget
100
1
1002
1
2
Gizmo
200
2
2001
2
2
Gizmo
200
2
2002
2
Now when you count any of these columns you get 4 because each column has 4 values.

Selecting values in columns based on other columns

I have two tables, info and transactions.
info looks like this:
customer ID Postcode
1 ABC 123
2 DEF 456
and transactions looks like this:
customer ID day frequency
1 1/1/12 3
1 3/5/12 4
2 4/6/12 2
3 9/9/12 1
I want to know which day has the highest frequency for each postcode.
I know how to reference from two different tables but im not too sure how to reference multiple columns based on their values to other columns.
The output should be something like this:
customer ID postcode day frequency
1 ABC 123 3/5/12 4
2 DEF 456 4/6/12 2
3 GHI 789 9/9/12 1
and so on.
You can filter with a correlated subquery:
select
i.*,
t.day,
t.frequency
from info i
inner join transactions t on t.customerID = i.customerID
where t.frequency = (
select max(t.frequency)
from info i1
inner join transactions t1 on t1.customerID = i1.customerID
where i1.postcode = i.postcode
)
Or, if your RBDMS supports window functions, you can use rank():
select *
from (
select
i.*,
t.day,
t.frequency,
rank() over(partition by i.postcode order by t.frequency desc)
from info i
inner join transactions t on t.customerID = i.customerID
) t
where rn = 1

SQL get a column as comma-seperated values from a subquery

I have these tables in my postresql:-
1.Payment(id, number, amount)
2.Invoice(id, number, amount)
and a invoice_payment table to allocate payments amount to invoices
3.InvoicePayment(id, payment_id, invoice_id, amount)
Single payment can be allotted to many invoices.
Now I want payment details, but I also want invoices.number of the invoices this payment is allotted to. I am doing something like this:-
SELECT
payments.number,
payments.amount,
(SELECT invoices.number from invoices INNER JOIN invoice_payments
ON invoices.id = invoice_payments.invoice_id
WHERE invoice_payments.payment_id = payments.id)
from payments
But it gives me error more than one row returned by a subquery. How do I make the invoice number comma-seperated for every payments row?
Sample data:-
Payment
id number amount
1 "Pay001" 100
2 "Pay002" 150
3 "Pay003" 150
Invoice
id number amount
1 "INV001" 100
2 "INV002" 200
3 "INV003" 100
InvoicePayment
id payment_id invoice_id amount
1 1 1 50
2 1 2 50
3 2 2 150
4 3 1 50
5 3 3 100
Result:-
payment_id payment_number amount invoices
1 "Pay001" 100 "INV001, INV002"
2 "Pay002" 150 "INV002"
3 "Pay003" 150 "INV001, INV002"
You can use string_agg function in postgres to concatenate rows, separated by chosen delimiter.
SELECT
payments.number,
payments.amount,
(SELECT string_agg(invoices.number,',') from invoices INNER JOIN invoice_payments
ON invoices.id = invoice_payments.invoice_id
WHERE invoice_payments.payment_id = payments.id)
from payments
Try this
SELECT
payments.number,
payments.amount,
inv_p.inv_no
from payments p inner join (Select invoice_payments.payment_id, string_agg(invoices.number,',') as inv_no
From invoices inner join invoice_payments on (invoices.id = invoice_payments.invoice_id)
Group by invoice_payments.payment_id) inv_p on (p.id = inv_p.payment_id)

Left join to a table where values do not exist (and are not NULLs)

EDIT (SOLVED): A cross join. One of those joins you never use until you need it. Thanks for the help
Left table: TS , single field with values [1,2,...,365].
Right table: PAYMENT with three fields (ID, TS, AMT)
For each ID, I want to see 365 records from a left join of TS on PAYMENT.
The problem is that "no value" is not the same as a NULL.
If PAYMENT.TS does not exist for a certain value (e.g. PAYMENT.TS=4), then there is no value to join on and the left join does not return a row #4.
I tried using NOT IN / NOT EXISTS as a condition, but this only treats the case where the right table has explicit NULLS and not the case where no value exists.
How can I proceed? Thanks!
(This is a DB2 system)
SELECT * FROM TS LEFT JOIN PAYMENT ON TS = PAYMENT.TS
TS TABLE:
| TS |
----------
1
2
...
365
PAYMENTS TABLE:
| ID | TS | PMT |
-----------------------------
1 1 70
1 2 20
1 5 10
2 3 200
EXPECTED RESULT:
| ID | TS | PMT |
-----------------------------
1 1 70
1 2 20
1 3
1 4
1 5 10
... ...
1 365
2 1
2 2
2 3 200
... ...
2 365
ACTUAL RESULT:
| ID | TS | PMT |
-----------------------------
1 1 70
1 2 20
1 5 10
2 3 200
You need to generate all the rows you want using a cross join and then use left join:
SELECT i.id, ts.ts. p.amt
FROM (SELECT DISTINCT ID FROM PAYMENT) i CROSS JOIN
TS LEFT JOIN
PAYMENT p
ON ts.TS = p.TS AND p.id = i.id;
This will return 365 rows for each id.
You have to join them matching the two common columns in each table. Preferably by the keys(foreign and primary).
Let's say TS table has this one column called 'NUMBERS' and its type is int. The table PAYMENT has the column ID, type of int also. Which means they may have common values. Thus, if you want to join two tables and get the common ones where the PAYMENT.ID exists in TS.NUMBERS then you should do:
SELECT * FROM TS LEFT JOIN PAYMENT ON TS.NUMBERS = PAYMENT.ID
I hope I've been clear.
Note: Also do not forget that if a column or more has the same name in both tables, you have to clarify from which table you want that column for instance if also PAYMENT table had the column named as NUMBERS, then:
SELECT PAYMENT.ID, TS.NUMBERS FROM TS LEFT JOIN PAYMENT ON TS.NUMBERS = PAYMENT.ID

SQL- SELECT minimum by other column

Let's have a table named employers with columns:
id
cash
and I need to select id and cash for employers that have minimal cash lower than minimal cash in rows with id = 2.
An example of the table:
----------
id cash
----------
1 100
2 200
2 150
2 125
3 320
4 400
It should select only the first row, because minimal cash in rows with id 2 is 125 and only this row has lower cash.
I tried something like this but it didn't work:
SELECT id, MIN(cash)
FROM employers
WHERE cash < (MIN(cash)
WHERE id = 2;
Try the following query
SELECT id, cash
FROM employers
WHERE cash < (SELECT MIN(cash)
FROM employers
WHERE id = 2
GROUP BY id);
EDIT: Removed the MIN from the query, this should work with your data sample.
Also I suggest that you modify the query based on your requirements.
In case you need only the least cash available at every employer, who has less cash than the min cash, then use a GROUP BY id and select MIN(cash).
SELECT T1.ID, min(T1.cash), T2.Id, min(T2.Cash)
FROM Table T1
CROSS JOIN table T2
WHERE T1.CASH < T2.CASH
and T2.ID = 2
and T1.ID <> 2
GROUP BY T1.ID, T2.ID