I have employees table with date_of_join field
and I have employee_leaves table with the following fields:
employee_id
leave_from
leave_to
total_days
the employee joined on 15 Feb 2011
I want to have a query showing the cound of leaves for every employee years based on his date_of_join
for example, if the employee joined on 15 Feb 2011 then the result will be like this:
Feb 2011 to feb 2012 ---- totals days: 21
Feb 2012 to feb 2013 ---- totals days: 26
Feb 2013 to feb 2014 ---- totals days: 8
where Feb to feb is the employee year so it's from 15 Feb to 14 Feb every year
can anyone help please?
Not having your data it is difficult to test this but I came up with the following based on your description:
SELECT employees.employee_id, DATE_ADD(employees.date_of_join, INTERVAL yrs.years) frm
,DATE_ADD(employees.date_of_join, INTERVAL yrs.years + 1) too,
SUM(employee_leaves.total_days)
FROM employee_leaves
INNER JOIN (SELECT 0 years UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) yrs
ON 1=1
INNER JOIN employees ON employee_leaves.employee_id = employees.employee_id
AND employee_leaves.leave_from BETWEEN DATE_ADD(employees.date_of_join, INTERVAL yrs.years) AND DATE_ADD(employees.date_of_join, INTERVAL yrs.years + 1)
GROUP BY employees.employee_id, DATE_ADD(employees.date_of_join, INTERVAL yrs.years)
;
Probably needs some fiddling, hope this helps.
Related
My dataset has the following format;
Student_id Month Year Amount
1 Jan 2010 600
1 Feb 2010 391
1 Apr 2010 673
1 Jul 2010 564
5 Jan 2010 789
5 Mar 2011 298
5 Aug 2010 347
7 Jan 2010 654
7 Dec 2011 621
7 Apr 2010 450
7 Nov 2011 980
... & so on.
I wish my output which will have max amount for each unique id-month-year combination. Viz,
Student_id Month Year Amount
1 Apr 2010 673
5 Jan 2010 789
7 Nov 2011 980
... & like this.
How to get the output using SQL? I tried
select distinct * , MAX(Amount) from student_details;
&
SELECT *, MAX(Amount)
FROM student_details
WHERE Amount IN
(
FROM student_details
GROUP BY Student_ID, Year, Month
);
but the output is not as desired.
Please suggest assistance. Thanks in advance.
In MySQL:
SELECT t0.*
FROM student_details AS t0
LEFT JOIN student_details AS t1 ON t0.Student_id=t1.Student_id t1.Amount>t0.Amount
WHERE t1.Student_id IS NULL;
In SQL server:
SELECT T.Student_id,T.Month,T.Year,T.Amount
FROM
(
SELECT *,row_number() over (PARTITION BY Student_ID ORDER BY Amount DESC) as RN
FROM student_details
)T
WHERE T.RN=1
Simply use below query
first select amount month for each user then select data which has selected month
SELECT * FROM table WHERE amount IN (
SELECT max(amount) FROM table group by student_id
)
If today is 10 JULY 2015 then what I need is a record set in SQL Server which has the following records
6 JULY 2015
29 JUNE 2015
22 JUNE 2015
15 JUNE 2015
8 JUNE 2015
1 JUNE 2015
25 MAY 2015
With the following expression you get the last Monday:
CAST(GETDATE()-(##DATEFIRST-1+DATEPART(weekday,GETDATE())) % 7+1 AS DATE)
Here we use ##DATEFIRST and DATEPART(weekday,...) to find the last MONDAY.
Then you should generate 8 (two months) rows sequence of days each of them -7 days prior the next. Here I've used CTE but you can use UNION or TOP 8 rows from existing (system or user) in your DB table to generate 8 rows.
;WITH CT(n) AS
(
SELECT 0
UNION ALL
SELECT n+1 FROM CT WHERE n < 7
)
SELECT DATEADD(DAY,-n*7,
CAST(GETDATE()-
(##DATEFIRST-1+DATEPART(weekday,GETDATE())) % 7+1 AS DATE)) as DT
FROM CT ORDER BY n
Sqlfiddle demo
Try this
;WITH dates AS
(
SELECT CONVERT(datetime,cast(month(getdate())-2 as varchar(2))+'/'+cast(day(getdate()) as varchar(2))+'/'+ cast(year(getdate()) as varchar(4))) as Date,' ' as eid
UNION ALL
SELECT DATEADD(d,1,[Date]),' ' as eid
FROM dates
WHERE DATE < GETDATE()
)
select datename(DD,dates.date)+' '+datename(MM,dates.date)+' '+ datename(YYYY,dates.date) from dates
where datename(dw,dates.date) = 'Monday'
I am trying to count the number of events in the subchannel column of my l_events table between the dates 2013-10-01 and 2014-03-31 and group by specific subchannel, year and month.
Right now my results look like:
year month subchannel count(*)
2013 10 creativemornings 1
2014 3 creativemornings 2
2013 11 founderinstitute 1
I would like my results to include rows for each specific subchannel where no events occurred occurred in that month. So something like:
year month subchannel count(*)
2013 10 creativemornings 1
2013 11 creativemornings 0
2013 12 creativemornings 0
2014 1 creativemornings 0
2014 2 creativemornings 0
2014 3 creativemornings 2
2013 10 founderinstitute 0
2013 11 founderinstitute 1
2013 12 founderinstitute 0
2014 1 founderinstitute 0
2014 2 founderinstitute 0
2014 3 founderinstitute 0
I tried to join my query to my calendar table (which includes a datetime column of all relevent dates) but it didn't work. Any thoughts?
SELECT
year(l.created) as year,
month(l.created) as month,
l.subchannel,
count(*)
FROM db.l_events l
right JOIN db.calendar c
ON (date(l.created) = c.datefield)
WHERE
l.created between '2013-10-01' and '2014-03-31'
group by
l.subchannel,
year(l.created),
month(l.created)
;
This will do it. It creates a Cartesian product of your subchannel list and your calendar table and LEFT JOINs the l_events table to that result set.
SELECT
year(c.datefield) as year,
month(c.datefield) as month,
s.subchannel,
count(l.created)
FROM db.calendar c
CROSS JOIN (select distinct subchannel from l_events) s
LEFT JOIN db.l_events l
ON (date(l.created) = c.datefield)
AND l.subchannel = s.subchannel
Where c.datefield between '2013-10-01' and '2014-03-31'
group by
s.subchannel,
year(c.created),
month(.created)
order by
s.subchannel,
year(c.created),
month(.created)
;
I have been trying to fix my query for days now :(.
It is supposed to group by month and return 0 if record is not found.
An example:
SELECT MONTH(created_at) AS month_num,
DATE_FORMAT(created_at, '%b') AS month_name, COUNT(*) AS total_num
FROM table WHERE user_id=1384249399168
GROUP BY MONTH(created_at) ORDER BY created_at DESC
Result:
month_num month_name total_num
8 Aug 20
7 Jul 15
5 May 39
2 Feb 10
1 Jan 8
Espected Result:
month_num month_name total_num
12 Dec 0
11 Nov 0
10 Oct 0
9 Sep 0
8 Aug 20
7 Jul 15
6 Jun 0
5 May 39
4 Apr 0
3 Mar 0
2 Feb 10
1 Jan 8
I have tried a lot of answers especially this MySql count() to return 0 if no records found but without success.
Any help please?
I haven't tested but it should work:
SELECT MONTH(created_at) AS month_num,
DATE_FORMAT(created_at, '%b') AS month_name, ifnull(count(*),0) AS total_num
FROM table WHERE user_id=1384249399168
GROUP BY MONTH(created_at) ORDER BY created_at DESC
This post solved my problem.
I had to create a table which contains all the 12 months.
mysql select the count of records for every month
Thanks anyone especially #Turdaliev
I have to a table like this
Year Month
2012 8
2012 7
2012 4
2012 3
2011 7
2011 3
2011 1
2010 10
2010 9
2010 8
This tables show which month and year is remaning, now let say I have completed 2012, 7 month. Now I want to have list like
Year Month
2012 8
2012 4
2012 3
2011 7
2011 3
2011 1
2010 10
2010 9
2010 8
I am using below query but this is not giving me the correct records
SELECT YEAR, MONTH from tablex
WHERE Year NOT IN (SELECT DISTINCT YEAR from OTHER_TABLE INNER JOIN Some_Other_Table)
AND MONTH NOT IN (SELECT DISTINCT MONTH FROM OTHER_TABLE INNER JOIN Some_Other_Table)
When OTHER_TABLE is null then I am getting the currect count, but when Other table has year 2012 and month 7 I am getting no results.
P.S.: There is no joining columns available for tablex and OTHER_TABLE and Som_Other_Table
You just have to use NOT EXISTS:
SELECT YEAR, MONTH
from tablex t1
WHERE NOT EXISTS
(
SELECT 1 FROM OTHER_TABLE t2
WHERE t2.Year = t1.Year AND t2.Month = t2.Month
)
This answer assumes you have a single row {Year = 2012, Month = 7} in OTHER_TABLE to designate the completed month.
This won't work because you're checking the year and month separately. Therefore you are not removing the row (2012, 7) from the results, you're removing all rows with Year = 2012 and all rows with Month = 7. Try this anti-join instead:
SELECT x.YEAR, x.MONTH
FROM tablex x
LEFT OUTER JOIN OTHER_TABLE y ON x.YEAR = y.YEAR AND x.MONTH = y.MONTH
WHERE y.YEAR IS NULL