I have table of regions:
table region:
id | title
Region has many adverts:
table advert:
id | region_id | ...
Then advert has many uses (many-many through table adv_use):
table use:
use | slug | ...
----------------
1 | slug_1 | ...
2 | slug_2 | ...
..................
table adv_use:
adv_id | use_id
I want select all regions with count(*) of adverts, which have uses with slug_1 AND slug_2. If advert has no use with slug_1 or with slug_2 (or both), it's must not be counted.
What i have now:
SELECT COUNT(DISTINCT advert.id) as count
FROM region
JOIN advert ON region.id = advert.region_id
JOIN adv_use ON advert.id = adv_use.adv_id
JOIN use ON adv_use.use_id = use.id
WHERE use.slug IN ('slug_1', 'slug_2')
GROUP BY region.id
HAVING COUNT(DISTINCT adv_use.use_id) = 2
But it's working not as i want.
SQLFiddle: http://sqlfiddle.com/#!2/5b4d4/1
Thanks for help and sorry for bad english.
You need to use a subquery to select the adv_id that use both slugs:
SELECT COUNT(DISTINCT advert.id) as count, region.id as reg_id
FROM region
JOIN advert ON region.id = advert.region_id
JOIN (SELECT adv_use.adv_id
FROM adv_use
JOIN tbl_use ON adv_use.use_id = tbl_use.id
WHERE tbl_use.slug IN ('slug1', 'slug2')
GROUP BY adv_use.adv_id
HAVING COUNT(DISTINCT adv_use.use_id) = 2) adv_use
ON advert.id = adv_use.adv_id
GROUP BY region.id
;
SQLFIDDLE
Related
Problem statement link
Correct code (by dongyuzhang):
select con.contest_id,
con.hacker_id,
con.name,
sum(total_submissions),
sum(total_accepted_submissions),
sum(total_views), sum(total_unique_views)
from contests con
join colleges col on con.contest_id = col.contest_id
join challenges cha on col.college_id = cha.college_id
left join
(select challenge_id, sum(total_views) as total_views, sum(total_unique_views) as total_unique_views
from view_stats group by challenge_id) vs on cha.challenge_id = vs.challenge_id
left join
(select challenge_id, sum(total_submissions) as total_submissions, sum(total_accepted_submissions) as total_accepted_submissions from submission_stats group by challenge_id) ss on cha.challenge_id = ss.challenge_id
group by con.contest_id, con.hacker_id, con.name
having sum(total_submissions)!=0 or
sum(total_accepted_submissions)!=0 or
sum(total_views)!=0 or
sum(total_unique_views)!=0
order by contest_id;
My changed code without sub-queries which is incorrect and giving larger values of sums. I don't understand how writing sub-queries is making the difference ? A simple example test case would be very helpful. THANKS !
select con.contest_id,
con.hacker_id,
con.name,
sum(total_submissions),
sum(total_accepted_submissions),
sum(total_views), sum(total_unique_views)
from contests con
join colleges col on con.contest_id = col.contest_id
join challenges cha on col.college_id = cha.college_id
left join view_stats vs
on cha.challenge_id = vs.challenge_id
left join submission_stats ss
on cha.challenge_id = ss.challenge_id
group by con.contest_id, con.hacker_id, con.name
having sum(total_submissions)!=0 or
sum(total_accepted_submissions)!=0 or
sum(total_views)!=0 or
sum(total_unique_views)!=0
order by contest_id;
In general with the subqueries first you make the aggregation before the join, so the values are right, since you have only one row per chalange_id respective contest_id and hacker id with the right sum.
If you join them together first, the values are summed up once for every matching row in the main-query.
Table1:
id | value1
a | 1
a | 2
b | 3
Table2:
id | value2
a | 5
a | 6
If you join without subqueries you got(before grouping)
a | 1 | 5
a | 1 | 6
a | 2 | 5
a | 2 | 6
So surely the sums are wrong.
select Table1.id , sum(value1), sum(value2) from
Table1 join Table2 on Table1.id = Table2.id
would return
a | 6 | 22
but
select Table1.id , sum(value1), max(sum2) from
Table1 join (select sum(value2) as sum2 from Table2 group by id) t2 on Table1.id = Table2.id
would return
a | 3 | 11
I don't know if this is the case in your query, but this is the main difference of using subqueries
I have this example of my problem:
http://sqlfiddle.com/#!9/0cc41/1/0
Description:
There are three tables (events, persons and user). The table persons connects events and user. persons.type_id is the events.id and persons.user_id is user.id. I created two events (id 1 and 2). There are one entry in person for each event.
My Sql:
SELECT events.*,
coalesce(part_person.Accept_Participants_LJ, 0) AS Accept_Participants
FROM events
LEFT JOIN (
SELECT GROUP_CONCAT(CONCAT(part_user.forname, ' ', part_user.surname) SEPARATOR ', ') AS Accept_Participants_LJ,
part_person.type_id
FROM persons AS part_person
LEFT JOIN user AS part_user ON part_user.id = part_person.user_id
WHERE part_person.type = 'event_participant'
) part_person ON events.id = part_person.type_id
GROUP BY events.id
My expectation was:
------------------------
|id|Accept_Participants|
------------------------
|1 | Carl Habicht |
------------------------
|2 | Peter Zwegert |
------------------------
As you can see, the result is:
----------------------------------
|id| Accept_Participants |
----------------------------------
|1 | Carl Habicht, Peter Zwegert|
----------------------------------
|2 | 0 |
----------------------------------
It seems, that he ignores the ON-Condition of the Left Join.
But, where is my mistake?
Group_concat should (almost) always have a group by and group by without any aggregation is not useful
Maybe this is what you need
SELECT events.*,
coalesce(part_person.Accept_Participants_LJ, 0) AS Accept_Participants
FROM events
LEFT JOIN (
SELECT GROUP_CONCAT(CONCAT(part_user.forname, ' ', part_user.surname) SEPARATOR ', ') AS Accept_Participants_LJ,
part_person.type_id
FROM persons AS part_person
LEFT JOIN user AS part_user ON part_user.id = part_person.user_id
WHERE part_person.type = 'event_participant'
group by part_person.type_id
) part_person ON events.id = part_person.type_id
order BY events.id
I have two table:
table POI:
NAME | VOTE
Paris | rt_1
Milan | rt_2
Rome | rt_3
... | ...
table rtgitems:
ITEM | TOTALRATE
rt_1 | 22
rt_2 | 3
rt_3 | 3
rt_4 | 5
... | ...
I want the attribute NAME from first table with minimum value in TOTALRATE from second table. Example: Milan, Rome.
I use this query:
SELECT POI.Name FROM POI INNER JOIN rtgitems ON POI.Vote=rtgitems.item WHERE POI.Vote = (SELECT MIN(rtgitems.totalrate) FROM rtgitems)
but don't work, I have empty result.
How must I do?
Thanks.
When ever you are using min or max kind or function in your sql you should use group by clause to get the real output from the table.
SELECT POI.Name FROM POI INNER JOIN rtgitems ON POI.Vote=rtgitems.item where totalrate= (select min(totalrate) from rtgitems)
GROUP BY POI.Name
hope it helps.
SELECT POI.Name, min(totalrate) FROM POI INNER JOIN rtgitems ON POI.Vote=rtgitems.item
GROUP BY POI.Name
try SELECT POI.name FROM POI join rtgitems ON POI.vote=rtgitems.item where totalrate<=(SELECT totalrate from rtgitems order by totalrate desc limit 1)
I am having trouble calculating students grades together to get their final grade.
I have the following tables
Students
----------------
stu_id
stu_fname
stu_lname
Grades
----------------
grade_id
grade_name
grade_type
grade_possible
StudentGrades
-----------------
stu_grade_id
grade_id
stu_id
grade_earned
GradeTypes
----------------
grade_type
grade_type_name
grade_weight
This is the query that I have been able to come up with
Select S.stu_fname, S.stu_lname, GT.grade_type_name,
(ROUND((SUM(SG.grade_earned)/SUM(G.grade_possible)), 2) * ROUND((GT.grade_weight/100.0)
, 2) ) as CalculatedGrade
FROM Student S
INNER JOIN StudentGrade SG on SG.stu_id = S.stu_id
INNER JOIN Grade G on SG.grade_id = G.grade_id
INNER JOIN GradeType GT WHERE G.grade_type = GT.grade_type
GROUP BY S.stu_fname, S.stu_lname, GT.grade_type_name;
I get the query report below
James | Fort | HW/QUIZ | 30.0
James | Fort | LogBook | 60.0
Robin | Hood | HW/QUIZ | 60.0
Robin | Hood | Logbook | 25.0
I want to be able to add both of James Forts grades together to get his final grade and the same for Robin Hood.
Any help is appreciated, I am stuck at this point. I am almost done. I have researched sub queries and need more help to narrow my search to get the answer.
Have you tried the following ?
SELECT results.stu_fname, results.stu_lname, sum(results.CalculatedGrade)
FROM(
SELECT S.stu_fname, S.stu_lname, GT.grade_type_name,
(ROUND((SUM(SG.grade_earned)/SUM(G.grade_possible)), 2) * ROUND((GT.grade_weight/100.0)
, 2) ) as CalculatedGrade
FROM Student S
INNER JOIN StudentGrade SG on SG.stu_id = S.stu_id
INNER JOIN Grade G on SG.grade_id = G.grade_id
INNER JOIN GradeType GT WHERE G.grade_type = GT.grade_type
GROUP BY S.stu_fname, S.stu_lname, GT.grade_type_name
)results
GROUP BY results.stu_fname, results.stu_lname;
Edit: added aliases thanks to AshReva's remark.
Well, just remove GT.grade_type_name from the select and group by. Does this do what you need?
Select S.stu_fname, S.stu_lname,
(ROUND((SUM(SG.grade_earned)/SUM(G.grade_possible)), 2) * ROUND((GT.grade_weight/100.0)
, 2) ) as CalculatedGrade
FROM Student S INNER JOIN
StudentGrade SG
on SG.stu_id = S.stu_id INNER JOIN
Grade G
on SG.grade_id = G.grade_id INNER JOIN
GradeType GT
on G.grade_type = GT.grade_type
GROUP BY S.stu_fname, S.stu_lname;
I've got these three tables in the DB & I want to select the event_name for a specific userID from t1event given that I know the value of ID from t1user. How can I do this in a single select statement. (I am using mysql).
**t1user**
+----+
| ID |
+----+
**t2userEvent**
+---------+----------+
| userID | eventID |
+---------+----------+
**t1event**
+----------+--------------+
| eventID | event_name |
+----------+--------------+
Use join:
SELECT t1user.ID, t1event.event_name
FROM t1user
JOIN t2userEvent ON t1user.ID = t2userEvent.userID
JOIN t1event ON t1event.eventID = t2userEvent.eventID
WHERE t1user.ID = :user_id
If you want the users who doesn't have events be listed too, then use LEFT JOIN instead.
You could try this:
SELECT A.event_name FROM t1event A INNER JOIN t2userEvent B
ON A.eventID = B.eventID WHERE b.userID = ?
If I understand it correctly you have the userID as parameter?