I have two sql tables,
table_1 looks like this:
datestamp items
2020-01-01 1
2020-01-01 33
2020-01-01 245
2020-01-01 55
2020-01-01 534
2020-01-01 35
2020-01-01 35
2020-01-02 10
2020-01-02 100
2020-01-02 50
2020-01-02 10
2020-01-02 1
2020-01-02 166
2020-01-02 76
2020-01-02 67
table_2 looks like this:
datestamp items_2
2020-01-01 346
2020-01-01 3623
2020-01-02 63
2020-01-02 73
What I am trying to achieve is:
- Group items by datestamp for both tables
- Join the tables on a datestamp
- Create another column which would be items - items_2
- Make it all as a view
What I have tried:
select datestamp, SUM(items) from table_1 group by datestamp
select datestamp, SUM(items_2) from table_2 group by datestamp
And it returns what is expected ( numbers are dummy ):
2020-01-01 132
2020-01-02 432
2020-01-03 353
2020-01-01 563
2020-01-02 236
2020-01-03 364
When I try to join the tables:
select table_1.datestamp, table_1.SUM(items), table_2.SUM(items_2)
from table_1
left join table_2
on table_1.datestamp = table_2.datestamp
I get:
Invalid operation: schema "table_1" does not exist
The output I am looking for would look like this ( numbers are dummy ):
datestamp items_1_sum items_2_sum difference
2020-01-01 346 525 items_2_sum-items_1_sum
2020-01-02 3623 352 63
2020-01-03 63 52 -36
2020-01-04 73 52 -352
Where is my mistake and is there a better way of achieving my desired output?
table_1.SUM(items)is wrong, that would mean a function calledsum()in the schematable_1. You wantsum(table_1.items)and the same fortable_2.SUM(items_2). - sticky bit