I have two tables with the following fields
emp_table: emp_id, emp_name
salary_increase: emp_id, inc_date, inc_amount
I am required to write a query which gives the employee details, the number of times an employee has received a salary increase, the value of the maximum increase amount and the date of that increase. Here is what i have so far:
SELECT e.*, count(i.inc_amount), max(i.inc_amount)
FROM salary_increase AS i
RIGHT JOIN emp_table AS e
ON i.emp_id=e.emp_id
GROUP BY e.emp_id;
this correctly gives all the requirements apart from the date on which the maximum increase was awarded. I have tried the following with no success:
SELECT e.*, count(i.inc_amount), max(inc_amount), t.inc_date
FROM salary_increase AS i
RIGHT JOIN emp_table AS e
ON i.emp_id=e.emp_id
RIGHT JOIN
(
SELECT emp_id, inc_date FROM salary_increase
WHERE inc_amount=max(inc_amount) GROUP BY emp_id
) AS t
ON e.emp_id=t.emp_id
GROUP BY e.emp_id;
this gives an error 'Invalid use of group function'. Does anyone know what i'm doing wrong?
You can't do this WHERE inc_amount=max(inc_amount) in the where clause, either use HAVING or do it in the condition of join, try this instead:
SELECT
e.emp_id,
e.inc_date,
t.TotalInc,
t.MaxIncAmount
FROM salary_increase AS i
INNER JOIN emp_table AS e ON i.emp_id=e.emp_id
INNER JOIN
(
SELECT
emp_id,
MAX(inc_amount) AS MaxIncAmount,
COUNT(i.inc_amount) AS TotalInc
FROM salary_increase
GROUP BY emp_id
) AS t ON e.emp_id = t.emp_id AND e.inc_amount = t.MaxIncAmount;
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 the followin query:
SELECT contracts.id,
(SELECT sum(pos.sum_to_pay) FROM pos
where pos.contract_id=contracts.id and pos.is_draft=0) as paid,
(SELECT sum(acts.amount) FROM acts
where acts.contract_id=contracts.id) as acts_sum
from contracts
it works but i want to add another result field to_pay that should be calculated like acts_sum - paid = to_pay.
I'm trying to do it like this:
SELECT contracts.id,
(SELECT sum(pos.sum_to_pay) FROM pos
where pos.contract_id=contracts.id and pos.is_draft=0) as paid,
(SELECT sum(acts.amount) FROM acts
where acts.contract_id=contracts.id) as acts_sum,
(acts_sum - paid) as to_pay
from contracts
but I got the error Unknown column 'acts_sum'. How can i find to_pay value based on acts_sum and paid?
Do it with a subquery like this
SELECT acts_sum, paid, (acts_sum - paid) as to_pay FROM
(SELECT contracts.id,
(SELECT sum(pos.sum_to_pay) FROM pos
where pos.contract_id=contracts.id and pos.is_draft=0) as paid,
(SELECT sum(acts.amount) FROM acts
where acts.contract_id=contracts.id) as acts_sum,
from contracts ) subq
You could rewrite your query using joins, correlated sub queries sometimes considered as a costly solution
select c.id,
COALESCE(a.acts_sum,0),
COALESCE(p.paid,0),
COALESCE(a.acts_sum,0) - COALESCE(p.paid,0) as to_pay
from contracts c
left join (
SELECT contract_id,sum(sum_to_pay) paid
FROM pos
where is_draft=0
group by contract_id
) p on c.id = p.contract_id
left join (
SELECT contract_id,sum(amount) acts_sum
FROM acts
group by contract_id
) a on c.id = a.contract_id
This is two query that i need to combine:
first query:
SELECT emp.name, count(rej.employee_ic) as 'rejected leave'
FROM employee as emp
LEFT OUTER JOIN rejectedleave as rej
ON emp.ic_no = rej.employee_ic
GROUP BY emp.`ic_no`
Second query:
SELECT emp.name, count(app.employee_ic) as 'approved leave'
FROM employee as emp
LEFT OUTER JOIN approvedleave as app
ON emp.ic_no = app.employee_ic
GROUP BY emp.`ic_no`
The output:
first query:
first query output image
second query:
second query output image
i want to combine this two table into one table. please help me to solve this problem, appreciate your help.
Is this you need? It find the count of approved and rejected leaves for each available employee in separate subqueries and then joins it with the employee table to show the count of rejected and approved leaves for each employee in one row.
Use coalesce(col,0) to return 0 if the count if null.
Coalesce returns first non null value from the given list of arguments.
select
t1.*,
coalesce(t2.approvedleave,0) approvedleave
coalesce(t3.rejectedleave,0) rejectedleave
from employee t1
left join (
select employee_ic, count(*) approvedleave
from approvedleave
group by employee_ic
) t2 on t1.ic_no = t2.employee_ic
left join (
select employee_ic, count(*) rejectedleave
from rejectedleave
group by employee_ic
) t3 on t1.ic_no = t3.employee_ic;
Read about coalesce here:
http://www.w3resource.com/mysql/comparision-functions-and-operators/coalesce-function.php
I suspect you want this:
SELECT e.name, COALESCE(r.rejected_leave, 0) as rejected_leave,
COALESCE(a.approved_leave, 0) as approved_leave
FROM employee e LEFT OUTER JOIN
(SELECT rl.employee_ic, COUNT(*) as rejected_leave
FROM rejectedleave rl
GROUP BY rl.employee_ic
) r
ON e.ic_no = r.employee_ic LEFT OUTER JOIN
(SELECT al.employee_ic, COUNT(*) as approved_leave
FROM approvedleave al
GROUP BY al.employee_ic
) a
ON e.ic_no = a.employee_ic;
That is, do the aggregation before doing the joins. This assumes that e.ic_no and e.name are both unique in the employees table.
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
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