I have 2 identical tables (_impressionsdaily, _impressionsalltime), at the end of every day i move all the data from daily to alltime. The structure is: ID, purchaseID, impression, unique, date.
I run the following query:
SELECT
daily.purchaseID,
SUM(daily.impression + IFNULL(alltime.impression, 0)) AS impression,
SUM(daily.unique + IFNULL(alltime.unique, 0)) AS `unique`
FROM _impressionsdaily daily
LEFT JOIN _impressionsalltime alltime ON alltime.purchaseID= daily.purchaseID
GROUP BY daily.purchaseID;
The thing is that if the alltime table has purchases that are not available in the daily i wont see them.
Basically the idea is to sum all impressions for both daily and alltime.
---EDIT The thing is at 12:00am everyday the daily table is copied to the alltime and then deleted. so the solution given are wrong..
Thanks for the Help, Danny
I think you're looking for a Union Select . Try something like this:
SELECT DISTINCT(p.ID), p.purchaseID
FROM (SELECT *
FROM _impressionsdaily
UNION SELECT *
FROM _impressionsalltime) AS p
GROUP BY p.purchaseID
SELECT
daily.purchaseID,
SUM(daily.impression + IFNULL(alltime.impression, 0)) AS impression,
SUM(daily.unique + IFNULL(alltime.unique, 0)) AS `unique`
FROM _impressionsdaily daily
LEFT JOIN _impressionsalltime alltime ON alltime.purchaseID= daily.purchaseID
GROUP BY daily.purchaseID;
In this case you haven't selected any field form _impressionsalltime table.
You can use this sql to select field from _impressionsalltime table as well
SELECT
alltime.*,
daily.purchaseID,
SUM(daily.impression + IFNULL(alltime.impression, 0)) AS impression,
SUM(daily.unique + IFNULL(alltime.unique, 0)) AS `unique`
FROM _impressionsdaily daily
LEFT JOIN _impressionsalltime alltime ON alltime.purchaseID= daily.purchaseID
GROUP BY daily.purchaseID;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.