I'm trying to get subjects with the highest average rating within a radius by averaging the comment rating column.
I've tried something along the lines of:
select avg(`rating`) AS `avgrate` , subject.*,
, ( 3959 * acos( cos( radians(22.28639) ) * cos( radians( lat ) ) * cos( radians( subject.long ) - radians(114.1491) ) + sin( radians(22.28639) ) * sin( radians( lat ) ) ) )
AS distance
from `subject_review`
JOIN `subject`
ON subject_review.subject_id = subject.id
group by `subject_id`
HAVING distance <= 100
order by `avgrate` DESC
LIMIT 10
This SQL works fine, I just need to add the radius filter,
select avg(`rating`) AS `avgrate` ,subject_review.id , subject.*
from `subject_review`
JOIN `subject`
ON subject_review.subject_id = subject.id
group by `subject_id`
order by `avgrate` DESC
LIMIT 10
Related
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
I have the following SQL statement:
SELECT Distinct(Venue_Name), Count(*) as `num1`
FROM venuetagview
INNER JOIN
(
SELECT Tag,COUNT(*) AS `num`
FROM usertags
WHERE UserID=4
GROUP BY Tag
ORDER BY `num` DESC
) as a
ON venuetagview.Tag=a.Tag
GROUP BY Venue_Name
ORDER BY `num1` DESC
And I am trying to add a 'distance' calculation into it, using the latitude/longitude I have in the venuetagview table. I have tried this:
SELECT
( 6371
* acos( cos( radians(51.529099) )
* cos( radians( Venue_Latitude ) )
* cos( radians( Venue_Longitude )
- radians(-0.084981) )
+ sin( radians(51.529099) )
* sin( radians( Venue_Latitude ) ) ) ) AS distance,
Distinct(Venue_Name),
Count(*) as `num1`
FROM venuetagview
INNER JOIN
(
SELECT Tag,
COUNT(*) AS `num`
FROM usertags
WHERE UserID=4
GROUP BY Tag
ORDER BY `num` DESC
) as a
ON venuetagview.Tag=a.Tag
GROUP BY Venue_Name
ORDER BY `num1` DESC
However I am getting an error using this.
Only issue I see with your posted query is
distinct is keywork and should be used like distinct column_name
SELECT Distinct (Venue_Name) should just be SELECT Venue_Name or
SELECT Distinct Venue_Name
As mentioned by Strawberry removing DISTINCT solved this:
SELECT
( 6371 * acos( cos( radians(51.529099) )
* cos( radians( Venue_Latitude ) )
* cos( radians( Venue_Longitude )
- radians(-0.084981) )
+ sin( radians(51.529099) )
* sin( radians( Venue_Latitude ) ) ) ) AS distance,
Venue_Name,
Count(*) asnum1
FROM venuetagview
INNER JOIN
(
SELECT Tag,
COUNT(*) ASnum
FROM usertags
WHERE UserID=4
GROUP BY Tag
ORDER BYnumDESC) as a
ON venuetagview.Tag=a.Tag
GROUP BY Venue_Name
ORDER BY distance ASC
I have two sql queries which when run independent produces the correct results
Query 1
SELECT id,
(6371 * acos( cos( radians(9.977364864079215) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(76.58620953448485) ) + sin( radians(9.977364864079215) ) * sin( radians( latitude ) ) ) )
AS distance
FROM geodata HAVING distance < 20
ORDER BY distance
LIMIT 0 , 20;
Query 2
SELECT DISTINCT e.* FROM schools e
WHERE (
(e.type = 'preprimary')
)
AND(
e.title LIKE '%government%'
)
LIMIT 0, 10
I want to merge the first query with the second one, so that it should return all "preprimary" type schools with title like "government" located within 20KM radius and the result needs to be ordered by the distance.
How can I merge the two queries? I tried using JOINING the geodata table on the school table. But I dont know the remaining. Sorry, if this is a silly question. I am pretty new to SQL world.
SELECT DISTINCT school.* FROM
( SELECT geodata.id,
(6371 * acos( cos( radians(9.977364864079215) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(76.58620953448485) ) + sin( radians(9.977364864079215) ) * sin( radians( latitude ) ) ) )
AS distance ,school.*
FROM geodata LEFT JOIN school on geodata.id=school.id
WHERE
(school.type = 'preprimary')
AND(
school.title LIKE '%government%'
)
AND school.id IS NOT NULL
HAVING distance < 20 )x
ORDER BY x.distance
LIMIT 0 , 10;
Try this:
SELECT *
From (
SELECT DISTINCT e.* ,
(6371 * acos( cos( radians(9.977364864079215) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(76.58620953448485) ) + sin( radians(9.977364864079215) ) * sin( radians( latitude ) ) )
) as distance
FROM schools e
LEFT JOIN geodata g ON e.id=g.id
WHERE (e.type = 'preprimary')
AND ( e.title LIKE '%government%' )
) as s
Where s.distance < 20
Order by s.distance
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
I have a big SQL query (for MySQL) that is slow. It's a union of two select statements. I have tried different things, but any slight variance gives me a different result set from the original. Any help with improving it will be greatly appreciated. Thanks. Here is the SQL:
(SELECT
CONCAT(city_name,', ',region) value,
latitude,
longitude,
id,
population,
( 3959 * acos( cos( radians($latitude) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians($longitude) ) + sin( radians($latitude) ) * sin( radians( latitude ) ) ) )
AS distance,
CASE region
WHEN '$region' THEN 1
ELSE 0
END AS region_match
FROM `cities`
$where and foo_count > 5
ORDER BY region_match desc, foo_count desc
limit 0, 11)
UNION
(SELECT
CONCAT(city_name,', ',region) value,
latitude,
longitude,
id,
population,
( 3959 * acos( cos( radians($latitude) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians($longitude) ) + sin( radians($latitude) ) * sin( radians( latitude ) ) ) )
AS distance,
CASE region
WHEN '$region' THEN 1
ELSE 0
END AS region_match
FROM `cities`
$where
ORDER BY region_match desc, population desc, distance asc
limit 0, 11)
limit 0, 11
The SQL does take some interpolated values (prefixed with the dollar sign($)).
The following might give the same result (I'm not sure about how the maximum/minimum functions are called in SQL, but you should get an idea -- you need two fields derived from foo_count which separate the items of the first part of your UNION from those of the second one and allow ordering within the first part without disturbing the order in the second part) -- of course, you later need a second query to throw the additional fields out again:
SELECT
CONCAT(city_name,', ',region) value,
latitude,
longitude,
id,
population,
( 3959 * acos( cos( radians($latitude) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians($longitude) ) + sin( radians($latitude) ) * sin( radians( latitude ) ) ) )
AS distance,
min ( 6, max (foo_count, 5)) AS group_discriminator,
max ( 6, foo_count) AS rank_for_use_in_first_group,
CASE region
WHEN '$region' THEN 1
ELSE 0
END AS region_match
FROM `cities`
$where
ORDER BY group_discriminator desc, region_match desc, rank_for_use_in_first_group desc, population desc, distance asc
limit 0, 11
EDIT: Improvements