mysql count on multiple columns from another table - mysql

I would like to get count of two columns from another table. For example, the source data is as below for the table called employee_master
emp_code dept_id sub_dept_id
1 10 22
2 11 20
3 10 22
4 10 22
5 11 20
6 10 21
Another source table called punching_data_table as shown below which is being joined with employee_master.
emp_code present
1 10:01 AM
2 10:02 AM
3 10:02 AM
4 10:03 AM
5 10:11 AM
6 10:09 AM //biometric punching time shown
The result is suppose to look like below.
dept_id dept_count sub_dept_id sub_dept_count
10 4 22 3
10 4 21 1
11 2 20 2
Tried with following code but it doesn't count seperately for both columns even though applied group by employee_master.dept_id, employee_master.sub_dept_id. Please note that there is only 1 column common between both table that is emp_code.
SELECT p.emp_code
, e.emp_code
, COUNT(e.dept_id) dept_id_count
, COUNT(e.sub_dept_id) sub_dept_id_count
FROM punching_data_table p
LEFT
JOIN employee_master e
ON e.emp_code = p.emp_code
GROUP
BY e.dept_id
, e.sub_dept_id
Any help on the subject with explanation will be highly appreciated.

This query (SQLFiddle) will give you the output that you specify:
SELECT em1.dept_id, em2.dept_count, em1.sub_dept_id, COUNT(em1.sub_dept_id) AS sub_dept_count
FROM employee_master em1
JOIN (SELECT dept_id, COUNT(dept_id) as dept_count
FROM employee_master e
JOIN punching_data_table p
ON e.emp_code = p.emp_code
GROUP BY dept_id) em2
ON em1.dept_id = em2.dept_id
WHERE EXISTS (SELECT * FROM punching_data_table pdt WHERE pdt.emp_code = em1.emp_code)
GROUP BY dept_id, sub_dept_id
Output:
dept_id dept_count sub_dept_id sub_dept_count
10 4 21 1
10 4 22 3
11 2 20 2
If you have a department names table e.g.
CREATE TABLE dept_master (dept_id INT, dept_name VARCHAR(20));
INSERT INTO dept_master VALUES
(10, 'Department 10'),
(11, 'Department 11');
You can join that as well using this query (SQLFiddle):
SELECT d.dept_name, em2.dept_count, em1.sub_dept_id, COUNT(em1.sub_dept_id) AS sub_dept_count
FROM employee_master em1
JOIN (SELECT dept_id, COUNT(dept_id) as dept_count
FROM employee_master e
JOIN punching_data_table p
ON e.emp_code = p.emp_code
GROUP BY dept_id) em2
ON em1.dept_id = em2.dept_id
JOIN dept_master d on d.dept_id = em1.dept_id
WHERE EXISTS (SELECT * FROM punching_data_table pdt WHERE pdt.emp_code = em1.emp_code)
GROUP BY d.dept_id, sub_dept_id
And then get department names in the output instead of ids:
dept_name dept_count sub_dept_id sub_dept_count
Department 10 4 21 1
Department 10 4 22 3
Department 11 2 20 2

Try this query
SELECT emp_code, MAX(dept_id_count) as dept_id_count, MAX(sub_dept_id_count) as sub_dept_id_count
FROM
(SELECT p.emp_code , COUNT(e.dept_id) dept_id_count, 0 as sub_dept_id_count
FROM punching_data_table p
LEFT JOIN employee_master e ON e.emp_code = p.emp_code
GROUP BY e.dept_id
UNION
SELECT p.emp_code, 0 as dept_id_count, COUNT(e.sub_dept_id) sub_dept_id_count
FROM punching_data_table p
LEFT JOIN employee_master e ON e.emp_code = p.emp_code
GROUP BY e.sub_dept_id) as subQuery
GROUP BY emp_code

There are some things which are not clear in your question for example do you want distinct employees or just a occurrences of employees, what happens if an employee does not exist in punching_data. I have made some assumptions in this answer but the crux of it is that you create 2 sub queries and join them on dept id which will force a 1 to many join
so given
drop table if exists employee_master,punching_data_table;
create table employee_master
(emp_code int, dept_id int, sub_dept_id int);
insert into employee_master values
(1 , 10 , 22),
(2 , 11 , 20),
(3 , 10 , 22),
(4 , 10 , 22),
(5 , 11 , 20),
(6 , 10 , 21 ),
(7 , 10 , 21);
create table punching_data_table(emp_code int);
insert into punching_data_table values
(1),
(2),
(3),
(4) ,
(5),
(6);
select a.dept_id,a.empcodes,s.sub_dept_id,s.empcodes from
(
select dept_id ,count(distinct emp_code) empcodes
from employee_master
where emp_code in (select emp_code from punching_data_table)
group by dept_id
) a
join
(select dept_id , sub_dept_id, count(distinct emp_code) empcodes
from employee_master
where emp_code in (select emp_code from punching_data_table)
group by dept_id,sub_dept_id) s
on s.dept_id = a.dept_id
order by a.dept_id,s.sub_dept_id
Result
+---------+----------+-------------+----------+
| dept_id | empcodes | sub_dept_id | empcodes |
+---------+----------+-------------+----------+
| 10 | 4 | 21 | 1 |
| 10 | 4 | 22 | 3 |
| 11 | 2 | 20 | 2 |
+---------+----------+-------------+----------+
3 rows in set (0.00 sec)

This result can be achieved without punching_data_table
SELECT t1.dept_id,
t2.sub_dept_id,
t1.count_dept,
t2.count_sub
FROM (SELECT dept_id,
sub_dept_id,
Count(dept_id) AS count_dept
FROM employee_master
GROUP BY dept_id)t1
LEFT JOIN (SELECT sub_dept_id,
dept_id,
Count(sub_dept_id) AS count_sub
FROM employee_master
GROUP BY sub_dept_id)t2
ON t1.dept_id = t2.dept_id

Related

how to choose value in 1 column that must have 2 values in another - sql?

table:
id lesson
11 11 A
8 11 B
4 11 A
4 11 A
2 11 A
6 11 A
5 11 A
13 11 A
11 11 B
the id 11 has both taught in classroom 11A, 11B. How to select the ids that have both values 11a,11b?
I tried this with no luck:
select id from table where lesson in '11A' and lesson in '11B'
because it gives empty table, because it can't be both 11a and b at the same time.
If it's exectly two values you can make an inner join
select a.id
from myTable a
inner join myTable b
on a.id = b.id
and a.lesson = '11 A' and b.lesson = '11 B'
for obtain the id with both the lesson
if could try using a subquery for the involved id and the count if the id have more then a result
select id
FROM (
select id
from my_table
where lesson in ('11A', '11b')
) t
group by id
having count(*) = 2

Get table row count using multiple table join

I have Three tables company_m , client_m , companyuser_m
company_m
CompanyID CompName
----------+-----------
7 Comp3
8 Comp2
9 Comp1
10 Company
11 Company4
client_m
clid AttachToCompanyID clname
-----+--------------------+------
1 7 client1
2 7 client2
6 8 client3
companyuser_m
uid CompanyID uname
---+---------+-------
1 7 user1
2 10 user2
My Goal
I need to get the count of all clients and users of the corresponding company
Expected Output
CompanyID CompName clientcount usercount
---------+-----------+--------------+--------
7 Comp3 2 1
8 Comp2 1 0
9 Comp1 0 0
10 Company 3 0 1
11 Company4 0 0
My Code
select company_m.CompanyID,company_m.CompName,
count(client_m.AttachToCompanyID) as clientcount ,
count(companyuser_m.CompanyID) as usercount from company_m
left join client_m on client_m.AttachToCompanyID=company_m.CompanyID
left join companyuser_m on companyuser_m.CompanyID=company_m.CompanyID
group by company_m.CompanyID
But the above code not gives the correct output.
check my fiddle
You should be left joining to separate subqueries which find the counts in the respective client_m and companyuser_m tables:
SELECT
t1.CompanyID,
t1.CompName,
COALESCE(t2.cnt, 0) AS client_count,
COALESCE(t3.cnt, 0) AS user_count
FROM company_m t1
LEFT JOIN
(
SELECT AttachToCompanyID, COUNT(*) AS cnt
FROM client_m
GROUP BY AttachToCompanyID
) t2
ON t1.CompanyID = t2.AttachToCompanyID
LEFT JOIN
(
SELECT CompanyID, COUNT(*) AS cnt
FROM companyuser_m
GROUP BY CompanyID
) t3
ON t1.CompanyID = t3.CompanyID;

SQL Inner Join Group By

Table - Customers
id FirstName LastName
23 James Smith
24 Tom Raven
25 Bob King
Table - Orders
id CustomerID
30 23
31 24
32 23
33 25
Table - Products
OrderID Product1 Product2 Product3
30 1 0 0
31 2 0 0
32 0 1 1
33 2 1 0
I want to count the total number of Products for each Customer so answers would be:
CustomerID FirstName LastName Total
23 James Smith 3
24 Tom Raven 2
25 Bob King 3
So far I've got for the SQL query:
SELECT Customers.id, Orders.id,
FROM Customers
INNER JOIN Orders ON Customers.id = Orders.CustomerID
INNER JOIN Products ON Orders.id = Products.OrderID
Not sure how to count up the products though.
Any help would be much appreciated.
select c.id as CustomerID
,(sum(p.Product1) + sum(p.Product2) + sum(p.Product3)) as ProductCount
from Customers c
inner join Orders o on c.id = o.CustomerID
inner join Products p on o.id = p.OrderID
group by c.id
You could use
SELECT c.id as CustomerID, c.firstname, c.lastname,
sum(p.Product1 + p.Product2 + p.Product3) as total
FROM Customers c
INNER JOIN Orders o
ON c.id=o.CustomerID
INNER JOIN Products p
ON o.id=p.OrderID
group by c.id,c.firstname, c.lastname;
And as #Thorsten Kettner's comment, you should consider normalizing your table design.
Add a GROUP BY clause! Normally it includes the selected columns that are not arguments to a set function.
SELECT O.CustomerID, C.FirstName, C.LastName, count(*) as Total
FROM Customers C
INNER JOIN Orders O ON C.id = O.CustomerID
INNER JOIN Products P ON O.id = P.OrderID
GROUP BY O.CustomerID, C.FirstName, C.LastName
PS. Now using table aliases, to save some typing.
SELECT Customers.id, Customers.firstname, Customers.lastname,count(*) as total FROM Customers
INNER JOIN Orders
ON Customers.id=Orders.CustomerID
INNER JOIN Products
ON Orders.id=Products.OrderID
group by Customers.id,Customers.firstname, Customers.lastname
A more conventional approach might be as follows:
DROP TABLE IF EXISTS customers;
CREATE TABLE customers
(customer_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,firstname VARCHAR(20) NOT NULL
,lastname VARCHAR(20) NOT NULL
);
INSERT INTO customers VALUES
(23,'James','Smith'),
(24,'Tom','Raven'),
(25,'Bob','King');
DROP TABLE IF EXISTS orders;
CREATE TABLE orders
(order_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,customer_id INT NOT NULL
);
INSERT INTO orders VALUES
(30 ,23),
(31 ,24),
(32 ,23),
(33 ,25);
DROP TABLE IF EXISTS order_detail;
CREATE TABLE order_detail
(order_id INT NOT NULL
,product_id INT NOT NULL
,quantity INT NOT NULL
,PRIMARY KEY(order_id,product_id)
);
INSERT INTO order_detail VALUES
(30 ,1 ,1),
(31 ,1 ,2),
(33 ,1 ,2),
(32 ,2 ,1),
(33 ,2 ,1),
(32 ,3 ,1);
SELECT c.*
, SUM(od.quantity) total
FROM customers c
JOIN orders o
ON o.customer_id = c.customer_id
JOIN order_detail od
ON od.order_id = o.order_id
GROUP
BY c.customer_id;
+-------------+-----------+----------+-------+
| customer_id | firstname | lastname | total |
+-------------+-----------+----------+-------+
| 23 | James | Smith | 3 |
| 24 | Tom | Raven | 2 |
| 25 | Bob | King | 3 |
+-------------+-----------+----------+-------+
SELECT DISTINCT CustomerID, FirstName, LastName, Total
FROM ( SELECT id AS CustomerID, FirstName, LastName FROM Customers ) AS c
NATURAL JOIN
( SELECT id AS OrderID, CustomerID FROM Orders ) AS o
NATURAL JOIN
( SELECT OrderID, SUM( Product1 + Product2 + Product3 ) AS Total
FROM Products
GROUP
BY OrderID ) AS p;

MySQL includes a specific row with order by

Given 2 tables, I want to generate top 3 highest amount from [Purchase] table.
Additional criteria is [Crocs] must be included in top 3 of the records.
I have following SQL, but it cannot generates the result as I wanted (Result A), please guide me on how to pull out the result in Result B. Thank you.
Table (Purchase):
Purchase_ID | StoreID | Amount
------------|---------|--------
1 | 21 | 22
2 | 23 | 13
3 | 25 | 6
4 | 26 | 23
5 | 28 | 18
Table (Store):
Store_ID | StoreName
---------|----------
21 | Adidas
22 | Nike
23 | Puma
24 | New Balance
25 | Crocs
26 | Converse
SQL:
SELECT IF(SUM(amount) IS NULL, 0, SUM(amount)) as totalAmount
FROM (
SELECT a.amount
FROM purchase a
INNER JOIN store b
ON a.store_id = b.storeid
GROUP BY a.amount
HAVING b.StoreName = 'Crocs'
ORDER BY a.amount DESC
LIMIT 3
) t
Result A: $6
Explanation A: Amount of Crocs is $6
Result B: $51
Explanation B: Total Amount of top 3 = $22 (Adidas) + 23 (Puma) + $6 (Crocs)
The answer from scaisEdge is almost right, but the first query could also return a row with crocs and the sorting is wrong (order by max(a.amount) limit 2 means that the lowest 2 results will be shown). Additionally you could wrap the query in another select query to sort the results
SELECT * FROM (
SELECT b.storename, max(a.amount) as maxAmount
FROM purchase a
INNER JOIN store b ON a.store_id = b.storeid
WHERE b.storename != 'crocks'
GROUP BY a.storename
ORDER BY max(a.amount) DESC
LIMIT 2
UNION
SELECT b.storename, a.amount as maxAmount
FROM purchase a
INNER JOIN store b
ON a.store_id = b.storeid
WHERE b.storename='crocks'
ORDER BY a.amount DESC
LIMIT 1
) ORDER BY maxAmount DESC
You could use an union
SELECT b.storename, max(a.amount)
FROM purchase a
INNER JOIN store b
ON a.store_id = b.storeid
GROUP BY a.storename
order by max(a.amount) limit 2
union
SELECT b.storename, a.amount
FROM purchase a
INNER JOIN store b
ON a.store_id = b.storeid
where b.storename='crocks'
try this one:
SELECT sum(amount)as sum_amount,a.store_id,storename,category from
(select amount,store_id from tbl_purchase) as a
inner JOIN
(select store_id,storename,category from tbl_store)as b on a.store_id = b.store_id where b.category = 'supermarket' GROUP BY category

SQL - How to calculate column value and join with another table

As I am not good with MySQL query's so I wish someone help me for creating this kind of sql query.
I having two MySQL tables which is describe bellow:
Table Name: rating
-------------------
property_id user_id area_rate_count safety_rate_count friendly_rate_count walkability_rate_count
4 28 1 1 1 2
5 38 2 3 4 1
5 40 2 2 3 1
6 40 2 3 1 4
10 43 2 2 3 1
Table Name: listing
-------------------
property_id title
4 Sample 1
5 Sample 2
6 Sample 3
10 Sample 4
11 Sample 5
12 Sample 6
Now first I want to sum each column and divide. (area_rate_count, safety_rate_count, friendly_rate_count, walkability_rate_count). For example In property_id:5 having two times so first calculate column sum and divide by 2.
After calculation we will get this output:
Table Name: rating (After Calculation)
--------------------------------------
property_id rate
4 5
5 9 (Divided by 2 because this property_id is two times in table)
6 10
10 8
And Finally I want join this result to my listing table and result looks something like this:
Table Name: listing
-------------------
property_id title rate
4 Sample 1 5
5 Sample 2 9 (Divided by 2 becouse property_id is two times in table)
6 Sample 3 10
10 Sample 4 8
11 Sample 5 0
12 Sample 6 0
Thanks.
I think you want the avg() aggregation function along with a join:
select l.property_id, l.title,
coalesce(avg(area_rate_count + safety_rate_count + friendly_rate_count + walkability_rate_count
), 0) as rate
from listing l left outer join
property_id p
on l.property_id = p.property_id
group by l.property_id, l.title ;
If I understood it right I think you need this:
select l.property_id, l.title, coalesce(r.ssum/if(r.ct=0,1,r.ct), 0) as rate
from listing l LEFT JOIN
(select property_id,
sum(area_rate_count+safety_rate_count
+friendly_rate_count+walkability_rate_count) ssum,
count(*) ct
from rating
group by property_id ) r
ON l.property_id = r.property_id
order by l.property_id
See it here on fiddle: http://sqlfiddle.com/#!2/589d6/5
Edit
As OP asked on the comments that he wants all columns from listing here is what he want:
select l.*, coalesce(r.ssum/if(r.ct=0,1,r.ct), 0) as rate
from listing l LEFT JOIN
(select property_id,
sum(area_rate_count+safety_rate_count
+friendly_rate_count+walkability_rate_count) ssum,
count(*) ct
from rating
group by property_id ) r
ON l.property_id = r.property_id
order by l.property_id
CREATE TEMPORARY TABLE IF NOT EXISTS
temp_table ( INDEX(col_2) )
ENGINE=MyISAM
AS (
SELECT
property_id,
AVG(area_rate_count) as area_rate_count,
AVG(safety_rate_count) as safety_rate_count,
AVG(friendly_rate_count) as friendly_rate_count,
AVG(walkability_rate_count) as walkability_rate_count
FROM rating
GROUP BY property_id
)
SELECT * FROM listing L
JOIN temp_table T
ON L.property_id = T.property_id
Use the below statement to get distinct property_id with its own rate
select property_id, sum(separaterating)/count(property_id) from (
select property_id,sum(area_rate_count , safety_rate_count , friendly_rate_count , walkability_rate_count) as separaterating from rating group by property_id AS temp ) group by
property_id
you can then join with the other table to get the final result as below
select * from ( select property_id, sum(separaterating)/count(property_id) from (
select property_id,sum(area_rate_count , safety_rate_count , friendly_rate_count , walkability_rate_count) as separaterating from rating group by property_id AS temp ) group by
property_id) AS A inner join listing AS B on A.property_id = B.property_id
try this:
select a.prop_id as property_id, l.title, a.allratings / b.numberofreviews as rate
from
(
select property_id as prop_id, SUM(coalesce(area_rate_count,0) + coalesce(safety_rate_count,0) + coalesce(friendly_rate_count,0) + coalesce(walkability_rate_count,0)) as allratings
from rating
group by property_id
) a inner join
(
select property_id, count(distinct user_id) as numberofreviews
from rating
group by property_id
) b on a.property_id = b.property_id
inner join listing l on a.property_id = l.property_id
Try This Query
select ls.property_id,ls.title,inr.rate from listing as ls
left join
(select r.property_id as pid,r.rate/r.cnt as rate from
(select property_id,user_id,(area_rate_count+safefty_rate_count+friendly_rate_count+walkability_rate_count) as rate,count(*) as cnt from rating group by property_id) as r) as inr on inr.pid=ls.property_id