Apologies if this has already been covered but I have checked many other questions and can't seem to get the result I want.
SQL Fiddle
http://www.sqlfiddle.com/#!9/d0a1f
I would want to return the highest level achievements grouped by the category. So the dummy data it would return the rows with 'Newbie Leveller' and 'Amateur Banker'
SELECT
achievementid,
a.title,
a.level
FROM
player_achievements p
INNER JOIN achievements a ON p.achievementid = a.id
I did try and change the tables abit (added category & level to the player_achievements, felt like this was wrong as the data is in the other table) and used this query:
SELECT
achievementid,
a.title
FROM
player_achievements p1
INNER JOIN achievements a ON p1.achievementid = a.id
WHERE
p1.level =(
SELECT
MAX(p2.level)
FROM
player_achievements p2
WHERE
p1.category = p2.category
)
AND playerid = 44
But it only returned one row
One solution is to add achievements into the subquery like this
SELECT
achievementid,
title
FROM player_achievements pa1
INNER JOIN achievements a1 ON pa1.achievementid = a1.id
WHERE
a1.level =(
SELECT MAX(a2.level)
FROM player_achievements pa2
INNER JOIN achievements a2 ON pa2.achievementid = a2.id
WHERE a1.category = a2.category and
pa2.playerid = pa1.playerid
)
AND pa1.playerid = 44
demo
Please look at this:
SELECT id, title
FROM achievements JOIN (
SELECT category, max(level) level
FROM player_achievements JOIN achievements ON achievementid = id
WHERE playerid = 44
GROUP BY category
) t USING (category, level);
Related
I have a rating system in my app. Now I'm trying to get all AVG results from the ratings. Every AVG result has a result (in text) that I need to grab from the rating_results table.
It looks like this:
select round(avg(rating_results.rating)) as ratingresult, count(*) as votes, score.question_nl,
(select result_nl from rating_results where rating_results.rating = ratingresult and rating_results.score_id = score.id) from score
inner join score_categories on score_categories.id = score.category_id
inner join rating ON score.id = rating.score_id
inner join rating_results on rating.rating_result_id = rating_results.id
inner join dog on dog.id = rating.ratable_id
where dog.breed_id = 201
group by score.question_nl
The problem I have is that I cannot use ratingresult in the subselect.
Query 1 ERROR: Reference 'ratingresult' not supported (reference to
group function)
I already tried a lot but can't figure out another way.
Could use some help here, thanks!
--EDIT
The rating result explains the rating. So if the AVG rating is 4 then in the rating_results table I can find what that rating means:
Instead of a select for column value you could use a subquery for avg in join
select t.ratingresult
, count(*) as votes
, score.question_nl
, rating_results.result_nl
FROM score
inner join score_categories on score_categories.id = score.category_id
inner join rating ON score.id = rating.score_id
inner join rating_results on rating.rating_result_id = rating_results.id
inner join dog on dog.id = rating.ratable_id
INNER JOIN (
select round(avg(rating_results.rating)) as ratingresult
, score.question_nl
from score
inner join rating ON score.id = rating.score_id
inner join rating_results on rating.rating_result_id = rating_results.id
group by score.question_nl
) t ON t.ratingresult = rating_results.rating
AND rating_results.score_id = score.id
AND score.question_nl = t.question_nl
where dog.breed_id = 201
group by score.question_nl, t.ratingresult
avoinding subquery
I have a list of technicians and their number of patients, when I click on a technician I get the list of patients and their details.
To do this, I have query that Returns the number of patients per technician and another returns the records of the patients.
SELECT *, SUM(Rcount) as Number_of_patients
FROM
(
SELECT users.users_id, users.name, patients.patients_id, count(*) as Rcount
FROM pecs
INNER JOIN users ON pecs.techniciens_id = users.users_id
INNER JOIN titles ON users.titles_id = titles.titles_id
INNER JOIN patients ON patients.patients_id = pecs.patients_id
GROUP BY users_id
UNION ALL
SELECT users.users_id, users.name, patients.patients_id, count(*) as Rcount
FROM followup
INNER JOIN users ON followup.technician_id = users.users_id
INNER JOIN titles ON users.titles_id = titles.titles_id
INNER JOIN pecs ON pecs.pecs_id = followup.pecs_id
INNER JOIN patients ON patients.patients_id = pecs.patients_id
GROUP BY users_id
)x
GROUP BY users_id ORDER BY last_name ASC
the result is:
users_id | name | Number_of_patients
40 | ABABAB | 223
that is 223 patients for technician_ID = 40
Now to view a list of the patients for this technician I have the following query:
SELECT *
FROM
(
SELECT patients.patients_id, patients.name
FROM pecs
LEFT JOIN users ON pecs.techniciens_id = users.users_id
LEFT JOIN titles ON users.titles_id = titles.titles_id
INNER JOIN patients ON patients.patients_id = pecs.patients_id
WHERE pecs.techniciens_id = 40
#GROUP BY patients_id
UNION ALL
SELECT patients.patients_id, patients.name
FROM followup
LEFT JOIN users ON followup.technician_id = users.users_id
LEFT JOIN titles ON users.titles_id = titles.titles_id
LEFT JOIN pecs ON pecs.pecs_id = followup.pecs_id
INNER JOIN patients ON patients.patients_id = pecs.patients_id
WHERE followup.technician_id = 40
#GROUP BY patients_id
)x
GROUP BY patients_id ORDER BY last_name ASC
Now, I get the same number of records (223) but there are duplicates rows of patients ...I need help on how to get the correct number of patients for each technician without duplicates.
Can anyone please help?
I am not sure I understood exactly your question. Anyway, I would try to keep things as simple as possibile. Regarding your "count" query, you could start from something like this, where I removed filed I think should not be useful for counting. This query should give you count avoiding multiple id (I use DISTINCT in the two select and UNION to remove possible duplicate between the two select). May be some join could be eliminated (but I don't know the whole structure). Please follow Tim B. suggestion about formatting and care in posting a question.
SELECT USERS_ID, COUNT(*) AS PATIENTS_COUNT
FROM (
SELECT DISTINCT USERS.USERS_ID, PATIENTS.PATIENTS_ID
FROM PECS
INNER JOIN USERS ON PECS.TECHNICIENS_ID = USERS.USERS_ID
INNER JOIN TITLES ON USERS.TITLES_ID = TITLES.TITLES_ID
INNER JOIN PATIENTS ON PATIENTS.PATIENTS_ID = PECS.PATIENTS_ID
UNION
SELECT DISTINCT USERS.USERS_ID, PATIENTS.PATIENTS_ID
FROM FOLLOWUP
INNER JOIN USERS ON FOLLOWUP.TECHNICIAN_ID = USERS.USERS_ID
INNER JOIN TITLES ON USERS.TITLES_ID = TITLES.TITLES_ID
INNER JOIN PECS ON PECS.PECS_ID = FOLLOWUP.PECS_ID
INNER JOIN PATIENTS ON PATIENTS.PATIENTS_ID = PECS.PATIENTS_ID
) A
Using this query to get the products with words that fulfill all three required word terms (lenovo, laptop, computer):
SELECT t1.id, t1.name, t1.price FROM
(SELECT p.id AS productid, name, price
FROM products p JOIN productwords pw ON p.id = pw.productid
JOIN words w ON pw.wordid = w.id WHERE word.term = 'lenovo') t1
INNER JOIN
(SELECT p.id AS productid, name, price
FROM products p JOIN productwords pw ON p.id = pw.productid
JOIN words w ON pw.wordid = w.id WHERE word.term = 'laptop') t2
INNER JOIN
(SELECT p.id AS productid, name, price
FROM products p JOIN productwords pw ON p.id = pw.productid
JOIN words w ON pw.wordid = w.id WHERE word.term = 'computer') t3
ON
t1.productid = t2.productid
AND
t1.productid = t3.productid
ORDER BY t1.name
As far as I can see, the query considers the whole words table for each term (the tables have indexes. Database is MySql).
Can the query be rewritten in a better way, so it will become faster? (the tables contain millions of rows)
For example with subsets, so the 'laptop' search only considers the rows matching 'lenovo' - and the 'computer' search only considers the rows matching first 'lenovo' and then 'laptop'.
Thanks!
You can use the HAVING clause :
SELECT p.id AS productid, name, price
FROM products p
JOIN productwords pw ON p.id = pw.productid
JOIN words w ON pw.wordid = w.id
WHERE word.term in ('lenovo','computer','laptop')
GROUP BY p.id , name, price
HAVING COUNT(DISTINCT word.term) = 3
That is if I understood the question, it looks like product -> words is 1:n relation , and if no column from the word table is selected, that should work perfectly.
This might be a quicker way of doing it:
SELECT p.id, name, price
FROM products p
where
EXISTS (select null
from productwords pw1
JOIN words w1 ON pw1.wordid = w1.id
where w1.term = 'lenovo'
and p.id = pw1.productid )
and EXISTS (select null
productwords pw2
JOIN words w2 ON pw2.wordid = w2.id
where w2.term = 'laptop'
and and p.id = pw2.productid )
and EXISTS (select null
productwords pw3 ON p.id = pw3.productid
JOIN words w3
where w3.term = 'computer'
and p.id = pw3.productid )
ORDER BY name;
I try to do an INNER JOIN depending of the result of CASE.
SELECT *
FROM album
INNER JOIN (
SELECT album_type CASE album.album_type
WHEN 1 THEN "album_int"
WHEN 2 THEN "album_ext"
END FROM album)
AS type ON type.album_id = album.id
WHERE album.id = 6
LIMIT 1;
I have see some examples on this site, but nothing work in my case.
Someone can help me?
I have 3 tables (album, album_int and album_ext).
I want to join album_int or album_ext to album.
if album.album_type = 1 I join album_int,
else if album.album_type = 2 I join album_ext.
In album_int and album_int I have a column name album_id (same unique
id in album)
You do not need any case expressions or even a where clause.
SELECT a.`id`, COALESCE(aint.`something`, aext.`something`) as something, ...
FROM ALBUM A
LEFT OUTER JOIN ALBUM_INT AS AINT ON A.ID = AINT.ALBUM_ID AND A.ALBUM_TYPE = 1
LEFT OUTER JOIN ALBUM_EXT AS AEXT ON A.ID = AEXT.ALBUM_ID AND A.ALBUM_TYPE = 2
AND A.ALBUM_TYPE = 1 as a join condition means that ALBUM_INT will ONLY join to ALBUM rows where album_type = 1
similarly:
AND A.ALBUM_TYPE = 2 as a join condition means that ALBUM_EXT will ONLY join to ALBUM rows where album_type = 2
please note that an answer previously given by Gordon Linoff uses the same join logic, I have just attempted to emphasize how it meets the described requirements.
case doesn't work on tables. You can use left join:
SELECT *
FROM album a LEFT JOIN
album_int ai
ON ai.album_id = a.id AND a.album_type = 1 LEFT JOIN
album_ext ae
ON ae.album_id = a.id AND a.album_type = 2
WHERE a.id = 6 AND a.album_type IN (1, 2)
LIMIT 1;
To get values into the same column, you then need to use coalesce().
Note: This does the right thing if there is at most one match in each table.
To Join to one of two tables conditionally, create a join column on each table and UNION them together as such:
SELECT *
FROM albums a
INNER JOIN (
SELECT 1 AS album_type, album_id, colA, colB
FROM album_int
UNION ALL
SELECT 2 AS album_type, album_id, colA, colB
FROM album_ext ) AS b
ON a.album_type = b.album_type
AND a.album_id = b.album_id
I want to select a "site" record(s) randomly, and then get the related "channels" for it. Here's what I've tried that doesn't work. Please help. Thank you!
SELECT A.*, B.*
FROM (
SELECT companies.company_name, sites.id
FROM sites
INNER JOIN company_sites ON company_sites.site_id = sites.id
INNER JOIN companies ON companies.id = company_sites.company_id
WHERE sites.active = 1
AND sites.stage_id = 5
GROUP BY sites.id
ORDER BY RAND()
LIMIT 1
)A
JOIN (
SELECT
channels.id
FROM channels
WHERE channels.site_id = A.sites_id
) B ON 1 = 1
Try this:
SELECT just the fields you need
FROM (
SELECT companies.company_name, sites.id id
FROM sites
INNER JOIN company_sites ON company_sites.site_id = sites.id
INNER JOIN companies ON companies.id = company_sites.company_id
WHERE sites.active = 1
AND sites.stage_id = 5
GROUP BY sites.id
ORDER BY RAND()
LIMIT 1
)A
join channels on channels.site_id = id
By the way, if you just wanted the list of channels on each site, you can concatenate them into one field (rather than getting them on separate rows). The query would be simpler:
SELECT companies.company_name, sites.id, group_concat(channels.id) as channels
FROM sites
INNER JOIN company_sites ON company_sites.site_id = sites.id
INNER JOIN companies ON companies.id = company_sites.company_id
inner join channels on channels.site_id = sites.id
WHERE sites.active = 1
AND sites.stage_id = 5
GROUP BY sites.id
ORDER BY RAND()
LIMIT 1