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
)
Related
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.
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.
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
i got a question here.
I'm trying to select all the date inside my database (1-31), instead of using 1,[2],[3]... manually way is there any easier way to select them all?
I tried using some dumb way like:
SUM(TOTAL) FOR DATE IN (*)
SUM(TOTAL) FOR DATE IN ([*])
Here is my query:
SELECT * FROM
(
SELECT BRANCH.NAME,SALES.TOTAL AS TOTAL,TIME.DATE
FROM SALES
INNER JOIN BRANCH
ON SALES.BRANCH_ID=BRANCH.BRANCH_ID
INNER JOIN TIME
ON SALES.TIME_ID=TIME.TIME_ID
WHERE TIME.MONTH='APR'
)AS TABLE1
PIVOT (
SUM(TOTAL) FOR DATE IN ([1],[2],[3],[5],[6],[7],[8],[9],[10])
) PIVOTTABLE
Also is there possible to create an extra grand total column by SQL like excel do ?
SAMPLE:
I have a question on the sql IN query. For example you have table with columns id, amount name.
With a query:
SELECT SUM(amount) FROM table WHERE id IN (101,101);
What I want with this is to add amount of a certain id. Whatever the id is inside the IN statement. If like this, two 101, amount of 101 + amount of 101.
The problem is it consider it is one instance. How do I do this? its suppose to be:
SELECT SUM(amount) FROM table WHERE id IN (SELECT id FROM table.........);
Which the sub select return "101, 101".
How
SELECT SUM(tbl.amount)
FROM tbl
JOIN (select 101 id UNION ALL
select 101) InList on InList.id = tbl.id
Expand this way.
I do something like this
Select * From table1
inner join dbo.fnSplit(#ArgList, ',')
That would definitely work for me