0

I Have two tables for get impression and click

Product
+----+--------+
| PID| brand  |
+----+--------+
|  1 | Ford   |
|  2 | Toyota |
|  6 | Holden |
+----+--------+

States
+----+--------+------+--------+------------+
| ID | PID    |CLICKS| VIEWS  | DATE       |
+----+--------+------+--------+------------+
|  1 | 1      |   1  |   0    |  12/12/2015|
|  1 | 1      |   1  |   0    |  12/12/2015|
|  2 | 2      |   1  |   0    |  12/12/2015|
|  3 | 2      |   0  |   1    |  12/12/2015|
|  3 | 1      |   0  |   1    |  12/12/2015|
+----+--------+------+--------+------------+

I need to get some result like this

+--------+------+--------+
| PID    |CLICKS| VIEWS  |
+--------+------+--------+
| 1      |   2  |   1    |
| 2      |   1  |   0    |
+--------+------+--------+

is it possible? i have tried too many times its coming faults data

3
  • Do a JOIN with a GROUP BY. Commented Apr 14, 2016 at 12:49
  • 1
    lookup how group by works Commented Apr 14, 2016 at 12:49
  • SELECT stats_property.date, property.idproperty, COUNT(stats_property.clicks='1') AS A, COUNT(stats_property.impression='0') AS I FROM property INNER JOIN stats_property ON property.idproperty = stats_property.idproperty group by property.idproperty, stats_property.clicks I Have try this way its not working Commented Apr 14, 2016 at 13:09

2 Answers 2

1
SELECT
  PID,
  SUM(CLICKS) as CLICKS,
  SUM(VIEWS) as VIEWS
FROM States
GROUP BY PID

If you need 0 for all Products

SELECT
  Products.PID,
  SUM(CLICKS) as CLICKS,
  SUM(VIEWS) as VIEWS
FROM Products 
     LEFT JOIN States ON States.PID=Products.PID
GROUP BY Products.PID
Sign up to request clarification or add additional context in comments.

Comments

1

Use this by Inner Join.

SELECT t1.pid, sum(clicks) as CLICKS, sum(views) as VIEWS
FROM Product as t1 INNER JOIN States as t2 ON t1.pid=t2.pid GROUP BY t1.pid

1 Comment

INNER JOIN is futile. States table is enough. If all products are necessary then OUTER JOIN should be used

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.