I have a table where column names are like years "2020-05","2020-06", "2020-07"
etc and so many years as columns.I need to select only the current month, next month and third month columns alone from this table.(DB : PostgreSQL Version 11)
But since the column names are "TEXT" are in the format YYYY-MM
, How can I select only the current month and future 2 months from this table without hard-coding the column names.
Below is the table structure , Name : static_data
所需的select语句是这样的,该表包含上面屏幕截图中的14个月数据,例如DATES列。从这开始,我想要当前月和接下来的2个月列以及它们的数据,如下所示。
从静态中选择“ 2020-05”,“ 2020-06”,“ 2020-07” -选择本月和接下来的2个月 要求的输出:
Technically, you can use the information schema to achieve this. But, like GMB said, please re-design your schema and do not approach this issue like this, in the first place.
The special schema
information_schema
contains meta-data about your DB. Among these is are details about existing columns. In other words, you can query it and convert their names into dates to compare them to what you need.Here are a few hints.
查询现有列名。
比较两个日期。
您自己已经很接近转换了。
玩得开心,重新设计您的架构!
免责声明:这无法回答您的问题-但是评论太久了。
您需要修复此表的设计。不要将日期存储在列中,而应将每个日期放在单独的行中。
当前设计有很多缺点:
Instead, consider this simple design, with one table that stores the master data of each
item_sku
, and a child table现在,您原来的问题很容易解决: