Tenho duas tabelas (adwords_performance
e analytics_transaction
) e preciso fazer um SELECT
somando as colunas adwords_performance.clicks
, adwords_performance.impressions
e analytics_transaction.transaction_revenue
.
Quando eu faço o SELECT
ele me retorna valores de errados em adwords_performance.clicks
e adwords_performance.impressions
.
SELECT
adwords_performance.date,
Sum(adwords_performance.clicks) AS clicks,
Sum(adwords_performance.impressions) AS impressions,
Sum(analytics_transaction.transaction_revenue) AS revenue
FROM
adwords_performance
LEFT JOIN analytics_transaction
ON analytics_transaction.date = adwords_performance.date AND
analytics_transaction.adwords_id = adwords_performance.adwords_id AND
analytics_transaction.adwords_campaign_id = adwords_performance.adwords_campaign_id
WHERE
adwords_performance.date = '2015-03-01' AND
adwords_performance.adwords_id = '0123456789'
GROUP BY
adwords_performance.date
Resultado:
+------------+--------+-------------+---------+
| date | clicks | impressions | revenue |
+------------+--------+-------------+---------+
| 2015-03-01 | 465 | 21017 | 1937.71 |
+------------+--------+-------------+---------+
Resultado que era para ser retornado:
+------------+--------+-------------+---------+
| date | clicks | impressions | revenue |
+------------+--------+-------------+---------+
| 2015-03-01 | 364 | 18577 | 1937.71 |
+------------+--------+-------------+---------+
Tabelas:
mysql> describe adwords_performance;
+--------------------------------+---------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------------+---------------------------------+------+-----+---------+----------------+
| id | int(20) unsigned | NO | PRI | NULL | auto_increment |
| adwords_id | bigint(255) unsigned | NO | | NULL | |
| adwords_campaign_id | bigint(255) unsigned | NO | | NULL | |
| campaign_network_type | varchar(255) | NO | | NULL | |
| date | date | NO | | NULL | |
| week | date | NO | | NULL | |
| week_day | varchar(25) | NO | | NULL | |
| clicks | int(255) | NO | | NULL | |
| impressions | int(255) | NO | | NULL | |
| cost | int(255) | NO | | NULL | |
| av_position | decimal(2,1) | NO | | NULL | |
| clicks_converted | int(255) | NO | | NULL | |
| estimated_converted | int(255) | NO | | NULL | |
| estimated_converted_rate | decimal(10,2) unsigned zerofill | NO | | NULL | |
| clicks_converted_assisted | int(255) | NO | | NULL | |
| impressions_converted_assisted | int(255) | NO | | NULL | |
| lost_budget | varchar(255) | NO | | NULL | |
| clicks_invalid | int(255) | NO | | NULL | |
+--------------------------------+---------------------------------+------+-----+---------+----------------+
mysql> describe analytics_transaction;
+----------------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+----------------------+------+-----+---------+----------------+
| id | int(15) unsigned | NO | PRI | NULL | auto_increment |
| analytics_id | bigint(255) unsigned | NO | MUL | NULL | |
| date | date | NO | | NULL | |
| time | int(2) | NO | | NULL | |
| source | varchar(255) | NO | | NULL | |
| medium | varchar(255) | NO | | NULL | |
| city | varchar(255) | NO | | NULL | |
| adwords_id | bigint(255) unsigned | NO | | NULL | |
| adwords_campaign_id | bigint(255) unsigned | NO | | NULL | |
| transaction_id | bigint(255) unsigned | NO | MUL | NULL | |
| transaction_revenue | double(10,2) | NO | | NULL | |
| transaction_shipping | double(10,2) | NO | | NULL | |
| transaction_tax | double(10,2) | NO | | NULL | |
| product_quantity | int(10) | NO | | NULL | |
+----------------------+----------------------+------+-----+---------+----------------+