My table is called Storage_Tot which has date, tagid, value components.
Below are the two tables (Table A and Table B) I created using SQL query. I essentially ran same query with different tagid with the same table ‘Storage_Tot). I would like to combine table A and B and also add a 3rd column which is the sum of the values of Table A and Table B (Table C).
Table A (Left table) Table B (Right Table)
Here is the sample query that I wrote for the two tables.
This is how I would like my final table to look like. As a first step I was trying to just combine the the values of Table A and Table B and after that focus on the arithmetic aspect.
Here is the SQL query when combing the two tables but it does not work enter image description here
_______________________________________ APPEND
Since I couldn’t post under the response question, After implementing your suggestion here is the error that I get. enter image description here
This is how the query looks as it sits and it appears that its not like where Storage_Tot.TagID = 106
Advertisement
Answer
I believe what you are asking for is (1) a JOIN
and (2) a calculated column.
Here’s the general idea for a JOIN
:
SELECT tblA.date, tblA.value, tblB.value FROM (SELECT * FROM Storage_Tot WHERE tagid = 'TABLEA_TAGID') tblA INNER JOIN (SELECT * FROM Storage_Tot WHERE tagid = 'TABLEB_TAGID') tblB ON (tblA.date = tblB.date);
Some of that is guesswork, because you didn’t provide complete details in your question. But hopefully you can see what’s going on: the sub-queries in a way ‘create’ what you are calling the two tables shown in your first image.
Now, to add a total, just add a calculated column to the above query:
SELECT tblA.date, tblA.value, tblB.value, tblA.value + tblB.value AS total_value
I have not verified any of this in an instance of MySQL, but I do believe I have the syntax correct for MySQL. You might need to make small adjustments for typos.. I have verified the above syntax via SQL Fiddle. Of course you need to fill in what TABLEA_TAGID
and TABLEB_TAGID
really are.
There are also lots of tutorials and references for using JOIN
s on-line. There are various kinds of JOIN
s, and I’m only showing one kind here.