usert4jju7 usert4jju7 - 4 months ago 22
SQL Question

MySQL - Distinct per user/distinct with group by

One of the DB tables looks as below

-------------------------
date emp_name activity
-------------------------
2015-01-01 A button1
2015-01-01 A button2
2015-01-01 B button1
2015-01-01 C button2
2015-01-01 A button6
2015-01-02 A button6
2015-01-02 A button7
2015-01-02 B button1
2015-01-03 B button3
2015-01-04 B button3
-------------------------


I'd like to print emp_name along with all the distinct activity that associates with them.

Expected Output

-------------------------
emp_name activity
-------------------------
A button1
A button2
A button6
A button7
B button1
B button3
C button2
-------------------------


I've tried the following queries so far, but it lists just the first entry for each emp_name

select emp_name, activity from table_name group by emp_name;

select * from (select emp_name, activity from table_name group by emp_name) t1 group by t1.emp_name;


Another query was using distinct, but, anyway couldn't use emp_name, distinct activity.

Could I pelase request help to get this query right.

Answer

Try this

select distinct emp_name, activity from table_name;
Comments