sum of 2 different columns in MSSQL - sql

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

Related

Merge row values based on other column value

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;

Select rows with max date from table

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

SQL Query -- AVG for Occurrence Across Multiple Columns

I have a table like the following
ID_A
ID_B
Avg_Class_Size
1
2
16
3
4
10
2
3
8
2
4
9
Where ID_A and ID_B represent distinct student ID codes, and AVG_Class_Size represents the average class size of the classes shared between students A and B.
I would like to calculate the average of the "avg_class_size" for each student, regardless of whether they are student "A" or student "B", with results like below:
ID
AVG
1
16
2
11
3
9
4
9.5
Is there a simple way to accomplish this with a SQL query?
Select with UNION ALL all the ids and averages of the students and aggregate:
SELECT ID, AVG(Avg_Class_Size) average
FROM (
SELECT ID_A ID, Avg_Class_Size FROM tablename
UNION ALL
SELECT ID_B ID, Avg_Class_Size FROM tablename
) t
GROUP BY ID
See the demo.
Results:
ID
average
1
16
2
11
3
9
4
9.5

Selection of Data From Multiple Tables having Relations

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

SQL - How to Count from multiple tables and add them together

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