Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据PostgreSQL的星期几使用不同的筛选器运行查询_Postgresql_Date - Fatal编程技术网

根据PostgreSQL的星期几使用不同的筛选器运行查询

根据PostgreSQL的星期几使用不同的筛选器运行查询,postgresql,date,Postgresql,Date,尝试使用不同的日期筛选器运行同一视图,具体取决于运行日期。如果我在周五、周六、周日或周一运行查询,那么应该在周二和周四过滤日期。如果我在周二、周三、周四运行查询,那么应该在周五-周一过滤日期。这是仅有的两种情况。我正在使用PostgreSQL 除此之外,视图本身非常简单: select * from tbl_1 where date between ____ and _____ 我确实有一个可以加入的日期表。我试过这样的方法: (select date ,case when day_of_we

尝试使用不同的日期筛选器运行同一视图,具体取决于运行日期。如果我在周五、周六、周日或周一运行查询,那么应该在周二和周四过滤日期。如果我在周二、周三、周四运行查询,那么应该在周五-周一过滤日期。这是仅有的两种情况。我正在使用PostgreSQL

除此之外,视图本身非常简单:

select * from tbl_1
where date between ____ and _____
我确实有一个可以加入的日期表。我试过这样的方法:

(select date
,case when day_of_week_iso IN(2, 3, 4) THEN 1 ElSE 4 END as "day"
from tbl.date
where date = current_date
) dates
使用
extract(道琼斯指数自当前日期起)
提取一周中的当前日期(星期日为0),请参阅

您可以使用自己的函数来缩短代码,但是在所有情况下使用
case
更简单,可能更快

select *
from tbl_1
where case extract(dow from current_date)
    when 2 then date between current_date- 4 and current_date- 1
    when 3 then date between current_date- 5 and current_date- 2
    when 4 then date between current_date- 6 and current_date- 3
    when 5 then date between current_date- 3 and current_date- 1
    when 6 then date between current_date- 4 and current_date- 2
    when 0 then date between current_date- 5 and current_date- 3
    when 1 then date between current_date- 6 and current_date- 4
    end;