MySQL Query grouping and counting results - mysql

here's my current query - it shows cities and a distance value:
SELECT city, ( 6371 * acos( cos( radians(51.228482) ) * cos( radians( geo_breit
) ) * cos( radians( geo_lang ) - radians(6.772457) ) + sin( radians(51.228482) ) * sin( radians( geo_breit
) ) ) )
AS distance
FROM partner HAVING distance < 50
ORDER BY city
which outputs:
i'm totally lost in grouping the data so it would return the following
city total max_distance
-------------------------------------------------------------
Bedburg 2 29.445788818447983
Bergheim 2 32.83341311371813
Bergisch Gladbach 4 39.62206615477636
Bochum 1 43.15766028349356
...
EDIT: i added the actual query, the problem is that grouping/max doesn't work when using the HAVING keyword .. any ideas?

Either you use a nested query like this:
SELECT cities.city,
count(*) as total,
max(cities.distance) as max_distance
FROM (SELECT city,
( 6371 * acos( cos( radians(51.228482) ) * cos( radians( geo_breit ) ) * cos( radians( geo_lang ) - radians(6.772457) ) + sin( radians(51.228482) ) * sin( radians( geo_breit) ) ) ) AS distance
FROM partner HAVING distance < 50
ORDER BY city) as cities
GROUP BY cities.city
Or you could try it with a where clause instead of a having
SELECT city, count(*),
max( 6371 * acos( cos( radians(51.228482) ) * cos( radians( geo_breit ) ) * cos( radians( geo_lang ) - radians(6.772457) ) + sin( radians(51.228482) ) * sin( radians( geo_breit) ) ) ) AS max_distance
FROM partner
WHERE (6371 * acos( cos( radians(51.228482) ) * cos( radians( geo_breit ) ) * cos( radians( geo_lang ) - radians(6.772457) ) + sin( radians(51.228482) ) * sin( radians( geo_breit) ) ) < 50
ORDER BY city
GROUP BY city

SELECT city, COUNT(city) as total, MAX(distance) AS max_distance
FROM table
GROUP BY city

You can use COUNT function count the number of occurrences and MAX function to find the maximum distance value with GROUP BY
Query
select city, count(city) as total, max(distance) as max_distance
from your_table_name
group by city;

use GROUP BY
SELECT city, COUNT(city) total, MAX(distance) max_distance
FROM table
GROUP BY city
The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

Related

MySQL - Using COUNT() to return total results when HAVING is specified

Using the query below, I can search for properties within a given radius and results are returned.
SELECT id, address, ( 3959 * acos( cos( radians( 53.184815 ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-3.025741) ) + sin( radians(53.184815) ) * sin( radians( lat ) ) ) ) AS distance
FROM properties
WHERE area = 1 HAVING distance <= 1
ORDER BY price DESC, distance ASC
LIMIT 0, 10
However I now want to add pagination, thus the "LIMIT 0, 10" but somehow have the query return the total results. For example, if there are 100 results but we're only limiting to the first 10 results, return the total as 100.
I tried adding "COUNT(*) AS total" after the select but this caused zero results to be returned.
How do I have the query return the total in this way?
I think it will need a subquery to achieve that:
SELECT
id, address, ( 3959 * acos( cos( radians( 53.184815 ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-3.025741) ) + sin( radians(53.184815) ) * sin( radians( lat ) ) ) ) AS distance,
(SELECT count(*) FROM properties WHERE area = 1 HAVING ( 3959 * acos( cos( radians( 53.184815 ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-3.025741) ) + sin( radians(53.184815) ) * sin( radians( lat ) ) ) )<= 1) AS total
FROM properties
WHERE area = 1 HAVING distance <= 1
ORDER BY price DESC, distance ASC
LIMIT 0, 10
You either have to use a separate query without limit with count(*) or as splash indicated, use SQL_CALC_FOUND_ROWS in your query and then issue a SELECT FOUND_ROWS(); to get the total number.
You can try to inject the count(*) query as a subquery in your main query, but to me that's only unnecessary complication of your query.

combine 2 select queries in mysql

I have 2 select statements:
timestamp of emp getting awards for specific emp id
SELECT * FROM user_table,employeetable,awards where user_table.empid=employeetable.empid AND user_table.empid=awards.empid AND user_table.empid=123 ORDER BY timestamp DESC
All employees staying around 25 miles from the current loc:current location: lat =37 lng=-122
SELECT * ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) )+ sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM user_table,employeetable,awards where user_table.empid=employeetable.empid AND user_table.empid=awards.empid HAVING distance < 25 ORDER BY distance;
How do I combine both and ORDER BY timestamp ?btw both have field timestamp.
1.has specific user
2.all users within specific radius
I really appreciate any help.Thanks in Advance.
You can combine the two queries into a single query, just using logic in the where clause (which this has turned into a having clause:
select *, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) )+ sin( radians(37) ) * sin( radians( lat ) ) ) ) as distance
from user u join
employee e
on u.empid = e.empid join
awards a
on u.empid = a.empid
having empid = 123 or distance < 25;
This uses having instead of where so the distance column alias can be used instead of the formula.

SQL with Multiple Where Clauses

First -- I have read about 7 pages of posts with similar titles but couldn't find the right insight for my challenge
My SQL:
SELECT name, address, lat, lng, city, state, phone, zip, info
, ( 3959 * acos( cos( radians('37.4969') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('-122.2674') ) + sin( radians('37.4969') ) * sin( radians( lat ) ) ) ) AS distance
FROM myhealthfinder_map
HAVING distance < '50' and location = '2'
ORDER BY distance LIMIT 0 , 10
I get the error message: Invalid query: Unknown column 'location' in 'having clause'
if instead of HAVING I just make it WHERE location = '2' then it works fine [it finds the column] (but I need the distance selector).
Any suggestion on how to knock this down?
Use both WHERE and HAVING. HAVING is used for aggregated and calculated columns. And WHERE on plain old columns.
SELECT name, address, lat, lng, city, state, phone, zip, info
, ( 3959 * acos( cos( radians('37.4969') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('-122.2674') ) + sin( radians('37.4969') ) * sin( radians( lat ) ) ) ) AS distance
FROM myhealthfinder_map
WHERE location = '2'
HAVING distance < '50'
ORDER BY distance LIMIT 0 , 10
More explanation found here WHERE vs HAVING
Don't use HAVING without GROUP BY. You can try this instead
SELECT name, address, lat, lng, city, state, phone, zip, info, ( 3959 * acos( cos( radians('37.4969') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('-122.2674') ) + sin( radians('37.4969') ) * sin( radians( lat ) ) ) ) AS distance
FROM myhealthfinder_map
WHERE location = '2' AND
( 3959 * acos( cos( radians('37.4969') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('-122.2674') ) + sin( radians('37.4969') ) * sin( radians( lat ) ) ) ) < 50
ORDER BY distance LIMIT 0 , 10
It's not pretty, but it should work.

MySQL: Combining two queries into one row result

First part of the query:
SET #centerLat = '48.531157';
SET #centerLng = '-123.782959';
SELECT user_id, lat, lng, ( 3959 * acos( cos( radians( #centerLat ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(#centerLng) ) + sin( radians( #centerLat ) ) * sin( radians( lat ) ) ) ) AS distance FROM bid_userloc HAVING distance < 25 ORDER BY distance LIMIT 0 , 20
Second aspect is taking the user_id and grabbing a bunch of information from the USERS table
I'm still learning what JOIN even means and I don't quite understand how it all works best...
you may try this
SELECT user_id, lat, lng, ( 3959 * acos( cos( radians( #centerLat ) )
* cos( radians( lat ) ) * cos( radians( lng ) - radians(#centerLng) )
+ sin( radians( #centerLat ) ) * sin( radians( lat ) ) ) )
AS distance,columnsfromuserstable FROM bid_userloc bid
inner join users us on bid.user_id=us.user_id
HAVING distance < 25
ORDER BY distance LIMIT 0 , 20
You can try something like this:
select * from users where user_id in (select user_id from(
SELECT user_id, lat, lng, ( 3959 * acos( cos( radians( #centerLat ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(#centerLng) ) + sin( radians( #centerLat ) ) * sin( radians( lat ) ) ) ) AS distance FROM bid_userloc HAVING distance < 25 ORDER BY distance LIMIT 0 , 20
));
Variant with JOIN -
SET #centerLat = '48.531157';
SET #centerLng = '-123.782959';
SELECT
t1.user_id, t1.lat, t1.lng,
(3959 * ACOS(COS(RADIANS(#centerLat)) * COS(RADIANS(t1.lat)) * COS(RADIANS(t1.lng) - RADIANS(#centerLng)) + SIN(RADIANS(#centerLat)) * SIN(RADIANS(t1.lat)))) distance,
t2.*
FROM
bid_userloc t1
JOIN users t2
ON t1.user_id = t2.user_id
HAVING
distance < 25
ORDER BY
distance
LIMIT
0, 20;

MySQL - Ordered GROUP BY Issue Optimization

I have a table called stores which contains store information and more importantly location coordinates of stores of various merchants. A merchant can have multiple stores at various locations. I need a listing of stores ordered by distance (from a particular location) with only the closest store of each merchant showing in the list.
The following query works but I believe its sub-optimal. Is there a way to revise or improve this query?
SELECT
`Store`.`id`,
`Merchants`.`id`,
`Store`.`area_id`,
`Store`.`url`,
( 6371 * acos( cos( radians(18.973212066666665) ) * cos( radians( Store.latitude ) ) * cos( radians( Store.longitude ) - radians(72.8140959) ) + sin( radians(18.973212066666665) ) * sin( radians( Store.latitude ) ) ) ) AS distance
FROM
`stores` AS `Store`
INNER JOIN
(SELECT DISTINCT id,( 6371 * acos( cos( radians(18.973212066666665) ) * cos( radians( Store.latitude ) ) * cos( radians( Store.longitude ) - radians(72.8140959) ) + sin( radians(18.973212066666665) ) * sin( radians( Store.latitude ) ) ) ) AS distance FROM stores as Store WHERE Store.active=1 AND Store.parent_id=0 AND (( 6371 * acos( cos( radians(18.973212066666665) ) * cos( radians( Store.latitude ) ) * cos( radians( Store.longitude ) - radians(72.8140959) ) + sin( radians(18.973212066666665) ) * sin( radians( Store.latitude ) ) ) ) < 50) GROUP BY Store.id ORDER BY distance) AS `St` ON (`St`.`id` = `Store`.`id`)
INNER JOIN
merchants AS `Merchants` ON (`Store`.`merchant_id` = `Merchants`.`id`)
WHERE
(( 6371 * acos( cos( radians(18.973212066666665) ) * cos( radians( `Store`.`latitude` ) ) * cos( radians( `Store`.`longitude` ) - radians(72.8140959) ) + sin( radians(18.973212066666665) ) * sin( radians( `Store`.`latitude` ) ) ) ) < 50) AND
`Store`.`parent_id` = 0 AND
`Store`.`active` = 1
GROUP BY
`Merchants`.`id`
ORDER BY
`distance` ASC
LIMIT 10
I am using the following formula to calculate distance ( 6371 * acos( cos( radians(LATITUDE) ) * cos( radians( Store.latitude ) ) * cos( radians( Store.longitude ) - radians(LONGITUDE) ) + sin( radians(LATITUDE) ) * sin( radians( Store.latitude ) ) ) )
Looks like you don't need the merchant table at all. You are using it only for merchant_id which you have in the store table anyways. So you can avoid this join.
If I understand what you need correctly, you can try this query:
select id, merchant_id, area_id, url,
min(distance function you are using) as distance
from stores group by merchant id, order by distance
I did not actually run it, but the idea should work.
-Mansi