Is it possible to sum a expression column in access? - ms-access

I have a column in access I need to get the total of. When I try the below method in my query I get the error "You tried to execute a query that does not include the specified expression 'ID' as part of an aggregate function."
Expr1: Sum([Column1])
Edit my query:
SELECT tblTest.ID,Field1,Sum([Field1]) AS Expr1 From tblTest;

Your question needs more details but I think you want this:
SELECT Sum(yourColumn)
FROM yourTable
Then if you need to you will have to add a GROUP BY
SELECT Sum(yourColumn)
FROM yourTable
GROUP BY yourTable.Id
Based on your comment your query would be:
SELECT ID
,Field1
,Sum([Field1]) AS Expr1
From tblTest
GROUP BY ID, Field1;

Related

Using fetched Column value as next parameter for evaluation/comparing in next 'IF' or 'CASE' clause in MYSQL SELECT query

I am trying to create a Flow Control Function query on one of my mysql database. That can fetch some data based on previously fetched column values, before sharing my current query, I am explaining what i am trying to achieve.
As you can see in the below SELECT QUERY the result of first IF CLAUSE lead_id has been used as a parameter in WHERE CLAUSE on third IF CLAUSE and that works perfectly, similarly the result of 3rd IF CLAUSE Cont_id has been used in fourth IF CLAUSE as a WHERE CLAUSE parameter, and that also works as desired, but when we use these returned values lead_id and Cont_id of IF CLAUSES for comparing a value in either IF CLAUSE or CASE CLAUSE, it doesn't work, can anyone help how to achieve this by using a different approach?
My query is like this..
SELECT *,
IF(tte.task_id>0, (SELECT mt.lead_id FROM mt WHERE mt.id=tte.task_id), 'NA') AS lead_id,
IF(tte.task_id>0, (SELECT mt.assigned_to_staff_id FROM mt WHERE mt.id=tte.task_id), 'NA') AS staff_id,
IF(tte.task_id>0, (SELECT leads.contact_id FROM leads WHERE leads.id=lead_id),'NA') AS Cont_id,
IF(tte.task_id>0, (SELECT contacts.Name FROM contacts WHERE contacts.id=Cont_id),'NA') AS PC_Name,
CASE WHEN tte.task_id>0 THEN (SELECT contacts.Name AS LC_Name FROM contacts WHERE contacts.id=PC_id) ELSE 'NA' END,
FROM tte ORDER BY request_log_time DESC;
I want to use returned values lead_id and Cont_id of IF CLAUSES for comparing a value in either IF CLAUSE like this:
IF(Cont_id>0, (SELECT contacts.Name FROM contacts WHERE contacts.id=Cont_id),'NA') AS PC_Name
or CASE CLAUSE like this:
CASE WHEN Cont_id>0 THEN (SELECT contacts.Name AS LC_Name FROM contacts WHERE contacts.id=Cont_id) ELSE 'NA' END

MySQL: Get total each row then get total each 'group by'

Sorry about the title, I don't know what's the proper term to use.
I have this data:
I want to produce a table something like this:
I want to get the total of each 'group by' and I'm doing this code but I'm getting wrong total.
SELECT code, (a * b)
AS total_each_code
FROM table1
GROUP BY code
UPDATE: updated photos of sample data, sorry for typos.
you can use sub query for this
select code, sum(a.total_each_code)
from (
SELECT code, (a * b)
AS total_each_code
FROM table1 order by code
)a
group by a.total_each_code
or just simple as
select code,sum(a*b) as total from table1 group by code.
Use sum() aggregation
SELECT code, sum(a * b)
AS total_each_code
FROM table1
GROUP BY code

MySQL subquery on itself returns all records

The following query returns all results, but according to my understanding should return the same ID's as the subquery. Could somebody explain why the subquery that returns all ID's (when ran separately) returns all records
select mya.id from mytable mya WHERE mya.id IN (
SELECT myb.id
FROM mytable myb
GROUP BY myb.mysecondcolumn
)
The subquery when ran as individual query would for example return 1,5,10,15, but when I run this query above it returns 1,2,3,4,5,...
Thanks!
Your query is malformed. You need an aggregation function in the subquery. Perhaps:
select mya.id
from mytable mya
where mya.id in (SELECT MIN(myb.id)
FROM mytable myb
GROUP BY myb.mysecondcolumn
);
This does not explain your actual problem. My guess is that the subquery is returning all ids, but just in a different order. You can check if this is the case by looking at the results of:
SELECT MIN(myb.id)
FROM mytable myb
GROUP BY myb.mysecondcolumn
ORDER BY MIN(myb.id)

Avoiding redundant expressions in SELECT query

Is there any way to avoid repeating column expressions in the SELECT query? I want to divide the sum and count of a column but would like to use the assigned name instead of repeating SUM(value)/COUNT(value) or using a sub query. Is this possible? If so, does that speed up the query by not repeating the calculation of the sum and count or does mysql remember already calculated expressions?
SELECT datalist.type, SUM(value) AS type_sum, COUNT(value) AS type_count, type_sum/type_count
FROM (...) AS datalist
GROUP BY datalist.type
throws: #1054 - Unknown column 'type_sum' in field list
Unless you put it in outer query, this is the only way.
SELECT datalist.type, SUM(value) AS type_sum, COUNT(value) AS type_count, SUM(value)/COUNT(value)
FROM (...) AS datalist
GROUP BY datalist.type
One workaround would be to use a alias table with pre-defined calculations and then later call it from outer table such as:
select d.type_sum/d.type_count as dividedValue from (SELECT datalist.type, SUM(value)
AS type_sum, COUNT(value) AS type_count
FROM (...) )AS d
GROUP BY d.type

Access: Use scalar correlated subquery result for sorting

In Access, is it possible to use the result of a scalar correlated subquery to sort the resultant rows? This didn't work (returned a sql error):
SELECT (SELECT MIN(DateTime) FROM Appt WHERE (PatientID = XXX.ID)) AS minDT, XXX.FullName,
FROM Patient AS XXX
ORDER BY minDT;
Replacing the "minDT" in the ORDER BY clause with the entire expression didn't work either.
Access won't allow you to use a field expression alias in the ORDER BY, but you can use a column's ordinal number ...
SELECT *
FROM
(
SELECT
(
SELECT MIN([DateTime])
FROM Appt
WHERE (PatientID = XXX.ID)
) AS minDT,
XXX.FullName
FROM Patient AS XXX
) AS sub
ORDER BY 1;
Note I bracketed the DateTime field name because I suspect it's a reserved word.
And don't include a comma after the last item in the SELECT field expression list.