I have a query stored in MS Access which is doing a standard select from an Access table. I would like to add a summary row at the end showing sums for some of the data above.
I have looked at DSum() but it isn't suitable as I would have to include the running total on each row as opposed to just the end.
Also, note that I don't want to sum data in column a - I would like to get an empty field for the summary of column a.
Example:
a | b | c
-------------
0 | 1 | 2
1 | 1 | 9
| 2 | 11 <-- Sums data above
Does anyone know how this problem can be solved in Access? An alternative might be to define a second query which does the aggregation and then merge it with the recordset of the first one, but this doesn't seem particularly elegant to me.
In SQL server it is apparently possible to use "COMPUTE" or "ROLLUP" but these are not supported under MS Access.
You can use a union query:
SELECT "" As Sort, a,b,c FROM Table
UNION ALL
SELECT "Total" As Sort, Sum(a) As A, Sum(b) As b, Sum(c) As C FROM Table
ORDER BY Sort
EDIT:
SELECT "" As Sort, a,b,c FROM Table
UNION ALL
SELECT "Total" As Sort, "" As A, Sum(b) As b, Sum(c) As C FROM Table
ORDER BY Sort
Related
I have a simple problem that I dont know how to solve in sql.
I have two tables,
cost :
a | b | c
-------+-------+---------------
31.99 | 14.12 | 133.1
second table: income
Party | sum
------+--------
A | 90
B | 12
C | 70
Now i want to get a result that substract for each party A, B, C the income-cost and finds the net value. I cannot compare the column header to column value. I am quite new to this, so I am struggling quite a lot. There should be really easy way of doing this.
I created the 'cost' table by
SELECT sum(A) as A, sum(B) as B, sum(C) as C FROM mytable;
may be there is clever way of creating this table in the same formate as income table that would make it easier to compare? I will appreciate any suggeestion on any of the two fronts. Thanks a lot!
You can compare, using case:
select party,
cost - (case when party = 'a' then a
when party = 'b' then b
when party = 'c' then c
else 0 end)
from cost c cross join
income i
There is a similar question which answer this for a known number of columns and only a single selection column. But the problem here is that
I have no knowledge of columns (count, type) of a specified SQL query and also I want to blank for all columns not a single column.
For example lets say I have following query.
Select * from View1
Result :
Column(1) Column(2) Column(..) Column(N)
1 A Sales 1500
2 C Sales 2500
3 C Sales 2500
4 A Development 2500
Expected result :
Column(1) Column(2) Column(..) Column(N)
1 A Sales 1500
2 C 2500
3
4 A Development
Pseudo SQL Query :
EXEC proc_blank_query_result 'Select * from View1'
If you're in SQL Server 2012 or newer, you can do this with lag, something like this:
select
nullif(column1, lag(column1) over (order by yourorderbyclause)) as column1,
nullif(column2, lag(column2) over (order by yourorderbyclause)) as column2,
...
from
View1
To make it dynamic, well then you have to parse a lot of metadata from the query. Using sp_describe_first_result_set might be a good idea, or use select into a temp. table and parse the columns of it.
I'm looking for a way to perform a SQL type command in Excel. I need to get a count of each string in a column without knowing the string's text before hand.
Here's some sample data, I want to get a count of each Name.
Name
----
A
B
C
A
D
B
IN SQL I'd
SELECT Name, count(*)
FROM #table
group by Name
And I'd expect to get
Name | Count
-----|------
A | 2
B | 2
C | 1
D | 1
How can I perform this operation in Excel?
You could go with the pivot tables that give you some options to analyze your data. There is a good example with explanation on this website: http://www.contextures.com/pivottablecountunique.html
I'm trying to add a column which calculates percentages of different products in MS Access Query. Basically, this is the structure of the query that I'm trying to reach:
Product |
Total |
Percentage
Prod1 |
15 |
21.13%
Prod2 |
23 |
32.39%
Prod3 |
33 |
46.48%
Product |
71 |
100%
The formula for finding the percent I use is: ([Total Q of a Product]/[Totals of all Products])*100, but when I try to use the expression builder (since my SQL skills are basic) in MS Access to calculate it..
= [CountOfProcuts] / Sum([CountOfProducts])
..I receive an error message "Cannot have aggregate function in GROUP BY clause.. (and the expression goes here)". I also tried the option with two queries: one that calculates only the totals and another that use the first one to calculate the percentages, but the result was the same.
I'll be grateful if someone can help me with this.
You can get all but the last row of your desired output with this query.
SELECT
y.Product,
y.Total,
Format((y.Total/sub.SumOfTotal),'#.##%') AS Percentage
FROM
YourTable AS y,
(
SELECT Sum(Total) AS SumOfTotal
FROM YourTable
) AS sub;
Since that query does not include a JOIN or WHERE condition, it returns a cross join between the table and the single row of the subquery.
If you need the last row from your question example, you can UNION the query with another which returns the fabricated row you want. In this example, I used a custom Dual table which is designed to always contain one and only one row. But you could substitute another table or query which returns a single row.
SELECT
y.Product,
y.Total,
Format((y.Total/sub.SumOfTotal),'#.##%') AS Percentage
FROM
YourTable AS y,
(
SELECT Sum(Total) AS SumOfTotal
FROM YourTable
) AS sub
UNION ALL
SELECT
'Product',
DSum('Total', 'YourTable'),
'100%'
FROM Dual;
I have an execl datasheet with data looking like this
id desc part no 1 Qty 1 part no 2 Qty 2 part no 3 Qty 3
1 PartsName 382A012-3-0 3 382A023-3 3 382A012-25 3
And need it to look like this
id desc part no Qty
1 PartsName 382A012-3-0 3
1 PartsName 382A023-3/42-0 3
1 PartsName 382A012-25/86-0 3
This from a SQL Table so I could do it in SQL if that makes it easier
Anybody any suggestions as how to best to sort this?
Simply make a UNION in the SQL
SELECT id, desc, partNo, qty FROM parts
UNION SELECT id, desc, partNo2 as partNo, qty2 as qty FROM parts
UNION SELECT id, desc, partNo3 as partNo, qty3 as qty FROM parts
ORDER BY id
If you don´t have the option of using SQL and need to use Excel. You can use the TRANSPOSE function. It is an array type function so you need to use the {}. If you haven´t used it before I recommend reading the help first. I however don´t think you can use transpose to get it exactly as you describe it. The id and desc column have to handled separately.
You need to add next formulas on a new sheet:
column A=MOD(ROW()+1,3)
it's like a skeleton :)
First row is headers
column B
=IF($A2=0,OFFSET(Sheet1!$A$1,COUNTIF($A$2:$A2,0),COLUMN()-2),B1)
autofil col C with it
column D
=OFFSET(Sheet1!$A$1,COUNTIF($A$2:$A2,0),COLUMN()+CHOOSE($A2+1,-2,0,2))
autofil col E with it
one more - your datasheet is "sheet1"