Complex mysql query counting - mysql

I have 1 table name "companies" with several datas like :
Id
Owner
Company
Job
1
John Doe
Company 1
CEO
1
John Doe
Company 2
CEO
1
John Doe
Company 3
CEO
1
Gab Durand
Company 4
CEO
1
Rob Dujn
Company 5
CTO
1
Alex DoeMorse
Company 6
COO
What I need is to get 1 line by company with a row calculating the number of company own by each person.
This is my desired output :
Id
Owner
Company
Job
Count
1
John Doe
Company 1
CEO
3
1
John Doe
Company 2
CEO
3
1
John Doe
Company 3
CEO
3
1
Gab Durand
Company 4
CEO
1
1
Rob Dujn
Company 5
CTO
1
1
Alex DoeMorse
Company 6
COO
1
What could be the mysql query?
EDIT : DB version 5.6.51
Thanks!

You can add an extra column containing analytic function such as
COUNT(*) OVER (PARTITION BY Id, owner) AS count
if DB version is 8.0
As having a former DB version, prefer using correlated subquery such as
SELECT Id, Owner, company, Job,
(SELECT COUNT(*)
FROM t
WHERE id = tt.id
AND Owner = tt.Owner ) AS count
FROM t AS tt
Demo

Query:
SELECT Owner, Job, count(Company) NoOfCompanies
FROM companies
WHERE Job='CEO'
GROUP BY Owner,Job;
Note: Grouping done on Owner and Job expecting you have other values along with 'CEO'

Related

Count and Distinct SQL query

I have a table of Books, that contains the following columns:
Book_Id User_Id
001 1
002 2
001 1
004 2
005 3
006 3
007 2
008 2
009 1
Where :
Book_Id - identifier of a book that a user read; User_Id - identifier
of a reader/user.
Let's assume that User1 read books three times, but 2 of them were same, so the user 1 read 2 distinct books (001 and 009). User 2 read 4 distinct books, while user 3 read 2 distinct books.
In overall, there are 2 users that read 2 distinct books, and 1 user that read 4 distinct books.
The output expected is as below:
Distinct_Books_Count --- User_Count
2 2
4 1
I tried the following:
SELECT COUNT(DISTINCT Book_Id), COUNT(User_Id) FROM Books GROUP BY
User_Id
But I receive the following table:
Distinct_Books_Count User_Count
2 3
4 4
2 2
So any alternative solution or changes?
I call this a "histogram of histograms" query. You can do it using two group bys:
SELECT num_books, COUNT(*)
FROM (SELECT b.User_Id, COUNT(DISTINCT Book_Id) as num_books
FROM Books b
GROUP BY User_Id
) b
GROUP BY num_books
ORDER BY num_books;

How to perform these joins with MySQL?

Having these tables:
clients
-------------------
id
name
town
companies
------------------
id
name
credits
------------------
clients_id
companies_id
credit
Example:
clients
-------------
1 john doe London
2 jane smith Paris
companies
-------------
1 mycompany1
2 mycompany2
credits
-------------
1 1 5000
1 2 3250
2 2 4500
How can I list all the clients including an additional column for each credit she has in a company? I also need to alias each of these columns in this fashion: "credit_"
Something like:
id name town credit_1 credit_2
1 john doe London 5000 3250
2 jane smith Paris NULL 4500
I'm not sure if doing two LEFT JOINS to credits would work.

prediction on rowwise data or progressive data

I am working on employee attrition analysis with a table having rowwise data for a (employee like Id, name, Date_Join Date_Relieving Dept Role etc)
eID eName Joining Releiving Dept Married Experience
123 John Doe 10Oct15 12Oct16 HR No 12
234 Jen Doee 01jan16 -NA- HR No 11 (ie she is available)
I can run regression on this data to find the beta coefficient
eID eName Joining Releiving Dept Married Experience
123 John Doe 10Oct15 12Oct16 HR No 12
234 Jen Doee 01jan16 -NA- HR No 11
But I've seen other approach too.. where employee have multiple entries depending on their difference between joining date and current month or relieving month(say Employee A joined in Jan and Left in Dec so he'll have 12 entries updating corresponding columns like experience and marriage etc)
eID eName Dept Married Experience
123 John Doe HR No 0
123 John Doe HR No 1
123 John Doe HR Yes 2
123 John Doe HR Yes 3
can someone tell what differentiate two approaches.. and what is the outcome of this second approach.

SQLite COUNT and selecting from multiple tables

Consider the database (made up for example):
Table 'owner'
id name
1 john
2 andrew
3 peter
Table 'duck'
id name ownerID
1 duck1 1
2 duck2 1
3 duck3 1
4 duck4 2
5 duck5 2
6 duck6 3
7 duck7 3
8 duck8 1
9 duck9 3
10 duck10 3
11 duck11 1
12 duck12 2
Table 'food'
id name type duckID
1 beef meat 4
2 grass veg 8
3 lemon fruit 5
4 apple fruit 3
5 pizza snack 7
I wish to write some SQL that for each OWNER, COUNT the number of ducks which eat some kind of food. For example for owner John, there would be 2 ducks (ducks 3 and 8).
So far I am writing this code:
select owner.name, count(duck.ownerID)
from duck, food, owner
where duck.id == food.duckID
and duck.ownerID == owner.id;
I am getting the result:
Peter | 5
Any suggestions are appreciated.
This is done with an group by clause:
select owner.name, food.name, count(duck.id)
from duck, food, owner
where duck.id == food.duckID
and duck.ownerID == owner.id
group by owner.name, food.name;
This query gives you one row for each combination of owner name and food name with the number of the owner's ducks eating this food.

Calculate sum from three tables

I have 3 mysql tables:
Client_courses
Client_courses_id Client_id Course_id
1 1 2
2 1 3
3 2 1
Client
Client_id Name
1 Tom
2 John
Courses
Course_id Name Price
1 Microsofr 100
2 Programming 250
3 Leadership 300
I need to calculate how much every client spent money on courses. For example: Tom spent 550 (250+300), John spent 100. And I am confused how to do it.
SELECT SUM(c.Price), cl.Name
FROM Client cl
INNER JOIN Client_courses clc ON cl.Client_id=clc.Client_id
INNER JOIN Courses cs ON clc.Course_id=cs.Course_id
GROUP BY cl.Name