I am facing an issue with a moderately simple-looking SELECT SQL Query.
My requirement: For One specific company, if there are multiple records with the same date, I want only the first record of that date, not all from that date.
Given table:
| User | company | joining_date | 
|---|---|---|
| Devika | 1/3/2021 | |
| Aparna | Apple | 12/9/2021 | 
| Suresh | 10/2/2022 | |
| Rajesh | Apple | 12/9/2021 | 
| Arun | 10/2/2022 | 
Expected Output:
| count | users | date | company | 
|---|---|---|---|
| 2 | Devika,Suresh | 1/3/2021,10/2/2022 | 
My Output:
| count | users | date | company | 
|---|---|---|---|
| 3 | Devika,Suresh, Arun | 1/3/2021,10/2/2022 | 
My Approach:
select 
    count(user) as count, 
    group_concat(DISTINCT user) as users, 
    group_concat(DISTINCT date) as date
    company
from employee 
GROUP by company 
having company = "Google";
As per the requirement, I grouped the records by company name. I only want the results for Google, so I added a condition for that. Now, if I use the Distinct keyword, I will not get duplicate dates, but I will still have three counts of users instead of two, and three usernames instead of two. I want to skip "Arun" from this result.
How can I achieve this logic?
 
     
    