I've the following query that does a group by on 2 columns from different tables. I understand this can cause performance issues. I'm unsure how to go about optimizing this. Any help is greatly appreciated!
Query
SELECT tableA.col AS tableAcol,
tableB.col AS tableBcol,
SUM(tableB.count) AS total
FROM tableB, tableA
WHERE tableB.uid = tableA.uid
AND tableA.eid=?
GROUP BY tableA.col, tableB.col
You can use a subquery that performs partial sums, to reduce the size of the JOIN that sum in the outer query.
SELECT tableA.col AS tableAcol
tableBgrouped.col AS tableBcol,
SUM(tableBgrouped.count) AS total
FROM tableA
JOIN (SELECT uid, col, SUM(count) AS count
FROM tableB
GROUP BY uid, col) AS tableBgrouped
ON tableA.uid = tableBgrouped.uid
WHERE tableA.eid = ?
GROUP BY tableAcol, tableBcol
SELECT A.`col` AS tableAcol
B.`col` AS tableBcol,
SUM(B.`count`) AS total
FROM tableA A
JOIN (SELECT B1.`uid`, B1.`col`, SUM(B1.`count`) AS count
FROM tableB B
GROUP BY B1.`uid`, B1.`col`) AS B
ON A.`uid` = B.`uid`
WHERE A.`eid` = ?
GROUP BY A.`col`, B.`col`
Related
I have 3 tables which are interconnected and i want to select columns from two tables and counts from table 3. If anyone is aware on this, any hint would be appreciated.
Below is the sql i tried, but the count is getting repeated
SELECT distinct p.p_id, p.p_f6, p.p_l4,m.m_id, (
SELECT COUNT(*)
FROM ttokens t where t.pdetail_id = p.pdetail_id
) AS token_count
FROM tparking p,ttokens t LEFT join ttokens_md m ON t.trefn_id = m.trefn_id
WHERE t.pdetail_id = p.pdetail_id
You can try to use JOIN with subquery to get your count instead of selcet subquery.
SELECT p.p_id, p.p_f6, p.p_l4,m.m_id,t.cnt
FROM tparking p
JOIN (
SELECT pdetail_id,COUNT(*) cnt
FROM ttokens
GROUP BY pdetail_id
) t ON t.pdetail_id = p.pdetail_id
LEFT join ttokens_md m ON t.trefn_id = m.trefn_id
Note
I would use JOIN instead of , comma with where condition to connect two tables,, is an old style.
I'm trying to do the following query:
SELECT
(
SELECT
COUNT(*)
FROM
pl_invoices
JOIN
pl_invoice_articles
ON
pl_invoices.invoice_id = pl_invoice_articles.invoice_article_invoice_id
WHERE
pl_invoices.invoice_amount_paid = SUM(pl_invoice_articles.invoice_article_price)
) as 'Aantal Betaald'
So: I need to know how many invoices are paid, but the only way to know that is to make the sum of all the invoice articles and compare that to the total amount paid.
This query does not work and I don't know what i'm doing wrong. Is there anyone who can help me?
Many thanks in advance!
Use having clause instead of where
SELECT cnt as 'Aantal Betaald' from (
SELECT COUNT(*) AS CNT, invoice_amount_paid FROM pl_invoices pl JOIN pl_invoice_articles pla
ON pl.invoice_id = pla.invoice_article_invoice_id
having pl.invoice_amount_paid = SUM(pla.invoice_article_price)
) tt ;
One solution is to this query to calculate the sum:
select
invoice_article_invoice_id,
SUM(invoice_article_price) as tot
from
pl_invoice_articles
group by
invoice_article_invoice_id
and join it as a subquery to the pl_invoices table:
select count(*)
from
pl_invoices inner join (
select
invoice_article_invoice_id,
SUM(invoice_article_price) as tot
from
pl_invoice_articles
group by
invoice_article_invoice_id
) s on pl_invoices.invoice_id = s.invoice_article_invoice_id
where
pl_invoices.invoice_amount_paid = s.tot
I have a table called
tb_applicants with fields id, aic, name
app_interview with fields id, atic, atname
My problem is i want to count all (atic) from app_interview table where atic is equal to aic from table (tb_applicants) group by 1(aic) from tb_applicants
In my current query its not working can anyone help me find where is the problem it gives me 0 count all the time.
query:
SELECT count(t.atic)
FROM app_interview as t
INNER JOIN tb_applicants as t2
WHERE t.atic = t2.aic
GROUP BY t2.aic;
Remove the ; and use ON for JOINS:
SELECT count(*) FROM app_interview INNER JOIN tb_applicants ON tb_applicants.aic = app_interview.atic GROUP BY tb_applicants.aic;
Could probably done simpler, since you need only matching rows:
SELECT count(t.atic)
FROM app_interview as t, tb_applicants as t2
WHERE t.atic = t2.aic
GROUP BY t.atic;
I have the following 2 queries.
Query 1 :
select distinct(thread_id) from records where client_name='MyClient'
Query 2 :
select max(thread_no) from records
where thread_id='loop_result_from_above_query' AND action='Reviewed'
Is it possible to combine them into a single query ?
The second query is run on every result of the first query.
Thank you.
See attached image of a small snippet of mysql records.
I need a single mysql query to output only records which have action="MyAction" as the latest records for a given set of thread_ids. In the sample data set : record with Sr: 7201
I hope this helps in helping me :)
SELECT client_name, thread_id, MAX(thread_no) max_thread
FROM records
WHERE action='Reviewed' AND client_name='MyClient'
GROUP BY client_name, thread_id
UPDATE 1
SELECT a.*
FROM records a
INNER JOIN
(
SELECT thread_id, max(sr) max_sr
FROM records
GROUP BY thread_id
) b ON a.thread_id = b.thread_id AND
a.sr = b.max_sr
WHERE a.action = 'MyAction'
You can use SELF JOIN, but it is not advisable and will impact your query performance. Please check below query for your reference
SELECT DISTINCT r1.thread_id, MAX(r2.thread_no) from records r1 LEFT JOIN records r2 ON r2.thread_id=r1.thread_id WHERE r1.client_name='MyClient' AND r2.action='Reviewed'
SELECT a.maxthreadid,
b.maxthreadno
FROM (SELECT DISTINCT( thread_id ) AS MaxThreadId
FROM records
WHERE client_name = 'MyClient') a
CROSS JOIN (SELECT Max(thread_no) AS MaxThreadNo
FROM records
WHERE thread_id = 'loop_result_from_above_query'
AND action = 'Reviewed') b
Try this.
SELECT *
FROM (SELECT Row_number()
OVER (
partition BY thread_id
ORDER BY thread_no) no,
Max(thread_no)
OVER(
partition BY thread_id ) Maxthread_no,
thread_id,
action,
client_name
FROM records
Where client_name = 'MyClient') AS T1
WHERE no = 1
AND action = 'Reviewed'
SELECT
count(A.gross_c) AS cnt,
SUM(A.gross_c) AS Gross_c,
B.store_name
FROM hr_location_c_verified A
JOIN hr_stores B ON A.c_location = B.id
WHERE A.c_ref_fkid IS NULL
GROUP BY A.c_location
I am executing this query, the output result coming correct, but count showing wrong.
Please help me.
It's likely because of your JOIN, producing more rows.
One way is to use the DISTINCT keyword.
SELECT
count(DISTINCT A.gross_c) AS cnt,
SUM(A.gross_c) AS Gross_c,
B.store_name FROM hr_location_c_verified A
JOIN hr_stores B ON A.c_location = B.id
WHERE A.c_ref_fkid IS NULL
GROUP BY A.c_location