Mysql Query Studied Days - mysql

I have Emp table with following values
Emp_Id Emp_Name Subject Dates
001 Smith Java 07-02-2012
001 Smith oracle 08-02-2012
001 smith C++ 10-02-2012
002 john java 01-01-2012
002 john SE 10-01-2012
002 john c 10-01-2012
001 smith physics 04-01-2012
001 smith c# 07-02-2012
001 smith javascript 07-02-2012
Now as we can see here smith studied only 3 days for month February and 1 for month Jan
while john studied only 2 days for month January.
How can we calculate this count for any employee?
As a e.g:Output should be in following way.
Emp_Id Emp_Name Month_Year No_Of_Days_Studied_In_Month
001 smith Feb12 3
001 smith Jan12 1
002 john Jan12 2

You can GROUP BY YEAR(Dates), MONTH(Dates) and do a COUNT.
TRY:
SELECT emp_id,
emp_name,
Date_format(DATE, '%b%y') AS dates,
COUNT(*) AS No_Of_Days_Studied_In_Month
FROM emp
GROUP BY Date_format(DATE, '%b%y'), emp_name
ORDER BY emp.emp_id

Related

Complex mysql query counting

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'

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.

Mysql unable to get aggregate query

I have a table with values as follows(MySql):
SQL> SELECT deptno, ename FROM emp ORDER BY deptno, ename;
DEPTNO ENAME
------ ----------
10 CLARK
10 KING
10 MILLER
20 ADAMS
20 FORD
20 JONES
20 SCOTT
20 SMITH
30 ALLEN
30 BLAKE
30 JAMES
30 MARTIN
30 TURNER
30 WARD
14 rows selected.
but I need them in the following less convenient format:
DEPTNO ENAME
------ -----------------------------------------
10 CLARK, KING, MILLER
20 ADAMS, FORD, JONES, SCOTT, SMITH
30 ALLEN, BLAKE, JAMES, MARTIN, TURNER, WARD
Kindly help me to achiever above result/output.
Using group_concat
SELECT deptno, group_concat(ename order by ename) as ename
FROM emp
group by deptno
ORDER BY deptno;

Datediff in date format

I have 2 tables with structure as
Emp Table
id name
001 Smith
002 Jerry
Leave
sr.no reason from_date to_date request_by status
1 PL 2011-12-11 2011-12-15 001 Declined
2 PL 2011-11-13 2011-11-13 001 Approved
3 PL 2011-10-02 2011-10-05 002 Declined
Now I have written this query
select DATEDIFF(Leave.from_date,Leave.to_date)as cdate,
Emp.id as emp
from Leave left join Emp
on Leave.request_by=Emp.id
gives me difference between these 2 dates like...
cdate emp
-4 001
0 001
-3 002
The first thing about this output difference between '2011-12-11 & 2011-12-15 ' need to be 5 as for 5 consecutive days employee is absent. That we achieve it.
But I need this cdate in date format like('%Y%m%d') and + if date difference is say -4 then 4 records should be displayed for that.
So I want to write a query which gives output like this......
cdate emp
2011-12-11 001
2011-12-12 001
2011-12-13 001
2011-12-14 001
2011-12-15 001
2011-11-13 001
2011-10-02 002
2011-10-03 002
2011-10-04 002
2011-10-05 002
So can anybody tell me what how should I need to write my query to get this output?
Try this query -
CREATE TABLE temp_days(d INT(11));
INSERT INTO temp_days VALUES
(0),(1),(2),(3),(4),(5),
(6),(7),(8),(9),(10),
(11),(12),(13),(14),(15); -- maximum day difference, add more days here
SELECT l.from_date + INTERVAL td.d DAY cdate, e.id emp
FROM
`leave` l
LEFT JOIN Emp e
ON l.request_by = e.id
JOIN temp_days td
ON DATEDIFF(l.to_date, l.from_date) >= td.d
ORDER BY
e.id

10 period moving average in MySql without using date

I have a table of goalie data, snipet below
year gameid player sv% gamenum
2009 200165 John Smith 0.923 0165
2009 209754 John Smith 1.000 9754
2009 206938 John Smith 1.000 6938
2009 206155 John Smith 0.833 6155
2009 203021 John Smith 0.667 3021
2009 206472 John Smith 0.909 6472
2009 209524 John Smith 0.833 9524
2009 209351 John Smith 0.800 9351
2009 203056 John Smith 1.000 3056
2009 206761 John Smith 0.935 6761
2009 200466 John Smith 0.954 0466
2009 204171 John Smith 0.932 4171
2009 207876 John Smith 0.958 7876
2009 201581 John Smith 0.941 1581
2009 205286 John Smith 0.930 5286
2009 208991 John Smith 0.961 8991
2009 202696 John Smith 0.916 2696
2009 206401 John Smith 0.935 6401
2009 200106 John Smith 0.921 0106
2009 201381 John Smith 0.918 1381
I want to get the 10 game moving averages for each goalie, but I don't have dates or game numbers such as his first, second, third game, etc. The game ids are also assigned in the order they are played at the league level, so game 200106 could be his first game of season, and 200165 could be his 2nd, and so on.
My question is: How can I get the max(10 game moving average) and min(10 game moving average) grouped by each goalie for each year?
Also, is there a way to rank the game ids by goalie, year using MySql?
A 10 game moving average means that if you had less than 10 games, there is no meaningful average (not enough games). If you had 12 games, the average is taken between
1-10 (avg)
2-11 (avg)
3-12 (avg)
max / min across the 3 averages
The most efficient way to do this in MySQL would be to
select .. (involving 13 #variables to rownumber and rotate the last
10 values into the variables, keeping track of
#player, #year, #rownumber)
order by player, year, gameid
This will pass through the data only once, building the averages. An outer query will simply take min/max from this derived table. I'm not up for fleshing this out at the moment though.
This is one idea (fair warning:not tested)
SELECT max(mavg) FROM
(SELECT (SELECT avg(avgfield),min(gamenum) as gn FROM YourTable g WHERE g.gamenum>t.gamenum LIMIT 10),t.gamenum
FROM
YourTable t
) d
or
SELECT max(mavg) FROM
(SELECT t.gamenum FROM
YourTable t INNER JOIN
(SELECT avg(avgfield),min(gamenum) as gn FROM YourTable g WHERE g.gamenum>t.gamenum LIMIT 10) q ON q.gn = t.gamenum
) d