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.
Related
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'
Suppose I have a table like this:
name position zipcode
-------------------------
Gary Gm 12345
Rob VP 54321
John Manager 1234
After 20 minutes some rows were added in the source and now the table looks like
name position zipcode
------------------------
Gary Gm 12345
Rob VP 54321
John Manager 1234
Chris Director 5478
Kane VP 9999
So Kane and Chris were added after 20 minutes in the source, and there is no date time field to identify on which day the rows were added, I just wanted to know if there are any functions to identify the newly added rows in the table in SQL Server 2014?
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.
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
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