I have a table called app_to_tables which is mapping between apps and table names, table names are around 20, so I cannot hard code them in the query. Here for simplicity I put just 2 table names (table_1, table_2). Table structure is the following:
+--------+------------+
| app_id | table_name |
+--------+------------+
| 1 | table_1 |
| 1 | table_2 |
| 2 | table_1 |
| 2 | table_2 |
+--------+------------+
from the other hand I have another tables, which named as table_1, table_2, table_3. The schema for these tables are the same, and the content is the following:
table_1
+------------+--------+--------+
| date | app_id | signal |
+------------+--------+--------+
| 2018-01-01 | 1 | sg |
| 2018-01-01 | 1 | sg |
| 2018-01-01 | 2 | sg |
| 2018-01-01 | 2 | sg |
| 2018-01-02 | 1 | sg |
| 2018-01-02 | 1 | sg |
+------------+--------+--------+
table_2
+------------+--------+--------+
| date | app_id | signal |
+------------+--------+--------+
| 2018-01-01 | 1 | sg |
| 2018-01-01 | 2 | sg |
| 2018-01-01 | 2 | sg |
| 2018-01-01 | 2 | sg |
| 2018-01-02 | 1 | sg |
| 2018-01-02 | 2 | sg |
+------------+--------+--------+
I am trying to have an sql query outcome which will be the following:
The number of signals per each date
per each table_name
per each app_id
, like the following:
+--------+------------+------------+-------+
| app_id | table_name | date | count |
+--------+------------+------------+-------+
| 1 | table_1 | 2018-01-01 | 2 |
| 1 | table_1 | 2018-01-02 | 2 |
| 1 | table_2 | 2018-01-01 | 1 |
| 1 | table_2 | 2018-01-02 | 1 |
| 2 | table_1 | 2018-01-01 | 2 |
| 2 | table_2 | 2018-01-01 | 3 |
| 2 | table_2 | 2018-01-02 | 1 |
+--------+------------+------------+-------+
My main trouble is how I can select a table names and then dynamically (without hardcoding the table names in the query) use that table names to count the number of signals.