data
There’re multiple tables which format is dm_ym_file_2022XXXX(date). The start date is 20220720 and the end date is 20221220
dm_ym_file_20220720
dm_ym_file_20220721
……
dm_ym_file_20221220
dm_ym_file_rules stores all names.The dm_ym_file_rules as belows:
ID start_date end_date table_names
36 2022-07-20 2022-07-20 dm_ym_file_share_20220720
37 2022-07-21 2022-07-21 dm_ym_file_share_20220721
38 2022-07-22 2022-07-22 dm_ym_file_share_20220722
goal
I want to groupby some fields from all of these tables into one table.
insert into target_table
select a,b,c,sum(d)
from
(
select a,b,c,sum(d)
dm_ym_file_20220720
group by 1,2,3
union all
select a,b,c,sum(d)
dm_ym_file_20220721
group by 1,2,3
union all
……
select a,b,c,sum(d)
dm_ym_file_20221220
group by 1,2,3
) a
group by 1,2,3;
My code should list all tables. It’s inconvenient.
3
Answers
The pattern you should use here is to union the source tables first, and then aggregate once on the outside.
Note that a better long term solution here might be to revisit your database design. Do you really need to have separate tables with an almost identical structure? It might make more sense to just have a single table, with an additional column(s) corresponding to the year/month date.
There are (at least) two problems in you SQL statement, besides the already mentioned fact that you should not store data like this.
In you statement the required keyword
FROM
is missing (3 times)In the subquery the column
sum(d)
should get an aliasd
. The subquery will then have a 4th column, which can be referred to asd
, which makes it possible for the outer query to refer to this field asd
.Applied to your script, the script looks like:
see: DBFIDDLE
This procedure could gets things done:
But keep in mind that this isn’t the best way to organize database. A single table with index on the datetime column would be better.