I want to select the views, increase and percentage increase from a table but only show the unique id’s. I can get the information what I want but it’s more then I need and can’t narrow the results.
I have the following data:
+------------+------------+-------+-------+
| datum | youtube_id | views | likes |
+------------+------------+-------+-------+
| 2012-10-07 | 123456 | 100 | 100 |
| 2012-10-07 | 98765 | 200 | 200 |
| 2012-10-08 | 123456 | 150 | 150 |
| 2012-10-08 | 98765 | 300 | 300 |
| 2012-10-09 | 123456 | 300 | 300 |
| 2012-10-09 | 98765 | 500 | 500 |
+------------+------------+-------+-------+
And the following sql statement:
SELECT
id,
startcount,
endcount,
(endcount - startcount)increasing,
((endcount - startcount) / ( startcount ) *100)percentChange
FROM (SELECT youtube_id AS id, views AS startcount
FROM charts
WHERE datum = '2012-10-08')startRange,
(SELECT views AS endcount
FROM charts
WHERE datum = '2012-10-09')endRange
This gives me the following result:
+--------+------------+----------+------------+---------------+
| id | startcount | endcount | increasing | percentChange |
+--------+------------+----------+------------+---------------+
| 123456 | 150 | 300 | 150 | 100.0000 |
| 98765 | 300 | 300 | 0 | 0.0000 |
| 123456 | 150 | 500 | 350 | 233.3333 |
| 98765 | 300 | 500 | 200 | 66.6667 |
+--------+------------+----------+------------+---------------+
The expected result would be:
+--------+------------+----------+------------+---------------+
| id | startcount | endcount | increasing | percentChange |
+--------+------------+----------+------------+---------------+
| 123456 | 150 | 300 | 150 | 100.0000 |
| 98765 | 300 | 500 | 200 | 66.6667 |
+--------+------------+----------+------------+---------------+
I have been looking at a group by or a join but can’t figure this out, already redoing this for a few days now but running into a loop myself.
If someone can point me to the right direction or help me out that would be great!
SQL Fiddle
Edit: changed the increasing