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
Related
The following MySQL query take too much time. its take 24sec . and total records not more the 15000 each table please guide me for faster
Thanks
select c1.code,
( SELECT COALESCE(sum(i.total_amount),0)
FROM invoice as i
WHERE i.customer_code= c1.code
)-
( SELECT COALESCE(sum(p.amount),0)
FROM collection as p
where p.customer_code = c1.code
)-
( SELECT COALESCE(sum(CN.amount),0)
FROM cr_note as CN
where CN.customer_code= c1.code
) as rem_Balance
from customer as c1
you make it fast by replacing sub queries to queries with left joins like this:
WITH allInvoice AS (SELECT customer_code AS code, SUM(total_amount) AS amount FROM invoice GROUP BY customer_code),
allCollection AS (SELECT customer_code AS code, SUM(amount) AS amount FROM collection GROUP BY customer_code),
allNote AS (SELECT customer_code AS code, SUM(amount) AS amount FROM cr_note GROUP BY customer_code)
SELECT customer.code,
(COALESCE(allInvoice.amount) - COALESCE(allCollection.amount) - COALESCE(allNote.amount)) AS rem_Balance
FROM customer
LEFT JOIN allInvoice ON allInvoice.code = customer.code
LEFT JOIN allCollection ON allCollection.code = customer.code
LEFT JOIN allNote ON allNote.code = customer.code
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 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.)
The following query always outputs SUM for all rows instead of per userid. Not sure where else to look. Please help.
SELECT * FROM assignments
LEFT JOIN (
SELECT SUM(timeworked) AS totaltimeworked
FROM time_entries
) assignments ON (userid = assignments.userid AND ticketid = ?)
WHERE ticketid = ?
ORDER BY assigned,scheduled
If you want to keep the SELECT *, you would have to add a group by clause in the subquery. Something like this
SELECT * FROM assignments
LEFT JOIN (
SELECT SUM(timeworked) AS totaltimeworked
FROM time_entries
GROUP BY userid
) time_entriesSummed ON time_entriesSummed.userid = assignments.userid
WHERE ticketid = ?
ORDER BY assigned,scheduled
But a better way would be to change the SELECT * to instead select the fields you want a add a group by clause directly. Something like this
SELECT
assignments.id,
assignments.assigned,
assignments.scheduled,
SUM(time_entries.timeworked) AS totalTimeworked
FROM assignments
LEFT JOIN time_entries
ON time_entries.userid = assignments.userid
GROUP BY assignments.id, assignments.assigned, assignments.scheduled
Edit 1
Included table names in query 2 as mentioned in chameera's comment below
This is the query I need in English:
Display the animal id, name, and number of exams of the animal(s) with the most examinations done on them.
Consider there might be ties for first place. In that case all tied animals should be returned.
Here's some relevant SQL:
select an_id, an_name, count(distinct ex_id) as NumberExams
from vt_animals
join vt_exam_headers using (an_id)
How can I do this without using desc and limit and ideally with group by? I thought of using max, but it doesn't seem to work with count.
If I understand well the query, something like this would return the group of animals if more than one have the most number of examinations:
SELECT a.an_id, a.an_name, a.number_exams
FROM (SELECT an_id, an_name, COUNT(ex_id) as number_exams
FROM vt_animals
JOIN vt_exam_headers USING (an_id)
GROUP BY an_id) AS a
HAVING a.number_exams >= MAX(a.number_exams)
You have to use group by clause to the column names which are not in the aggregate functions
select an_id, an_name, count(distinct ex_id) as NumberExams
from vt_animals
group by an_id, an_name
First select animal with most examinations:
SELECT an_id,count(ex_id) FROM animals GROUP BY an_id ORDER BY count(*) DESC LIMIT 1
Then you can use it as a subquery.
Explanation: you sort this table descending by count(*) and then you choose top 1, which is maximum.
Depending on the database product you're using, this could vary in complexity. For example, emibloque's answer will not work in MS SQL Server because the having clause needs to correspond with the group by clause. In this case, you'd have to do something along these lines:
select * from
(
select an_name, count(*) exams
from vt_animals a join vt_exam_headers e on a.an_id = e.an_id
group by an_name
) sub1
where exams =
(
select max(exams) from
(
select an_id, count(*) exams
from vt_exam_headers
group by an_id
) sub2
)
or if you prefer the use of variables:
declare #max_exams int;
select #max_exams = (
select max(exams) from
(
select an_id, count(*) exams
from vt_exam_headers
group by an_id
) sub
);
select * from
(
select an_name, count(*) exams
from vt_animals a join vt_exam_headers e on a.an_id = e.an_id
group by an_name
) sub1
where exams = #max_exams