为了举例,表unique_ptr
中有五列:
month
的数量由函数
col
如何选择列EXTRACT(WEEK FROM NOW()) - EXTRACT(WEEK FROM DATE_TRUNC('month', NOW())) + 1
?这就是我到目前为止所拥有的
colX
但这给了我错误
错误:列month.week不存在 SQL状态:42703
答案 0 :(得分:3)
用例陈述
SELECT
CASE WHEN (week expresion) = 1 THEN month.week1
WHEN (week expresion) = 2 THEN month.week2
WHEN (week expresion) = 3 THEN month.week3
WHEN (week expresion) = 4 THEN month.week4
ELSE month.week5
END as WeekValue
FROM month
OR
SELECT
CASE (week expresion)
WHEN 1 THEN month.week1
WHEN 2 THEN month.week2
WHEN 3 THEN month.week3
WHEN 4 THEN month.week4
ELSE month.week5
END as WeekValue
FROM month
答案 1 :(得分:0)
动态sql示例:
t=# create table so58(i int,w1 text);
CREATE TABLE
t=# create or replace function so59(_n int) returns table (i int,w text) as $$begin
return query execute format('select i,w%s from so58',_n);
end;
$$ language plpgsql;
CREATE FUNCTION
t=# select * from so59(1);
i | w
---+---
(0 rows)