How to Join the table in phpmyadmin - mysql

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.

Related

how can show count for each row in mysql?

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`

Count non null values from a left joint table

Count non-null values directly from select statement (not using where) on a left joint table
count(*) as comments Need this to provide count of non-null values only. Also, inner join is not a solution because, that does not include content which have zero comments in count(distinct (t1.postId)) as no_of_content
select t1.tagId as tagId, count(distinct (t1.postId)) as no_of_content, count(*) as comments
from content_created as t1
left join comment_created as t2
on t1.postId=t2.postId
where
( (t1.tagId = "S2036623" )
or (t1.tagId = "S97422" )
)
group BY 1
Though Posting the sample data might help us more to answer this but you can update your count function to -
COUNT(CASE WHEN postId IS NULL THEN 1 END) as comments
Count only counts non-null values. What you need to do is reference the right hand side table's column explicitly. So instead of saying count(*) use count(right_joined_table.join_key).
Here's a full example using BigQuery:
with left_table as (
select num
from unnest(generate_array(1,10)) as num
), right_table as (
select num
from unnest(generate_array(2,10,2)) as num
)
select
count(*) as total_rows,
count(l.num) as left_table_counts,
count(r.num) as non_null_counts
from left_table as l
left outer join right_table as r
on l.num = r.num
This gives you the following results:

Join two MySQL SELECT statements containing one WHERE clause

How can I join the two select statements below to work as one statement? I would like the result to appear in one table. Thanks for your help.
First statement -
SELECT Account_ID, SUM(Profit_Loss) AS Starting_Balance
FROM client_ledger_history
WHERE Summary = 'Cash In'
GROUP BY Account_ID WITH ROLLUP
Second statement -
SELECT
client_ig_client_list.Account_ID,
client_ig_client_list.`Name`,
Share_Status,
Forex_Status,
Index_Status,
Share_Weighting,
Forex_Weighting,
Index_Weighting,
SUM(
client_ledger_history.Profit_Loss
) AS Current_Balance
FROM
client_ledger_history
LEFT JOIN client_ig_client_list ON client_ig_client_list.Account_ID = client_ledger_history.Account_ID
GROUP BY
Account_ID WITH ROLLUP
You should make a join into a nested table
SELECT
client_ig_client_list.Account_ID,
Starting_Balance,
client_ig_client_list.`Name`,
Share_Status,
Forex_Status,
Index_Status,
Share_Weighting,
Forex_Weighting,
Index_Weighting,
SUM(client_ledger_history.Profit_Loss) AS Current_Balance
FROM
client_ledger_history LEFT JOIN client_ig_client_list ON client_ig_client_list.Account_ID = client_ledger_history.Account_ID
LEFT JOIN
(SELECT Account_ID, SUM(Profit_Loss) AS Starting_Balance
FROM client_ledger_history WHERE Summary = 'Cash In' GROUP BY Account_ID WITH ROLLUP) as client_ledger_aggreagated_history
ON client_ledger_aggreagated_history.Account_ID = client_ledger_history.Account_ID
GROUP BY Account_ID WITH ROLLUP

SQL select query using joins, group by and aggregate functions

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;

Mysql Group BY with if statement

I'm not sure if this is something you can do in a single select statement without nesting selects.
I am grouping my results and I want to know IF a field inside the entire grouping contains a condition, display yes. With this it will just take the first row from the group and check the condition instead of all the rows in the group
if(field = 'condition','yes','no') as field_found
example table: id, score
SELECT t1.id, (
SELECT IF("10" IN (
SELECT score FROM table t2 WHERE t1.id = t2.id
),'yes','no'))
FROM table t1
GROUP BY t1.id
does that work?
Since you are already doing a group by, you should be able to add a MAX() as a column having the condition you are expecting and just add that to the group... such as
select
MAX( if( field LIKE '%condition%','1', '2' )) as ExtraOrderBy,
First_Name,
Last_Name,
... rest of query ...
group by
customers.Customer_ID
order by
1
In this case, the order by is the ordinal column in the SQL list instead of explicit retyping the MAX( IF() ) condition... So, if the condition is true, mark it with "1", otherwise "2" will float all those that qualify to the top of the list... Then, you could sub-order by other things like last name, first name, or other fields you have queried.
if(GROUP_CONCAT(field) LIKE '%condition%','yes','no')
SELECT first_name,last_name, CONCAT(physical_address," ",physical_address2," ",city, " ",zip) as address, MONTHNAME(install_date) as billing_month, IFNULL(status.quantity,0) as total_measures_instalclient2d,IFNULL(client1_measures,"") as client1_measures,IFNULL(client2_measures,"") as client2_measures,IFNULL(client1_quantity,0) as client1_quantity,IFNULL(client2_quantity,0) as client2_quantity,if(GROUP_CONCAT(measure_list.measure_type) LIKE '%Outreach/ Assessment%','yes','no') as outreach_invoiced,GROUP_CONCAT(IF(client='Client1',CONCAT(percent*100,"%-",measure_list.measure_type),NULL)) as client1_percent,GROUP_CONCAT(IF(client='Client2',CONCAT(percent*100,"%-",measure_list.measure_type),NULL)) as client2_percent,work_order.notes FROM customers
INNER JOIN measure on measure.customer_id = customers.customer_id
INNER JOIN measure_list on measure_list.measure_list_id = measure.measure_list_id
INNER JOIN work_order on work_order.work_order_id = measure.work_order_id
INNER JOIN billing on billing.workmanship = work_order.workmanship AND billing.measure_type = measure_list.measure_type
LEFT JOIN (
SELECT customers.customer_id,SUM(quantity) as quantity,GROUP_CONCAT(IF(client='Client1',measure_description,NULL)) as client1_measures,GROUP_CONCAT(IF(client='client2',measure_description,NULL)) as client2_measures,SUM(IF(client='client1',quantity,0)) as client1_quantity,SUM(IF(client='client2',quantity,0)) as client2_quantity FROM customers
INNER JOIN measure on measure.customer_id = customers.customer_id
INNER JOIN measure_list on measure_list.measure_list_id = measure.measure_list_id
INNER JOIN work_order on work_order.work_order_id = measure.work_order_id
INNER JOIN billing on billing.workmanship = work_order.workmanship AND billing.measure_type = measure_list.measure_type
WHERE measure_list.measure_type NOT IN ('measure1','measure2')
GROUP BY customers.customer_id
) as status on status.customer_id = customers.customer_id
WHERE measure_list.measure_type IN ('measure1','measure2')
GROUP BY customers.customer_id