SQL,使用列号代替列名

时间:2019-04-14 02:44:43

标签: sql postgresql

我试图查看是否可以通过编号而不是名称来选择列,我每周都有一次从SAP下载的报告,该报告可以为您提供下周的预测在Column_2中看到其从当前一周(第15周)开始,而我将在第二周生成报表,则Column_2将更改为week16,依此类推。

我无法创建名称查询,因为每周都会更改

示例:

ID  week15 week16 week17  
==  =====  =====  =====   
1    1000  3000   1000

2 个答案:

答案 0 :(得分:0)

如果将转置表并将其追加到滚动表,则选择单个星期应该很简单。

week      ID 
======   =====  
week15    1000
week16    3000
week17    1000

答案 1 :(得分:0)

尝试以下动态查询:

EXECUTE 'SELECT ID ' || ',week' || EXTRACT('week' FROM NOW()) ||  ',week' || EXTRACT('week' FROM NOW()) + 1 || ',week' || EXTRACT('week' FROM NOW()) + 2
' FROM table_name' INTO results