This is my table
+-----+-----+-----+-------+-------+---------+
| pa | pl | rp | corp | year | rating |
|-----+-----+-----+-------+-------+---------|
| pa1 | pl1 | rp1 | a1 | 2016 | 6 |
| pa1 | pl1 | rp1 | a1 | 2017 | 7 |
| pa1 | pl1 | rp1 | a2 | 2016 | 6.5 |
| pa1 | pl1 | rp1 | a2 | 2017 | 7.5 |
| pa1 | pl1 | rp2 | a1 | 2016 | 2 |
| pa1 | pl1 | rp2 | a1 | 2017 | 1.5 |
| pa1 | pl1 | rp2 | a2 | 2016 | 4 |
| pa1 | pl1 | rp2 | a2 | 2017 | 4.5 |
+-------------------------------------------+
Its name is list.
The query I wrote is to find where the average values have increased with time.
This is my query.
select count(sq.rp)
from
(select l1.pa, l1.pl, l1.rp, l1.corp, l1.rating as r1, l2.rating as r2
from list l1, list l2
where l1.year = '2016' and l2.year = '2017' and l1.pa = l2.pa
and l1.pl = l2.pl and l1.rp = l2.rp and l1.corp = l2.corp) sq
group by pa, pl, rp
having (avg(sq.r2)-avg(sq.r1)) > 0
When I don't use the count in the first line ( or replacing the first line with this line
select sq.rp
It shows the output result as with one row 'rp1'.
+-----+
| rp |
|-----|
| rp1 |
+-----+
But when I use the 'count' keyword also, it shows the count as 2. I can't understand the reason.
JOINsyntax, it's so much easier to read than comma separated joins.