Sum fields from mutiple tables with conditions - sql

I'm working on a quite big project and I need to compute some values through SQL. I have 3 datasets with quite identical fields :
Query1 : txt groupName, int cat, long hoursSubTot1
Query2 : txt groupName, int cat, long hoursSubTot2
Query3 : txt groupName, int cat, long hoursSubTot3
I need to add hoursSubTot1,hoursSubTot2 and hoursSubTot3 like that :
I would like the datas to be agregate as shown in the result table. But I do not manage to agregate the right values in Access, I am out of ideas so any help or advice would be welcome...
Thanks!

If you're not opposed to using multiple queries, here's my thought. First create a query using UNION ALL to combine the three queries. Then you can create a new query to calculate the sums.
SELECT Group, Category, Hours FROM Table1
UNION ALL
SELECT Group, Category, Hours FROM Table2
UNION ALL
SELECT Group, Category, Hours FROM Table3

ANSI SQL answer, may or may not work for Access:
select group, category, sum(hours)
from
(
select group, category, hours from table1
union all
select group, category, hours from table2
union all
select group, category, hours from table3
)
group by group, category

Related

Removing doubling lines

I have written a union query but I need to eliminate the lines that are duplicated (line 2 and 3 in the column 'kods') and leave only distinct values of column 'kods'. How can that be done?
You need to decide which of the id values to discard using either min or max and group by the remaining columns. you don't need distinct and can union all since group by will perform the dedupe.
select kods, min(id) id, vards, uzvards from (
select kods, id, vards, uzvards
from dataset
union all
select kods, id, vards, uzvards
from dataset_2
)x
group by kods, vards, uzvards

How to pass the output of the subquery into the main query so it could count the items from a separate table

Below is what I am trying to achieve. I have a database 'CARS_DATABASE', wherein there are the following tables:
Lorries
Vans
Buses
Convertibles
City_cars
Stretch_limos
Rescue_vehicles
Double_decker_buses
First of all, I am going to run the following query to get the list of the tables:
SELECT TableName
FROM
(SELECT TableName
FROM DBC.TablesV
WHERE DatabaseName = 'CARS_DATABASE';
Now, I would like to incorporate it into one query which would look as follows:
Select count(*) from CARS_DATABASE.**TableName**
WHERE TableName = (SELECT TableName
FROM
(SELECT TableName
FROM DBC.TablesV
WHERE DatabaseName = 'CARS_DATABASE') Q1);
I wonder how I can make this dynamic and pass into FROM clause the names of all tables. Essentially, I would like it to select from all those tables dynamically, e.g.
Select count(*) from CARS_DATABASE.Lorries
Select count(*) from CARS_DATABASE.Vans
Select count(*) from CARS_DATABASE.Buses
and so on.
Do I have to write a macro or stored procedure to achieve that?
Many thanks in advance for your suggestions.
Cheers
Andy
Have a derived table (the subquery), where you SELECT and UNION ALL the different vehicle tables. GROUP BY the result.
select vehicle_type, count(*)
from
(
select 'lorries' as vehicle_type from CARS_DATABASE.Lorries
union all
select 'vans' as vehicle_type from CARS_DATABASE.vans
union all
...
select 'Double_decker_buses' as vehicle_type from CARS_DATABASE.Double_decker_buses
) vehicles
GROUP BY vehicle_type
Note: I'd store all vehicles in one common table. If you're stuck with separated tables, you could create a view to UNION ALL the different tables.

Query two tables as one from different namespaces

I have two tables in a PostgreSQL database that contains two tables that differ only by namespace: Japan.Revenue and Korea.Revenue. I want to query them as there is one table only.
E.g
SELECT productgroup, sum(transactionvalue)
FROM ???? (Something that combines the two tables)
Group by productgroup where ....
How do I do that?
I need the Korea and Japan results combined. So far I did sent 2 queries and combined the result in the app. There must be a better way
You need to fully-qualify the table names and union the results.
It's possible to do more than a simple select for the inner queries, but because you are doing an aggregation, you need to do it outside.
https://www.postgresql.org/docs/current/static/queries-union.html
SELECT productgroup, sum(transactionvalue)
FROM (
SELECT * FROM Japan.Revenue
UNION ALL
SELECT * FROM Kora.Revenue
) sub
GROUP BY productgroup
WHERE ....
;
Edit: Now using UNION ALL from comments below.
try this:
SELECT productgroup, sum(transactionvalue)
FROM korea
Group by productgroup
union all
SELECT productgroup, sum(transactionvalue)
FROM japon
Group by productgroup
when you have same column with same type best way to combine is union
and you can group by it outside of select too.

Join two select statements together

I am trying to work out how much we have taken in for entry fees.
I have two separate queries both returning values but i need them be as one instead of two separate queries.
SELECT SUM(ENTRY) AS TOTAL1 FROM MONEY
SELECT SUM(ENTRY) AS TOTAL1 FROM MONEY2
I needed to use UNION in order to get the statements together. Then used the below to get one number.
SELECT SUM(X.TOTAL1) from
(
SELECT SUM(ENTRY) AS TOTAL1 FROM MONEY
UNION
SELECT SUM(ENTRY) AS TOTAL1 FROM MONEY2
) X;
select sum(entry) as grand_total
from ( select entry from money
union all
select entry from money2
);
The point being, you SHOULD use UNION ALL; and how many columns each table has is irrelevant, because you don't need to UNION ALL the two tables (all columns from each); you only need to UNION ALL the ENTRY column from the first table and the ENTRY column from the second table.

combine SELECTS in ONE VIEW DISPLAY

I need to know of a way to combine multiple SELECT statements in one VIEW? I tried the UNION ALL, but it fails since I am using unique columns to aggregate the GRAND TOTAL.
I am a student this is part of a group project.
I have one table with 4 columns: account, description, short_description, and balance. The COA (chart of accounts) is an excel spreadsheet that is imported.
CREATE VIEW [account_balance_sums]
AS
SELECT SUM(balance) AS total,
SUBSTRING (Account,0,2) AS account_group
FROM COA
GROUP BY account_group
GO
SELECT * FROM [account_balance_sums]
SELECT SUM(total) AS Grand_total
FROM [account_balance_sums]
Assuming that you are trying to create a view that gives account group and total balance with a single extra row for the total across all accounts then this view should help:
CREATE VIEW [account_balance_sums] AS
SELECT SUM(balance) AS total, SUBSTRING (Account,0,2) AS account_group
FROM COA
GROUP BY account_group
UNION ALL
SELECT SUM(balance), 'Grand Total'
FROM account_group
By the way, the sub-string of the first characters of the account name suggests that you have more than one piece of data in a single column. This indicates a data that is not properly normalised, which you should probably address if you want top marks. See wikipedia on normal form
In a UNION'd statement, there must be:
The same number of columns in each SELECT statement
The data types must match at each position in the SELECT statement
Use:
SELECT *
FROM [account_balance_sums]
UNION ALL
SELECT SUM(total),
NULL AS account_group
FROM [account_balance_sums]
UNION ALL should work. basic structure like this
select a,b,c,d
from t1
union all
select a,b,c,e
from t2
so long as d and e are the same data type.
to do the sum, then you wrap this with the aggregation layer - using this structure as an inline view (among other methods)
something like:
select sum( d )
from (
select a,b,c,d
from t1
union all
select a,b,c,e
from t2
)