i have two table galang_dana and donasi, and then i'm try to count all data inside the galang_dana table, and donasi table. The data in galang_dana table is there 6 data, when i'm try this query it's show the data correctly there 6 data
select count(galang_dana.id_galang_dana) as jumlah_galang_dana
from galang_dana
but when i'm try to inner join that both table using this query to count and sum the other data, the data of query select count(galang_dana.id_galang_dana) as jumlah_galang_dana its showing 8 data.
select count(galang_dana.id_galang_dana) as jumlah_galang_dana,
count( distinct donasi.id_user) as jumlah_donatur,
SUM(donasi.jumlah_dana) as total_dana_terkumpul
from `galang_dana` left join
`donasi`
on `galang_dana`.`id_galang_dana` = `donasi`.`id_galang_dana`
I think you want to aggregate before joining:
select gd.jumlah_galang_dana,
d.jumlah_donatur, d.total_dana_terkumpul
from (select count(*) as jumlah_galang_dana
from `galang_dana` gd
) gd cross join
(select count(distinct d.id_user) as jumlah_donatur,
sum(d.jumlah_dana) as total_dana_terkumpul
from donasi d
) d;
DB::table('galang_dana')
->selectRaw('
COUNT(*) jumlah_galang_dana,
(SELECT COUNT(*) FROM donasi WHERE donasi.id_galang_dana = galang_dana.id_galang_dana) jumlah_donatur,
(SELECT SUM(`jumlah_dana`) FROM donasi WHERE donasi.id_galang_dana = galang_dana.id_galang_dana) total_dana_terkumpul
')
->groupBy('id')
->get();
Related
I have this table below as a result
SELECT
doctors.`name`,
COUNT(`doctor-barge-naghs`.`code-naghs`) AS 'countEachDoctor'
FROM
doctors
INNER JOIN `doctor-barge-naghs` ON `doctor-barge-naghs`.`code-doctor` = doctors.id
GROUP BY doctors.`name`
and I want to calculate the SUM 'countEachDoctor' field and show
it beside of each row.
I did this
SELECT t1.*,(SELECT SUM(t1.countEachDoctor))
FROM(
SELECT
doctors.`name`,
COUNT(`doctor-barge-naghs`.`code-naghs`) AS 'countEachDoctor'
FROM
doctors
INNER JOIN `doctor-barge-naghs` ON `doctor-barge-naghs`.`code-doctor` = doctors.id
GROUP BY doctors.`name`) AS t1
it is what I wanted but unfortunately ,it just show one records,I need all records.
IF you are not using mysql 8, you can achieve it like this:
SELECT
doctors.`name`,
COUNT(`doctor-barge-naghs`.`code-naghs`) AS 'countEachDoctor',
(SELECT SUM(t.countEachDoctor)
FROM (
SELECT
COUNT(`doctor-barge-naghs`.`code-naghs`) AS 'countEachDoctor'
FROM doctors
INNER JOIN `doctor-barge-naghs` ON `doctor-barge-naghs`.`code-doctor` = doctors.id
GROUP BY doctors.`name`) t) AS sumCount
FROM
doctors
INNER JOIN `doctor-barge-naghs` ON `doctor-barge-naghs`.`code-doctor` = doctors.id
GROUP BY doctors.`name`
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 want this statement to return the systems where there are more than two stations, I got this far but I don't know what to do next. All this does it return every system with a regionID less than 1100001. Am I onto the right idea at least?
SELECT DISTINCT mapSolarSystems.regionID,solarSystemID,solarSystemName,x,z,security
FROM mapSolarSystems
WHERE mapSolarSystems.regionID <11000001
AND
2 < (SELECT COUNT(*) FROM stations,mapSolarSystems
WHERE mapSolarSystems.solarSystemID=stations.systemid)
See if this work:
SELECT DISTINCT mapSolarSystems.regionID,solarSystemID,solarSystemName,x,z,security
FROM mapSolarSystems m
join stations s on m.solarSystemID = s.systemid
WHERE mapSolarSystems.regionID <11000001
AND select COUNT(*) FROM stations >2
OR like that:
SELECT DISTINCT
mapSolarSystems.regionID,solarSystemID,solarSystemName,x,z,security
FROM mapSolarSystems m
join (select sistemId ,count(systemid) from station group by sistemId having
count(sistemId) > 2) X on m.solarSystemID = X.systemid
WHERE mapSolarSystems.regionID <11000001
SELECT DISTINCT m.regionID,solarSystemID,solarSystemName,x,z,security FROM mapSolarSystems m join stations s on m.solarSystemID = s.systemid WHERE m.regionID <11000001 AND (SELECT COUNT(*) FROM stations) >2
I have a table, named rendeles_termekek.(In english, ordered_products)
I would like to count, that each product how many times was ordered.
With the SQL below, I get 4 as ennyi. I upload a picture, and I wrote the correct number to each rows.
SELECT DISTINCT
rendeles_termekek.termek_id,
termek.termek_id,
termek.termek_nev,
( SELECT COUNT(rendeles_termekek.termek_id) FROM rendeles_termekek ) AS ennyi
FROM rendeles_termekek
LEFT JOIN termek ON rendeles_termekek.termek_id = termek.termek_id
ORDER BY termek.termek_nev ASC
**r1** is the alias of your table rendeles_termekek so you have to access those columns through alias name "r1" like I did in below query. try below
SELECT DISTINCT
r1.termek_id,
termek.termek_id,
termek.termek_nev,
( SELECT COUNT(r.termek_id) FROM rendeles_termekek r where r.termek_id = r1.termek_id ) AS ennyi
FROM rendeles_termekek r1
LEFT JOIN termek ON r1.termek_id = termek.termek_id
ORDER BY termek.termek_nev ASC
I think you have only four rows in your table "rendeles_termekek".
I have updated your query. try this
SELECT DISTINCT
rendeles_termekek.termek_id,
termek.termek_id,
termek.termek_nev,
( SELECT COUNT(rendeles_termekek.termek_id) FROM rendeles_termekek r where r.termek_id = r1.termek_id ) AS ennyi
FROM rendeles_termekek r1
LEFT JOIN termek ON rendeles_termekek.termek_id = termek.termek_id
ORDER BY termek.termek_nev ASC
Note that I have changed the sub-query and added a where condition in it
`( SELECT COUNT(rendeles_termekek.termek_id) FROM rendeles_termekek r where r.termek_id = r1.termek_id ) AS ennyi`
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