As the title suggests, I'd like to select the first row of each set of rows grouped with a GROUP BY.
Specifically, if I've got a user_analytics
table that looks like this:
SELECT latitude, longitude, country_name, code, COUNT(*) as total_visitors FROM referral_analytics
WHERE created_at BETWEEN '2020-01-03'AND '2021-01-03'
GROUP BY code,country_name,latitude,longitude
My Output:
latitude | longitude | country_name | code| total_visitors
----------+------------+---------------------------+-----+---------------
32.548328 | -92.045235 | United States of America | US | 1
33.389011 | -111.844017 | United States of America | US | 1
34.060734 | -118.239738 | United States of America | US | 1
38.935699 | -77.3508 | United States of America | US | 1
39.613087 | -104.883926 | United States of America | US | 1
39.751099 | -104.997101 | United States of America | US | 1
39.772247 | -86.156517 | United States of America | US | 1
39.966381 | -83.012772 | United States of America | US | 4
I'd like to query the first or min latitude and longitude for each group in MySQL/ Laravel query. Something like this:
Expected Output:
latitude | longitude | country_name | code| total_visitors
----------+------------+---------------------------+-----+---------------
32.548328 | -92.045235 | United States of America | US | 11