How can I combine these two SQL statements?
SELECT SUM(hits01 + hits02 + hits03 + hits04 + hits05 + hits06 + hits07 + hits08 + hits09) AS \'AEROwiz\'
FROM tb
SELECT SUM(hits01 + hits02 + hits03 + hits04 + hits05 + hits06 +
hits07 + hits08 + hits09 + t2010.hits10 + t2010.hits11 + t2010.hits12) AS 'AEROwiz'
FROM tbl_2010 t2010
JOIN tbl_2011 t2011 ON t2010.appName = t2011.appName
WHERE t2010.appName='AEROwiz'
SELECT SUM(hits01 + hits02 + hits03 + hits04 + hits05 + hits06 +
hits07 + hits08 + hits09) AS 'AEROwiz'
FROM tbl_2011
WHERE appName='AEROwiz'
UNION ALL
SELECT SUM(hits10 + hits11 + hits12) AS 'AEROwiz'
FROM tbl_2010
WHERE appName='AEROwiz'
Use UNION ALL
as it will allow duplicates, and UNION
will not put duplicates in the query result. With the SUM()
aggregate, I'm guessing there's a good chance of duplication summations, so I'd go with UNION ALL
for this one.
You could use two subselects:
SELECT
(
SELECT SUM(hits01 + hits02 + hits03 + hits04 + hits05 + hits06 + hits07 + hits08 + hits09)
FROM tbl_2011
WHERE appName='AEROwiz'
) T1
+
(
SELECT SUM(hits10 + hits11 + hits12)
FROM tbl_2010
WHERE appName='AEROwiz'
) T2
AS AEROwiz
You may also want to consider normalizing your database so that you don't have a table for each year.
Use a UNION query - just stuff "UNION" between the two queries:
SELECT SUM(...) AS AEROWiz
FROM ...
UNION
SELECT SUM(...) AS AEROWiz
FROM ...
update
wrap the union in yet another query:
SELECT SUM(AEROWiz)
FROM (
.... unioned queries here
) AS child