MS access query optimizing - ms-access

I got issue with access after new computer.That new computer have less GHz in single core that older and access using only one core.
I find out most of load takes one query that counts all balances and can not think how to count all balances some another way so computer would not take 4 sec to calculate all data eaxh time this query in use.
Code that I use now for query
PARAMETERS KodMater Text ( 255 );
SELECT
DISTINCTROW Mater.NumberMat
, IIf(IsNull([Query Sum Likuc]!SumOfKiekPajam),0,[Query Sum Likuc]!SumOfKiekPajam)
+IIf(IsNull([Query sum Pajam]!SumOfKiekPajam),0,[Query sum Pajam]!SumOfKiekPajam)
-IIf(IsNull([Query Sum Nuras]!SumOfKiekNuras),0,[Query Sum Nuras]!SumOfKiekNuras)
-IIf(IsNull([Query Sum Pard]!SumOfKiekNuras),0,[Query Sum Pard]!SumOfKiekNuras)
-IIf(IsNull([Query Sum PardV]!SumOfKiekNuras),0,[Query Sum PardV]!SumOfKiekNuras
) AS Expr1
FROM
((((Mater
LEFT JOIN [Query Sum Likuc] ON Mater.NumberMat = [Query Sum Likuc].NumberMat)
LEFT JOIN [Query sum Pajam] ON Mater.NumberMat = [Query sum Pajam].MaterID)
LEFT JOIN [Query Sum Pard] ON Mater.NumberMat = [Query Sum Pard].MaterID)
LEFT JOIN [Query Sum Nuras] ON Mater.NumberMat = [Query Sum Nuras].MaterID)
LEFT JOIN [Query Sum PardV] ON Mater.NumberMat = [Query Sum PardV].MaterID
WHERE (((Mater.NumberMat) Like [KodMater]));
Code that I use rezult
Query data without merge

Related

sql query group by not working in subquery

I have a sql query based on 2 IDs in the same table, the results of the sum have come out correctly, but the problem is group by cannot handle data that appears 2 times
SELECT coa_a.debet_april,coa_a.namacoaapril, coa_b.kredit_april ,coa_b.namacoaapril
FROM `t_jurnalumum` join coa on coa.m_coa_4_id=t_jurnalumum.IdDebet
join (select DISTINCT m_coa_4_id, sum(a.Nilai) as debet_april, coa.namacoa as namacoaapril
from t_jurnalumum a j
join coa on a.IdDebet=coa.m_coa_4_id
where year (a.Tanggal)=2021 and month (a.Tanggal)=4
GROUP by a.IdDebet ) as coa_a on coa_a.m_coa_4_id=t_jurnalumum.IdDebet
join (select DISTINCT m_coa_4_id, sum(b.Nilai) as kredit_april, coa.namacoa as namacoaapril
from t_jurnalumum b
join coa on b.IdKredit=coa.m_coa_4_id
where year (b.Tanggal)=2021 and month (b.Tanggal)=4
GROUP by b.IdKredit ) as coa_b on coa_b.m_coa_4_id=t_jurnalumum.IdKredit
GROUP by coa_b.namacoaapril, coa_a.namacoaapril
this the result
and this is the main table
It is because you are using the same table. The first sub query of join will give you the distinct value from first group by and second group by gives distinct value. And at the end you also have grouped the whole result. The first one gives two distinct value and second one gives two distinct value which eventually gives all value.

I was wondering if anyone knew how to find the min value and a max value and find the timeframe between them?

im working with a movie database I made and I wanted to make a select query that would separately select the the movie with the highest revenue and the movie with the lowest value and find the timeframe between them
i've tried to use the min and max functions to try to separately select the lowest and the highest movies and tried to use datediff() to try and the timeframe between them. my code is below
SELECT titles.title, min(financial_info.revenue),max(financial_info.revenue),
DATEDIFF(year, date(min(financial_info.revenue), date(max(financial.revenue)))
production_company.release_date
from titles
left join financial_info on titles.id = financial_info.id
left join production_company on
titles.imdb_id = production_company.imdb_id
I had aggregation error and a syntax error
The general format for this kind of query would be:
SELECT stuff
, maxB.Y - minB.Y -- Or, in this case DATEDIFF instead of minus
FROM (SELECT MIN(x) AS minX, MAX(x) AS maxX FROM a) AS minMax
INNER JOIN a AS minA ON minMax.minX = minA.X
INNER JOIN a AS maxA ON minMax.maxX = maxA.X
INNER JOIN b AS minB ON minA.b_id = minB.b_id
INNER JOIN b AS maxB ON maxA.b_id = maxB.b_id
;
You can end up with more than one result if there are multiple entries with the same revenue amount. (2 at min revenue and 3 at max revenue would yield 6 results).

MySql - Sum data from Table, with aggregated data from sub-tables - performance issue

i have the following query
select q.has_content_violation,
if(q.max_finding_status = 1, 1,
if(q.min_finding_status >=3, 3, 2)) scan_item_finding_status,
count(*)
from (
SELECT si.id, if(f.id is not null, true, false) has_content_violation,
min(f.finding_status) min_finding_status,
max(f.finding_status) max_finding_status
FROM bank b
JOIN merchant m ON m.bank_id = b.id
JOIN merchant_has_scan ms ON m.last_scan_completed_id = ms.id
JOIN scan_item si ON si.merchant_has_scan_id = ms.id
LEFT JOIN finding f ON f.scan_item_id = si.id
WHERE f.finding_type = 1
GROUP BY si.id
) q
GROUP BY scan_item_finding_status, q.has_content_violation
the query takes data from table finding and agregate it for scan item, finding if it has several key parameters like finding_type=1 and using the left join to find if the scan_item has findings in the sub-table with finding_type=1.
after that the outer query takes the agregated data and sums it up by some columns.
the query works great but the outer query is not using indexes and doing a full-table search.
Is there a way to make the outer query run better and use indexes? this query is going to run on 10,000 rows each run and will run all day, so it is most important that it will be as fast as possible

MySQL sum case value equals max value of that group

I'm trying to solve this query to return the total count of items in a group and the total of items in that group which value equals the max value of one field of that group and also which is the max(value).
So far:
SELECT
limid, COUNT(*), SUM(CASE WHEN cotavertical=MAX(cotavertical) THEN 1 ELSE 0 END), MAX(cotavertical)
FROM limites
LEFT JOIN tbparentchild ON parent=limid
LEFT JOIN tbspatialbi ON child=rgi
WHERE limtipo=4 AND x=1
GROUP BY limid
So far MySQL returns
"Invalid use of group function."
Is it too complex to solve in MySQL only? Better to use algorithm?
You are trying to use the max value for each group in an aggregate function (SUM) before the aggregation has finished, and hence it is not available. The query below uses the strategy of joining a subquery which contains the max value of cotavertical for each limid group. In this case, the max value per group which you want to use will now be available from another source, and you can sum using it.
SELECT l.limid,
COUNT(*),
SUM(CASE WHEN cotavertical = t.cotamax THEN 1 ELSE 0 END),
MAX(cotavertical)
FROM limites l
LEFT JOIN tbparentchild pc
ON pc.parent = l.limid
LEFT JOIN tbspatialbi s
ON pc.child = s.rgi
LEFT JOIN
(
SELECT limid, MAX(cotavertical) AS cotamax
FROM limites
LEFT JOIN tbparentchild
ON parent = limid
LEFT JOIN tbspatialbi
ON child = rgi
WHERE limtipo = 4 AND x = 1
GROUP BY limid
) t
ON l.limid = t.limid
WHERE limtipo = 4 AND l.x = 1
GROUP BY l.limid
Another option for solving your problem would be to use a subquery directly in the CASE statement. But, given the size and number of joins in your original query, this would be way uglier than the query above. MySQL does not support common table expressions, which would have helped with both these solutions.

AGGREGATE Function Group By

I am trying to count my total number of tables, total number of rows, the last time the DB has been updated and last time a stored proc was executed. I have a total of 4 tables and 153 rows after manually counting. When I add the sEPS.last_execution_time to the end of my SELECT, it throws off the numbers. Is there a way I can successfully AGGR everything, then pull back the last execution date?
SELECT
COUNT(SCHEMA_NAME(sO.schema_id)) AS TableCount,
SUM(sPTN.Rows) AS [RowCount],
MAX(sO.modify_date) AS LastUpdated,
(sEPS.last_execution_time) AS 'LastExecuted'
FROM sys.objects AS sO
INNER JOIN sys.partitions AS sPTN ON sO.object_id = sPTN.object_id
INNER JOIN sys.dm_exec_procedure_stats AS sEPS ON sO.object_id = sEPS.object_id
WHERE sO.type = 'U'
GROUP BY sEPS.last_execution_time
When I run the above code, I'm getting 5 rows back(there should only be one) and I am get one table 3 times. Any help is appreciated. THANKS
The last time an sp was executed can't be joined to the rest of the tables, because the other are joined by the object_id of the table. You could do something like this:
SELECT COUNT(DISTINCT SO.object_id) AS TableCount,
SUM(sPTN.Rows) AS [RowCount],
MAX(sO.modify_date) AS LastUpdated,
MAX(LastExecuted) LastExecuted
FROM sys.objects AS sO
INNER JOIN sys.partitions AS sPTN
ON sO.object_id = sPTN.object_id
CROSS JOIN (SELECT MAX(last_execution_time) LastExecuted
FROM sys.dm_exec_procedure_stats) AS sEPS
WHERE sO.type = 'U';