Retrieve latest time row using Group by not working - mysql

Tables :-
Table 1 :- Person_LatLong
Person_Id
Lat
Lng
Location_DateTime
Person_LatLong Data :-
10 23.030211421184454 72.55705489668806 2014-09-02 04:23:42
10 23.03029215373424 72.55710855670746 2014-09-02 04:25:09
10 23.030301667271765 72.55715279051745 2014-09-02 04:27:21
11 19.045563510415214 72.915665750359 2014-09-02 08:22:07
11 19.046567530190785 72.91524330185979 2014-09-02 08:32:36
11 19.04553070382594 72.91621148133476 2014-09-02 08:39:47
11 18.537267778519347 73.83538450865574 2014-09-03 01:44:19
11 18.33554237666039 73.85274219500492 2014-09-03 07:18:02
11 18.331919816746026 73.8525499279805 2014-09-03 07:18:59
11 18.33181875247372 73.85243149060277 2014-09-03 07:19:02
11 18.777939290860722 73.31834934181029 2014-09-04 22:07:31
11 18.790032969638293 73.30265963437363 2014-09-04 22:09:06
11 18.79108238318203 73.29928216416553 2014-09-04 22:09:22
11 18.800857529132163 73.28531940244517 2014-09-04 22:11:22
11 18.812675453346255 73.27794458217039 2014-09-04 22:13:26
11 18.82985965773455 73.25592224937081 2014-09-04 22:15:31
11 18.84531169311457 73.23344887176076 2014-09-04 22:17:49
11 18.869063931831764 73.2185512231118 2014-09-04 22:19:54
11 18.893204517796047 73.20479873759578 2014-09-04 22:22:03
11 18.910161939581506 73.18348844819505 2014-09-04 22:24:30
Table 2:- LS_For
Subject_Id -> This is basically Person_Id but I have changed the name.
Watcher_Id
Assistance_Group_Id
LS_For Table Data :-
1 10 1
1 11 1
1 17 1
Query:-
SELECT *,( 6371 * acos( cos( radians(23.030) )
* cos( radians(Lat) ) * cos( radians(Lng) - radians(72.5570) )
+ sin( radians(23.030) ) * sin( radians(Lat) ) ) )
AS distance FROM Person_LatLong WHERE Person_Id IN
(SELECT Watcher_Id FROM LS_For WHERE Subject_Id = 1 AND Assistance_Group_Id = 1)
HAVING distance < 5 ORDER BY Location_DateTime DESC;
Gives Below as Output :- ID | LAT | LNG | DATE TIME | DISTANCE
10 23.030301667271765 72.55715279051745 2014-09-02 04:27:21 0.037008818510632306
10 23.03029215373424 72.55710855670746 2014-09-02 04:25:09 0.03433299317128307
10 23.030211421184454 72.55705489668806 2014-09-02 04:23:42 0.02417068347133403
11 23.030301667271765 72.55715279051745 2014-09-02 05:27:21 0.037008818510632306
11 23.03029215373424 72.55710855670746 2014-09-02 05:25:09 0.03433299317128307
11 23.030211421184454 72.55705489668806 2014-09-02 05:23:42 0.02417068347133403
After Adding GROUP BY :-
SELECT *,( 6371 * acos( cos( radians(23.030) )
* cos( radians(Lat) ) * cos( radians(Lng) - radians(72.5570) )
+ sin( radians(23.030) ) * sin( radians(Lat) ) ) )
AS distance FROM Person_LatLong WHERE Person_Id IN
(SELECT Watcher_Id FROM LS_For WHERE Subject_Id = 1 AND Assistance_Group_Id = 1)
GROUP BY Person_Id HAVING distance < 5 ORDER BY Location_DateTime DESC;
Gives me below Output :-
10 23.030211421184454 72.55705489668806 2014-09-02 04:23:42 0.02417068347133403
But I want to get latest time row not the oldest.
OUTPUT Required :- Working with 2 tables, LS_For is table where I am finding the Ids for which I have to look for the location distance and based on that need to filter the result based on distance.
10 23.030301667271765 72.55715279051745 2014-09-02 04:27:21 0.037008818510632306
11 23.030301667271765 72.55715279051745 2014-09-02 05:27:21 0.037008818510632306

Generate a set of data containing the max date time (most recent) for each person and then INNER JOIN to it so that your base set islimited to only the most recent entry in person_LatLong.
(Select person_ID, max(`Location_DateTime`) mldt FROM person_LatLong group by Person_ID)
generates the most recent latLong for each user... So then...
POSSIBLE 1
SELECT a.Person_ID, a.LAT, a.LNG, a.`Location_DateTime`,
( 6371 * acos( cos( radians(23.030) )
* cos( radians(a.Lat) ) * cos( radians(a.Lng) - radians(72.5570) )
+ sin( radians(23.030) ) * sin( radians(a.Lat) ) ) ) AS Distance
FROM Person_LatLong a
INNER JOIN (SELECT Person_ID, max(`Location_DateTime`) as mldt
FROM Person_latLong
GROUP BY Person_ID) P
on P.Person_ID = a.Person_Id
and P.mldt = a.`Location_DateTime`
LEFT JOIN LS_FOR C
on a.Person_Id = C.Watcher_ID
AND C.Subject_Id = 1
AND C.Assistance_Group_Id = 1
GROUP BY a.Person_ID, a.LAT, a.LNG, a.`Location_DateTime`
HAVING Distance < 5;
POSSIBLE 2
SELECT a.Person_ID, a.LAT, a.LNG, a.`Location_DateTime`,
( 6371 * acos( cos( radians(23.030) )
* cos( radians(a.Lat) ) * cos( radians(a.Lng) - radians(72.5570) )
+ sin( radians(23.030) ) * sin( radians(a.Lat) ) ) ) AS Distance
FROM Person_LatLong a
INNER JOIN (SELECT Person_ID, max(`Location_DateTime`) as mldt
FROM Person_latLong
GROUP BY Person_ID) P
on P.Person_ID = a.Person_Id
and P.mldt = a.`Location_DateTime`
INNER JOIN LS_FOR C
on a.Person_Id = C.Watcher_ID
WHERE C.Subject_Id = 1
AND C.Assistance_Group_Id = 1
GROUP BY a.Person_ID, a.LAT, a.LNG, a.`Location_DateTime`
HAVING Distance < 5;
I've amend the response to treat LS_FOR as a left join incase there are not records for each user in LS_FOR, and if not still return the person even though they wouldn't have a subject_ID or assistance_groupID....
In other words, The above should return in plain english:
The most recent personLatLong will be returned for each unique Person_ID; provided the distance is < 5 and if there is an entry in LS_FOR and that PersonLatLong entry, only records with a subjectID of 1 and asstianceGorupID of 1 will be returned. If there is no entry in LS_FOR it will still return the person (Perhaps you do want it as an inner join...)

Instead of
GROUP BY Person_I
Use
LIMIT 1
OR
GROUP BY Person_Id HAVING distance < 5 ORDER BY Location_DateTime DESC;
to
GROUP BY Person_Id HAVING distance < 5 ORDER BY Location_DateTime ASC;

Check this link.
Retrieving the last record in each group
Here the solution is much faster than sub query.
select person1.* from Person_LatLong person1
LEFT JOIN Person_LatLong person2 on person1.id = person2.id and
person1.dateTiem < person2.dateTime
where person2.id IS NULL;
This will give you latest record.

try doing this:
SELECT
*,( 6371 * acos( cos( radians(23.030) ) * cos( radians(Lat) ) * cos( radians(Lng) - radians(72.5570) ) + sin( radians(23.030) ) * sin( radians(Lat) ) ) ) AS distance
FROM
(select * from Person_LatLong ORDER BY Location_DateTime DESC) as t
WHERE
Person_Id IN (SELECT Watcher_Id FROM LS_For WHERE Subject_Id = 1 AND Assistance_Group_Id = 1)
GROUP BY
Person_Id
HAVING
distance < 5 ;

Related

Query to get nearest distance record

I have following mysql query:
SELECT a.id
, ( 3959 * acos( cos( radians(a.latitude) ) * cos( radians( w.latitude ) )
* cos( radians( w.longitude ) - radians(-a.longitude) )
+ sin( radians(a.latitude) ) * sin(radians(w.latitude)) ) ) distance
FROM global_restaurants a
JOIN webgeocities w
ON w.name = a.locality
AND w.country_code = a.country
AND a.latitude LIKE w.latitude
JOIN states s
ON s.state_code = w.state_code
AND w.country_code = s.country_code
WHERE a.city_id = 0
After execution it returns:
id distance
70 6665.478678743614
70 6496.46971480875
70 6725.900646648246
70 6733.5156930808
90 6969.449661399672
90 7252.889875588891
I want it to only return 2 rows with minimum distance like:
id distance
70 6496.46971480875
90 6969.449661399672
add order by at the end of your query:
order by distance ASC LIMIT 2;
now you calculating the distance but
at end of the query you must sort them and limit the returns row count..
.
NOTE:
i think you have a small mistake on your query
the 3959 is for miles but i think the values are in meter
your full query can be like this:
select a.id, ( 6371000 * acos( cos( radians(a.latitude) ) * cos( radians( w.latitude ) )
* cos( radians( w.longitude ) - radians(-a.longitude) ) + sin( radians(a.latitude) ) * sin(radians(w.latitude)) ) ) AS distance
from `global_restaurants` as a INNER JOIN webgeocities as w ON (w.name = a.locality AND w.country_code = a.country and a.latitude like w.latitude) INNER JOIN
states AS s ON (s.state_code = w.state_code and w.country_code = s.country_code) where a.city_id = '0' ORDER BY distance ASC LIMIT 2;
For simplicity, let's assumed that you have your query in view - stack, then your query should look like this
select stack.* from stack join (
SELECT id, GROUP_CONCAT(distance ORDER BY
distance asc) grouped_distance FROM stack GROUP BY id
) group_distance on group_distance.id=stack.id
where find_in_set(stack.distance,group_distance.grouped_distance)between 1 and 2;

Muliple Order by MYSql

I have stores records in my table and I want to sort them on the basis of highest rating of store and which has nearest distance to my location.
SELECT rating,
( 3959 * acos( cos( radians(37) )
* cos( radians( lat ) )
* cos( radians( lon )
- radians(-122) )
+ sin( radians(37) )
* sin( radians( lat ) )
)
) AS distance
FROM mystores sr
order by sr.rating desc ,distance asc
It is not giving me my desired results
Table Mystores
id|rating|distance
66 5 55
55 4 56
99 3 60
I assume that you want just the closest store and you want to ignore all other with the same rank but with bigger distance.
The group by raiting allows us to get the minimal distance for each raiting.
select m1.*
from mystores m1
join (
select m.raiting,
min(m.distance) distance
from mystores m
group by m.raiting
) m2
on m2.raiting = m1.raiting and
m2.distance = m1.distance
order by m1.raiting desc, m1.distance asc

SELECT column but also add AVG column

I'm not quite sure how to expand this query so that I can also have a AVG(price_current) column ... instead of having to calculate this value within PHP once the query comes back.
SELECT
listing_subtype, bedrooms, total_baths, tot_sqft_finished, price_current, latitude, longitude, (
3959 * acos (
cos ( radians(48.639) )
* cos( radians( latitude ) )
* cos( radians( longitude ) - radians(-123.404) )
+ sin ( radians(48.639) )
* sin( radians( latitude ) )
)
) AS distance
FROM rets_property_resi
WHERE listing_subtype = 'Single Family Detached' AND
bedrooms >= 2 AND bedrooms <= 3 AND
total_baths >= 1 AND total_baths <= 2 AND
tot_sqft_finished >= 2000 AND tot_sqft_finished <= 2500
HAVING distance < 5
ORDER BY distance
LIMIT 0, 25;
I'm going to make some assumptions here:
You look for similar houses/homes in a given radius around a requested one. Each of those homes has a primary key in the table you are querying from. Let us call that primKey here.
Additionally, you want the average price of all returned homes which is limited to 0,25, and not the AVG of all records in the table that match you WHERE clause.
All returned rows will have therefore an extra field with the average price and is the same for each row.
You will have to run the same query again as a subquery. However, since you want to limit the AVG you have to run that in a subquery to calculate the correct AVG.
This is really clunky:
SELECT
rets_property_resi.listing_subtype, rets_property_resi.bedrooms, rets_property_resi.total_baths, rets_property_resi.tot_sqft_finished, rets_property_resi.price_current, rets_property_resi.latitude, rets_property_resi.longitude, (
3959 * acos (
cos ( radians(48.639) )
* cos( radians( latitude ) )
* cos( radians( longitude ) - radians(-123.404) )
+ sin ( radians(48.639) )
* sin( radians( latitude ) )
)
) AS distance, outerSubQuery.averagePrice
FROM rets_property_resi
LEFT JOIN
(
SELECT innerSubQuery.primKey AS primKey, AVG(innerSubQuery.price_current) AS averagePrice
FROM
(SELECT
primKey, price_current, (
3959 * acos (
cos ( radians(48.639) )
* cos( radians( latitude ) )
* cos( radians( longitude ) - radians(-123.404) )
+ sin ( radians(48.639) )
* sin( radians( latitude ) )
)
) AS distance
FROM rets_property_resi
WHERE listing_subtype = 'Single Family Detached' AND
bedrooms >= 2 AND bedrooms <= 3 AND
total_baths >= 1 AND total_baths <= 2 AND
tot_sqft_finished >= 2000 AND tot_sqft_finished <= 2500
HAVING distance < 5
ORDER BY distance
LIMIT 0, 25) AS innerSubQuery
GROUP BY innerSubQuery.primKey
) AS outerSubQuery ON (outerSubQuery.primKey = rets_property_resi.primKey)
WHERE listing_subtype = 'Single Family Detached' AND
bedrooms >= 2 AND bedrooms <= 3 AND
total_baths >= 1 AND total_baths <= 2 AND
tot_sqft_finished >= 2000 AND tot_sqft_finished <= 2500
HAVING distance < 5
ORDER BY distance
LIMIT 0, 25;
I bet there is a more elegant version of this though. You might be better off with a temporary table here or keep just calculating in code.

Hiding alias column in MySQL

It might be the repeated question but need help.
i am new to MySQL.
Here is my problem.
i have a query to calculate distance between latitude and longitude. based on the distance order i have to return id's.
SELECT dlo.id,
(3959 * acos(cos(radians(12.9)) * cos(radians(y(gproperty))) * cos(radians(x(gproperty)) - radians(77.5)) +sin(radians(12.9)) * sin(radians(y(gproperty))))) AS distance
FROM db1.gfeature dgf,
db2.loc dlo,
db2.cust dcu
WHERE gf.o_type = 6
AND dcu.id = 240
AND dgf.o_id = dlo.p_id HAVING distance < 20
ORDER BY distance LIMIT 10;
which returns as
+------+-----------------------+
| id | distance |
+------+-----------------------+
| 101 | 0.00025714756425665 |
| 199 | 0.10971525612556807 |
| 722 | 0.22772618588406165 |
+------+-----------------------+
But i need only id column to be displayed. I asked same-question yesterday. But now i am using three tables to get data. So confused in joining 3 tables.
Can anybody suggest me.?
I tried this way
select id from (
select
dlo.id,
( 3959 * acos( cos( radians(12.9) )
* cos( radians( y(gproperty) ) )
* cos( radians( x(gproperty) ) - radians(77.5) )
+ sin( radians(12.9) )
* sin( radians(y(gproperty) ) )
)
) AS distance
from db1.gfeature dgf
join db2.cust dcu, db2.loc dlo
on dgf.o_type = 6 and dcu.id = 10 and dgf.o_id = dlo.w_id
) t
where distance < 10
order by distance
limit 10;
but getting syntax error near "on..."
Maybe something like this. Just use subquery to achieve.
SELECT S.ID
FROM
(SELECT dlo.id,
(3959 * acos(cos(radians(12.9)) * cos(radians(y(gproperty))) * cos(radians(x(gproperty)) - radians(77.5)) +sin(radians(12.9)) * sin(radians(y(gproperty))))) AS distance
FROM db1.gfeature dgf, db2.loc dlo , db2.cust dcu
WHERE gf.o_type = 6 AND dcu.id = 240 AND dgf.o_id = dlo.p_id
HAVING distance < 20) S
ORDER BY S.distance
LIMIT 10;

Sql query combining

i have two sql queries those are following
1) SELECT a.* FROM modzzz_listing_main as a LEFT JOIN modzzz_listing_rating as b ON a.id=b.gal_id WHERE LTRIM(a.city) = 'Houston' AND a.state = 'TX' AND a.tags LIKE '%Barber Shop%' ORDER BY b.gal_rating_sum DESC LIMIT 0 ,10
2) SELECT zip_code ,( 3959 * acos( cos( radians('41.97734070') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('-70.97234344') ) + sin( radians('41.97734070') ) * sin( radians( latitude ) ) ) ) AS distance FROM city_finder WHERE latitude IS NOT NULL AND longitude IS NOT NULL HAVING distance < 20 ORDER BY distance ASC
how can i combine this two queries by the condition `
modzzz_listing_main.zip=city_finder.zip_code
` .i am totally confused..please any one help me..
to see the join easier:
select * from
(
SELECT a.* FROM modzzz_listing_main as a LEFT JOIN modzzz_listing_rating as b ON a.id=b.gal_id WHERE LTRIM(a.city) = 'Houston' AND a.state = 'TX' AND a.tags LIKE '%Barber Shop%' ORDER BY b.gal_rating_sum DESC LIMIT 0 ,10
) queryA
left join
(
SELECT zip_code ,( 3959 * acos( cos( radians('41.97734070') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('-70.97234344') ) + sin( radians('41.97734070') ) * sin( radians( latitude ) ) ) ) AS distance FROM city_finder WHERE latitude IS NOT NULL AND longitude IS NOT NULL HAVING distance < 20 ORDER BY distance ASC
) queryB
on queryA.zip=queryB.zip_code
proper formatting
SELECT *
FROM
( SELECT a.*
FROM modzzz_listing_main AS a
LEFT JOIN modzzz_listing_rating AS b ON a.id=b.gal_id
WHERE LTRIM(a.city) = 'Houston'
AND a.state = 'TX'
AND a.tags LIKE '%Barber Shop%'
ORDER BY b.gal_rating_sum DESC LIMIT 0 ,
10 ) queryA
LEFT JOIN
( SELECT zip_code ,
(3959 * acos(cos(radians('41.97734070')) * cos(radians(latitude)) * cos(radians(longitude) - radians('-70.97234344')) + sin(radians('41.97734070')) * sin(radians(latitude)))) AS distance
FROM city_finder
WHERE latitude IS NOT NULL
AND longitude IS NOT NULL HAVING distance < 20
ORDER BY distance ASC ) queryB ON queryA.zip=queryB.zip_code