Consider the following tables:
[Table: talks]
talkID | title | starred
-------+--------------+--------
1 | talk1-title | 1
2 | talk2-title | 1
3 | talk3-title | 0
4 | talk4-title | 0
5 | talk5-title | 0
[Table: talkspeaker]
talkID | speaker
-------+---------
1 | Speaker1
1 | Speaker2
2 | Speaker3
3 | Speaker4
3 | Speaker5
4 | Speaker6
5 | Speaker7
5 | Speaker8
[Table: similartalks]
talkID | similarTo
-------+----------
1 | 3
1 | 4
2 | 3
2 | 4
2 | 5
3 | 2
4 | 5
5 | 3
5 | 4
What I want to do is: Given the set of starred talks, I would like to select the top 2 of the unstarred talks (starred = 0) and their titles and speakers that are most similar to the set of starred talks. The problem is that getting the speakers requires using an aggregate function, and so does getting the most similar talks.
Without the speakers in the fray, I have been able to get the most similar talks using the following query:
select t2.talkID, t2.title, count(*) as count
from similarTalks s, talks t1, talks t2
where s.talkID = t1.talkID
and t1.Starred = 1
and s.similarTo = t2.TalkID
and t2.Starred = 0
group by t2.title, t2.talkID
order by count desc
limit 2
Generally, I use the following aggregate function for getting the speakers, with appropriate group by columns (assume t = talkspeaker):
group_concat(t.speaker, ', ') as Speakers
as in
select t1.title, group_concat(t2.speaker, ', ') as Speakers
from talks t1, talkspeaker t2
where t1.talkID = t2.talkID
group by t1.title
But I am not able to combine the two things together. It might matter that I am planning to run this query in a sqlite database (that is where the group_concat function comes from). The answer to the top 2 unstarred talks most similar to starred talks seem to be with talkIDs 3 and 4.
For aggregation purposes, there are the SQL aggregate functions. And for multi-level aggregation, you'd use (at least) two aggregate functions at the same time. If you're interested in quality reporting, you'll need much more than SQL's aggregate functions. However, they're certainly the basis of good reporting.
This is because GROUP BY will only return unique results per group and the SELECT list can only consist aggregate functions or columns that are part of the GROUP BY clause. So depending on what you want to get, you can use different functions to get the optimal output.
Of course it is not possible to combine two different values resulting from two aggregate functions only in a single column.
Firstly you might want to read this article about reasons to use ANSI 92 Joins instead of the aged ANSI 89 as used above. Secondly, SQLLite does support the GROUP_CONCAT function so you can use this.
You just neeed to add your second query as subquery into the first to get the desired result:
SELECT Talks.TalkID,
Talks.Title,
ts.Speakers,
COUNT(*) AS SimilarTalks
FROM Talks
INNER JOIN SimilarTalks
ON Talks.TalkID = SimilarTalks.SimilarTo
INNER JOIN Talks t2
ON SimilarTalks.TalkID = t2.TalkID
AND t2.Starred = 1
INNER JOIN
( SELECT TalkID, GROUP_CONCAT(Speaker, ',') AS Speakers
FROM TalkSpeaker
GROUP BY TalkID
) ts
ON ts.TalkID = Talks.TalkID
WHERE Talks.Starred = 0
GROUP BY Talks.TalkID, Talks.Title, ts.Speakers
ORDER BY COUNT(*) DESC
LIMIT 2;
Example on SQL Fiddle
EDIT
You could also do this without a subquery using DISTINCT
:
SELECT Talks.TalkID,
Talks.Title,
GROUP_CONCAT(DISTINCT ts.Speaker) AS Speakers,
COUNT(DISTINCT t2.TalkID) AS SimilarTalks
FROM Talks
INNER JOIN SimilarTalks
ON Talks.TalkID = SimilarTalks.SimilarTo
INNER JOIN Talks t2
ON SimilarTalks.TalkID = t2.TalkID
AND t2.Starred = 1
INNER JOIN TalkSpeaker ts
ON ts.TalkID = Talks.TalkID
WHERE Talks.Starred = 0
GROUP BY Talks.TalkID, Talks.Title
ORDER BY COUNT(DISTINCT t2.TalkID) DESC
LIMIT 2;
However I see no benefit at all in this method, and it is likely to be less efficient (I have not tested so can't be certain)
First, to get just the IDs of the desired talks, remove the other fields from your first query:
SELECT unstarred.talkID
FROM talks AS starred
JOIN similarTalks AS s ON starred.talkID = s.talkID
JOIN talks AS unstarred ON s.similarTo = unstarred.talkID
WHERE starred.starred
AND NOT unstarred.starred
GROUP BY unstarred.talkID
ORDER BY COUNT(*) DESC
LIMIT 2
Then, use this as a subquery to get the information about the desired talks:
SELECT t.title AS Title,
group_concat(s.speaker, ', ') AS Speakers
FROM talks AS t JOIN talkspeaker AS s ON t.talkID = s.talkID
WHERE t.talkID IN (SELECT unstarred.talkID
FROM talks AS starred
JOIN similarTalks AS s ON starred.talkID = s.talkID
JOIN talks AS unstarred ON s.similarTo = unstarred.talkID
WHERE starred.starred
AND NOT unstarred.starred
GROUP BY unstarred.talkID
ORDER BY COUNT(*) DESC
LIMIT 2)
GROUP BY t.talkID
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With