MySQL-Count from 2 tables - mysql

I have 2 tables. Table table1 like this
specialty doctor
1 A
1 B
2 A
2 C
Table table2 like this:
specialty doctor
1 A
1 D
2 C
I want to count distinct doctor from both the table group by specialty and have the output like this
specialty doctor
1 3
2 2
(specialty 1 has 3 doctors:A, B, D; specialty 2 has 2 doctors: A, C)
Really appreciate your help

You can use union to bring the tables together and then group by:
select specialty, count(*)
from ((select specialty, doctor from table1) union
(select specialty, doctor from table2)
) t
group by specialty;
Note the use of union here and not union all. You specifically want to eliminate duplicates.

Related

Mysql sum restult from a query

I have this table of a database:
cou_id | cou_tea_1 | cou_tea_2 | cou tea_3
1 1 1 2
2 2 1 1
3 4 1 1
Each course has an id, and cou_tea_1, cou_tea_2, cou tes_3 has the teacher that teaches in the lesson 1 of the course, lesson 2 and lesson 3.
In this example in the course 1 the first lesson was made by the teacher 1, the second by the teacher 1 and the third by the teacher 2.
I need a query that gives me the number of the teacher and the total of the lessons he made, in this example:
teacher | total_lesson_number
1 6
2 2
4 1
This is a very poor table design. In general, when you have multiple columns with the same name, you are storing values in columns. These should be in a row.
What you need to do is unpivot the data and then aggregate:
select teacher, count(*) as total_lesson_number
from (select cou_tea_1 as teacher from t union all
select cou_tea_2 from t union all
select cou_tea_3 from t union all
select cou_tea_4 from t
) t
group by teacher;
After getting this to work, you should study up on junction tables so you know the right way to store such data in a relational database.
You can use UNION ALL to unpivot the table, then GROUP BY the teacher id:
SELECT teacher, COUNT(*) AS total_lesson_number
FROM (
SELECT cou_id, cou_tea_1 AS teacher
FROM mytable
UNION ALL
SELECT cou_id, cou_tea_2
FROM mytable
UNION ALL
SELECT cou_id, cou tea_3
FROM mytable) AS t
GROUP BY teacher
Not a new idea I did not want to discard...
select teacher, sum(n) as total_lesson_number from (
(select cou_tea_1 as teacher, count (*) as n from course group by cou_tea_1)
union all
(select cou_tea_2 as teacher, count (*) as n from course group by cou_tea_2)
union all
(select cou_tea_3 as teacher, count (*) as n from course group by cou_tea_3)
)
group by teacher

Multiple Aggregate with different Group By

I have table User, Company, ParentCompany and table Goal.
Each Company have ParentCompany, and each User inside one Company. Goal have number of action, and type of Goal, user who execute the goal, and time executed.
I want to calculate the number of action in a date range for each type of Goal, for each user, company, and parent_company. Number of action for each company equal to sum of action for user that reside in that company.
More or less after some join query, I able to get this table below, where column id is id of company, parent_id is id of companyparent, and num is number of goal for all user inside of id company.
id parent_id num
----------- -------------------- -----------------------
1 3 1
2 1 2
3 1 1
4 2 4
Now I want to make it like below:
id parent_id sum_id sum_parent
----------- -------------------- -------------- -------------
1 3 1 1
2 1 2 3
3 1 1 3
4 2 4 4
How can I make it works? I can get one of the value (sum_id or sum_parent) with GROUP BY,
SELECT id,SUM(num) AS sum_id FROM tableA GROUP BY id
or
SELECT parent_id,SUM(num) AS sum_parent FROM tableA GROUP BY parent_id
but is there any way to make it only in one query? tableA results from query with 5 join inside.
Try this:
SELECT a.id, a.parent_id, a.sum_id, b.sum_parent
FROM (SELECT id, parent_id, SUM(num) sum_id FROM tableA a GROUP BY id) a
INNER JOIN (SELECT parent_id, SUM(num) sum_parent FROM tableA a GROUP BY parent_id) b ON a.parent_id = b.parent_id
Try this :
SELECT
t1.id, t1.parent_id, t1.sum_id, t2.sum_parent
FROM
(SELECT id, parent_id, SUM(num) AS sum_id FROM mytable GROUP BY id) t1
INNER JOIN
(SELECT parent_id, SUM(num) AS sum_parent FROM mytable GROUP BY parent_id) t2
ON t1.parent_id = t2.parent_id
Apparently what I want can be done with WITH ROLLUP statement. (http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html)
With
SELECT id,sum(num) FROM tableA GROUP BY parent_id, id WITH ROLLUP;
will results in
parent_id id sum(num)
----------- -------------------- --------------
1 2 2
1 3 1
1 null 3
2 4 4
2 null 4
3 1 2
3 null 2

Explode prevsiouly rolled up data in SQL

I have data as follows:
Product Quantity
A 3
B 2
This is data that as been previously rolled up at the product level. Assume there are only two columns as of now.
I want an output as follows:
Product Quantity
A 1
A 1
A 1
B 1
B 1
You could use a trick like this:
SELECT Product, 1 AS Quantity
FROM
Products INNER JOIN (
SELECT 1 AS q UNION ALL
SELECT 2 UNION ALL SELECT 2 UNION ALL
SELECT 3 UNION ALL SELECT 3 UNION ALL SELECT 3
) quantities
ON Products.Quantity = quantities.q
of course, this query is limited to a quantity of 3, but you can add more quantities to your subquery if they are of a limited amount.

Is it possible to join two tables into one single table , both tables have the same column names ...?

Is it possible to join two tables in mysql into one single table , both tables have the same column names ...?
table a has
id name state
1 jose up
2 sam mp
3 jack tn
table b is
id name state
4 ken ker
5 sk wb
is it possible to join both like:
id name state
1 jose up
2 sam mp
3 jack tn
4 ken ker
5 sk wb
Use UNION ALL
SELECT
id,name,state
from
tbla
UNION ALL
SELECT
id,name,state
from
tblb
If you want the exact duplicates to be excluded from the output. Use UNION
SELECT
id,name,state
from
tbla
UNION
SELECT
id,name,state
from
tblb
Reference:
13.2.8.4 UNION Syntax
The requirement you are describing is a union all query, not a join:
SELECT id, name, state
FROM table_a
UNION ALL
SELECT id, name, state
FROM table_b

Counting unique numbers in a column MySQL

I have a query that returns data in the following format:
id | name | number
1 John 12545
1 John 50496
2 Mary 23443
3 Mark 54
3 Mark 5600
3 Mark 50206
I would like to find out the number of distinct ids that appear in the result set. For example, for the result above. I would like to obtain the value 3.
Is there any way to add a column so the result looks like this instead?
count | id | name | number
3 1 John 12545
3 1 John 50496
3 2 Mary 23443
3 3 Mark 54
3 3 Mark 5600
3 3 Mark 50206
My query is:
SELECT * FROM (
SELECT id FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) totalCount,
id,name,number
FROM tableName
or by using CROSS JOIN
SELECT x.totalCount,
a.id, a.name, a.number
FROM tableName a, (SELECT COUNT(DISTINCT id) totalCount
FROM tableName) x
You should try :
SELECT id,name,number, (SELECT COUNT(DISTINCT name) FROM YourTableName) FROM YourTableName
Good luck
SELECT COUNT(DISTINCT id) would be faster than using column name.
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) as 'count',
id,name,number
FROM tableName
SELECT COUNT(id) AS count , id, name, number
FROM
(
SELECT id
FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
GROUP BY id, name, number