there any ways to merge row values. suppose emp Name is same for different month.
for e.g
emp Name Jan Feb Mar
a 1
a 5
a 9
I want result Like:-
emp Name Jan Feb Mar
a 1 5 9
Try MAX:
SELECT `emp Name`,MAX(Jan) as Jan,MAX(Feb) as Feb, MAX(Mar) as Mar
FROM TableName
GROUP BY `emp Name`
Learn more about the aggregate function MAX() here.
Related
I have table with 2 columns: Year And Month.
Example:
Year | Month
1. 2020 9
2. 2020 8
3. 2020 7
4. 2020 5
5. 2020 4
I don,t have record for 06.2020, so as a result i want to get only all the results before the break: 1,2,3
Example 2:
1. 2020 12
2. 2020 11
3. 2020 10
4. 2020 09
5. 2020 08
6. 2020 07
7. 2020 05
8. 2020 04
9. 2020 02
I want to get as result: 1,2,3,4,5,6
One method is:
select t.*
from (select t.*,
sum(case when next_yyyymm <> year * 12 + month + 1 then 1 else 0 end) over (order by year desc, month desc) as num_misses
from (select t.*,
lead(year * 12 + month) over (order by year, month) as next_yyyymm
from t
) t
) t
where num_misses = 0;
This adds up the number of misses up to a given year/month combination. It returns the rows with no missing month.
Here is a db<>fiddle showing both examples.
I am trying to display records using group by name but when the sdate is available.
my table :
Example
Hotel Tampa is available on jan 1, jan 4,jan 6
then I want to show like this
name
available date
tampa
jan 1 , jan 2 , jan 6
tabcd
jan 3 , jan 9 , jan 12
SELECT name, GROUP_CONCAT(sdate) as gr_date FROM table_name GROUP BY name
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
)
I have a table "UserLogins" and when the user login into the system I will drop a record in the "UserLogins" table.
Jan 10th (Users : 1,2,3,4) // four records with Ids 1,2,3,4
Jan 20th (1,2,3,4,5,6) // six records with Ids 1,2,3,4,5,6
Jan 30th (1,2)
Feb 10th (1,7)
Feb 20th (2,6,8)
Feb 25th (1,2,3,5)
Mar 10th (3,4)
Mar 20th (4,5,9)
Mar 30th (8,10,11)
Apr 10th (10,12)
Apr 20th (1,2,3,6,13, 14, 15)
Apr 30th (11,12,16)
When I write the group by my results as follows
Jan - 6
Feb - 7
Mar - 7
Apr - 11
But I need an out put like as follows
Upto Jan - 6 //count of distinct users upto Jan
Upto Feb - 8 //count of distinct users upto Feb
Upto Mar - 11 //count of distinct users upto Mar
Upto Apr - 16 //count of distinct users upto Apr
Your first count could simply be like this:
SELECT
DATE_FORMAT(login_date, '%Y-%b') AS year_month,
COUNT(DISTINCT user_id)
FROM
UserLogins
GROUP BY
DATE_FORMAT(login_date, '%Y-%b')
while to count all users up to a given mount, I would use a Join:
SELECT
DATE_FORMAT(last_day, '%Y-%b') AS year_month,
COUNT(DISTINCT user_id)
FROM
(SELECT DISTINCT LAST_DAY(login_date) as last_day
FROM UserLogins) d
INNER JOIN
UserLogins ON UserLogins.login_date<=last_day
GROUP BY
DATE_FORMAT(last_day, '%Y-%b')
on the subquery d I will return all last days of every month that has a record on the UserLogins table, then I will join all userlogin that logged up to the end of the month, and then I will do the count.
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.