I need to select data from Multiple Table having Relation like
A->B->C (A have one-to-many relationship with B and B have many-to-one relationship with C).
As in given Picture of Database, I need to select sum of total_amount from petty_claim and vendors_claim on a specific Expense type and specific office(Invoice_main) group by Month(Invoice_main.date) click here to view picture
Given Below are exemplary tables having no real values but similar to original tables. All Values are Random.
Need Help for this. Thanks for your time
Table: Invoice_main
Columns: invoice_ID Office_ID Date
row: 1 2 2018-05-5
row: 2 1 2018-04-4
row: 3 1 2018-05-3
Table: Office
Columns: office_ID Office
row: 1 Support
row: 2 HR
row: 3 Billing
Table: Petty_claim
Columns: invoice_id Expense_id Amount
row: 1 2 1000
row: 2 3 1000
Table: Vendors_claim
Columns: invoice_id Expense_id Amount
row: 3 1 2000
row: 4 3 2000
Table: Expense_Types
Columns: expense_id dept_id
row: 1 Type A
row: 2 Type B
row: 3 Type C
OUT PUT Required
Month Expense_Type Office_1 Office_2
1 Type A 1000 2000
2 Type B 2033 1034
It will be something like this:
SELECT SUM(invoice.total_amount)
FROM (SELECT date, total_amount
FROM invoice_main main , invoice_petty_claim petty, invoice_expense_type expense_type
WHERE main.office = 'XXX' and main.id=petty.invoice_id and petty.expense_type = expense_type.type
UNION ALL
SELECT date, total_amount
FROM invoice_main main, invoice_vendors_claim vendor, invoice_expense_type expense_type
WHERE main.office = 'XXX' and main.id=vendor.invoice_id AND vendor.expense_type = expense_type.type
) invoice
GROUP BY invoice.date
Related
I'm trying to merge the values of two rows based on the value of another row in a different column. Below is my based table
Customer ID
Property ID
Bookings per customer
Cancellations per customer
A
1
0
1
B
2
10
1
C
3
100
1
C
4
100
1
D
5
20
1
Here is the SQL query I used
select customer_id, property_id, bookings_per_customer, cancellations_per_customer
from table
And this is what I want to see. Any ideas the query to get this would be? We use presto SQL
Thanks!
Customer ID
Property ID
Bookings per customer
Cancellations per customer
A
1
0
1
B
2
10
1
C
3 , 4
100
1
D
5
20
1
We can try:
SELECT
customer_id,
ARRAY_JOIN(ARRAY_AGG(property_id), ',') AS properties,
bookings_per_customer,
cancellations_per_customer
FROM yourTable
GROUP BY
customer_id,
bookings_per_customer,
cancellations_per_customer;
I have such table and need table 2 result. I am trying to select rows with max date grouped by project_id and ordered by id. And result table must have id column. Tried such request:
SELECT MAX(charges.id) as id,
"charges"."profile_id", MAX(failed_at) AS failed_at
FROM "charges"
GROUP BY "charges"."profile_id"
ORDER BY "charges"."id" ASC
And have error:
ERROR: column "charges.id" must appear in the GROUP BY clause or be used in an aggregate function)
Example table
id
profile_id
failed_at
1
1
01.01.2021
2
1
01.02.2021
3
1
01.03.2021
4
2
01.06.2021
5
2
01.05.2021
6
2
01.04.2021
Needed result
id
profile_id
failed_at
3
1
01.03.2021
4
2
01.06.2021
SELECT charges.*
FROM charges
INNER JOIN
(
SELECT
profile_id,
MAX(charges.failed_at) AS MaxFailed_at
FROM charges
GROUP BY profile_id
) AS xQ ON charges.profile_id = xQ.profile_id AND charges.failed_at = xQ.MaxFailed_at
I have two tables, expenses and categories, they have a many-to-many relationship through the table expenses_categories. I'm trying to implement a filter by categories, lets say that I provided the id for the categories A and B, I want to return the expenses who only have A and B. For example:
Expense X have Category A, B, and C
Expense Y have Category A and B
Expense Z have Category B
I want to return only the Expense Y
I'm using PostgreSQL by the way. I really need to learn how to do this kind of stuff.
Categories
ID
NAME
1
TV
2
CC
3
NET
ExpensesCategories
expense_id
category_id
1
1
1
2
2
1
2
2
2
3
3
1
4
2
I want to get all the Expenses that have ONLY the Categories 1 and 2.
In that case, I expect to only get the Expense 1
expense_id
category_id
1
1
1
2
You can group by expense_id and use STRING_AGG() in the HAVING clause to collect all the category_ids of each expense_id and compare it to a string like '1,2' which contains the category_ids that you want in ascending order as a comma separated list:
SELECT expense_id
FROM ExpensesCategories
GROUP BY expense_id
HAVING STRING_AGG(category_id::text, ',' ORDER BY category_id) = '1,2';
If you want all the rows of these expense_ids in ExpensesCategories, use the above query as a CTE:
WITH cte AS (
SELECT expense_id
FROM ExpensesCategories
GROUP BY expense_id
HAVING STRING_AGG(category_id::text, ',' ORDER BY category_id) = '1,2'
)
SELECT *
FROM ExpensesCategories
WHERE expense_id IN (SELECT expense_id FROM cte);
See the demo.
I have 2 tables A and B. The columns names are similar in both the tables. The columns are
1. fees
2. user_id
I want to get the sum of fees from both tables where user_id = 1
For eg:
Table A:
id user_id fees
1 1 10
2 2 11
3 1 5
Table B:
id user_id fees
1 1 15
2 2 10
3 1 20
I need the result as below:
user_id fees
1 50
2 21
Please help me with the query
Try this:
select user_id, sum(fees) from (
select user_id, fees from Table_A
union all
select user_id, fees from Table_B
) as A
group by user_id
I am trying to do a count from multiple tables but there could be multiple entries in each table. Here is simple sample data simplified. There are actually more then 3 tables but just so I get an understanding of how to do it
table2 table2 table3
person_ID person_id person_id
1 1 2
2 1 2
3 2 1
4 2 4
5 4 5
I'm trying to get a count of each person ID in each table so the output would be the following. Note that personID is a key I don't need the addition of the number of the ID not 2+2+2+2. But the count of the number of appearances it makes in the all tables then the count in each table added together for total number of appearances it makes. Basically I'm trying to find a total number of items attached to each personID
person_id total
1 4
2 4
3 1
4 3
5 2
Select the ids from all the tables together withunion. That result can be grouped by the id and counted for each
select person_id, count(*) as count
from
(
select person_id from table1
union all
select person_id from table2
union all
select person_id from table3
) tmp
group by person_id