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
Related
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();
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 have two tables: 1 Table wccrm_orders where all orders are stored, and a table wccrm_kunden where all the user data is stored. In wccrm_orders, I have a date-field "ordered_date" and in wccrm_kunden a date-field "Anprobe"
When I want to select the wccrm_orders I use that code:
Select Year(wccrm_orders.ordered_date) as Jahr,
Month(wccrm_orders.ordered_date) as Monat, round(sum(wccrm_orders.preis)) as Summe,
count(*) as Anzahl from wccrm_orders
GROUP BY YEAR(wccrm_orders.ordered_date), MONTH(wccrm_orders.ordered_date)
When I want to count(*) all the appointments from wccrm_kunden.anprobe I use this code:
Select Year(wccrm_kunden.anprobe) as Jahr, Month(wccrm_kunden.anprobe) as
Monat, count(*) as Anzahl from wccrm_kunden where wccrm_kunden.status = 1
GROUP BY YEAR(wccrm_kunden.anprobe), MONTH(wccrm_kunden.anprobe)
How can I combine these codes? How can I achieve it, to get the numbers of trials (appointments) into the first SELECT?
Thank you very much for your help!
BR,
Stefan
A couple of sub-queries may work:
SELECT A.Jahr, A.Monat, A.Summe, A.Anzahl, B.AnzahlB
FROM
(SELECT Year(wccrm_orders.ordered_date) as Jahr,
Month(wccrm_orders.ordered_date) as Monat,
round(sum(wccrm_orders.preis)) as Summe,
count(*) as Anzahl
FROM wccrm_orders
GROUP BY
YEAR(wccrm_orders.ordered_date),
MONTH(wccrm_orders.ordered_date)) AS A
LEFT OUTER JOIN
(SELECT Year(wccrm_kunden.anprobe) as Jahr,
Month(wccrm_kunden.anprobe) as Monat,
count(*) as AnzahlB
FROM wccrm_kunden
WHERE wccrm_kunden.status = 1
GROUP BY
YEAR(wccrm_kunden.anprobe),
MONTH(wccrm_kunden.anprobe)) AS B
ON A.Jahr = B.Jahr AND A.Monat = B.Monat
(Sorry, don't have the schema for this DB, so there may be a syntax error (or three!) in this code, but hopefully you get the idea.)
This is just a test project. I want to know how to select All professors with more than 5 failed students in a subject
I already know how to select All professors with at least 2 subjects with the following query:
SELECT paulin_professors.*,
IFNULL(sub_p.total, 0) num
FROM paulin_professors
LEFT JOIN ( SELECT COUNT(*) total, pau_profid
FROM paulin_profsubject
GROUP BY pau_profid
) sub_p ON (sub_p.pau_profid = paulin_professors.pau_profid)
WHERE sub_p.total >= 2;
I know I'm close but I can't get it to work (All professors with more than 5 failed students in a subject) . Any ideas? TIA
try using SELECT with UNION
select [columnName1],[columnName2] from [Table1] where [condition] union select [columnName1],[columnName2] from [Table1] where [condition] union ....
Looks like can get the professor IDs from the profsubject table and JOIN the studentenrolled table using the subjid for the join. In a similar way to what you had, you can get the count of students who have a grade less than a certain pass/fail threshold (in this case 65).
Then to get a short list, you can select the distinct profids from this derivaed table.
SELECT
distinct pau_profid
FROM
(SELECT
t1.pau_profid,
IFNULL(t2.total_failed, 0) number_failed >= 5
FROM
paulin_profsubject t1
LEFT JOIN
(SELECT
COUNT(*) total_failed,
pau_subjid
FROM
paulin_studentenrolled
WHERE
pau_grade < 65
GROUP BY
pau_subjid
) t2
ON
t1.pau_subjid = t2.pau_subjid
WHERE
number_failed >= 5
) t3;
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