Sql 在一周中的某一天行动

Sql 在一周中的某一天行动,sql,postgresql,Sql,Postgresql,希望有人能在这里提供一些建议,我正在使用PostgreSQL数据库 此查询的目的是确定当前允许访问的用户。目标是提供一个日期和时间范围作为输入,并查看在指定日期和时间范围内允许访问的用户帐户 该表是这样设置的,“开始时间”和“结束时间”的时间列指定了允许用户使用的时间范围。然后为一周中的每一天创建一个布尔列,指定是否允许该用户访问该天的时间范围 [START TIME] [END TIME] [MON] [TUES] [WED] [THURS] [FR

希望有人能在这里提供一些建议,我正在使用PostgreSQL数据库

此查询的目的是确定当前允许访问的用户。目标是提供一个日期和时间范围作为输入,并查看在指定日期和时间范围内允许访问的用户帐户

该表是这样设置的,“开始时间”和“结束时间”的时间列指定了允许用户使用的时间范围。然后为一周中的每一天创建一个布尔列,指定是否允许该用户访问该天的时间范围

   [START TIME]    [END TIME]   [MON]   [TUES]     [WED]     [THURS]     [FRI]    [SAT]     [SUN]
    09:00:00       11:00:00     True     True      True       True       True     False     False 
现在,这似乎很简单,但在我看来,系统需要首先知道一周中的哪一天,然后使用一个长时间的“case when”来表示“如果输入的日期是星期一,并且table.mon=true,则该用户符合标准

到目前为止,我有这样的想法:

DO $$
DECLARE 

--Specify 'variables'
active_date timestamp := '2020-10-4';
start_time time := '00:00:00';
end_time time := '23:59:00';
day_of_week text := to_char(active_date, 'day');

BEGIN
    CREATE TEMP TABLE temp_output ON COMMIT DROP AS
    select distinct
    date(account.lastupdated) as "Date",
    concat(to_char(account.start_time, 'HH:MI'), ' - ', to_char(account.end_time, 'HH:MI')) as "Time Range"
    from account
    where account.lastupdated >= active_date AND account.lastupdated < active_date + interval '1 day'
    and account.start_time >= start_time AND account.end_time <= end_time;
END $$;

SELECT * FROM temp_output;

但是,如何根据输入的日期为整个结果集实现此逻辑?

如果您只需要确定某一行是否与特定日期匹配,请检查相应的列是否为真

-- Check if this is valid for Monday
where mon

正如您所发现的,在SQL中,使用单个列存储列表项效果不好

在传统SQL中,您将有一个联接表来存储这次应用的日期

create table schedule (
  id bigserial primary key,
  start_time time not null,
  end_time time not null
);

create table schedule_days (
  schedule_id bigint not null references schedule(id),
  day text not null
);

insert into schedule (start_time, end_time) values ('09:00', '11:00');
insert into schedule_days (schedule_id, day) values
  (1, 'Mon'), (1, 'Tues'), (1, 'Wed'), (1, 'Thurs'), (1, 'Fri');

select *
from schedule s
join schedule_days sd on s.id = sd.schedule_id
where current_time between start_time and end_time
  and day = $1
我们可以通过使用枚举来存储天数来改进这一点。这样可以确保只添加正确的值。它节省了存储空间,因为它们实际上存储为整数。它们可以继续作为字符串进行查询

create type day_of_week as ('Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun');

create table schedule_days (
  schedule_id bigint not null references schedule(id),
  day day_of_week not null
);

或者,您也可以使用单程票

create table schedule (
  id bigserial primary key,
  start_time time not null,
  end_time time not null,
  days day_of_week[] not null
);

insert into schedule (start_time, end_time, days)
  values ('09:00', '11:00', array['Mon', 'Tues', 'Wed', 'Thurs', 'Fri']);
然后在规范化输入后,检查它是否包含在days数组中

select *
from schedule s
where current_time between start_time and end_time
  and days @> array['Tues']

也许处理这个问题的最好方法是在底部加上一个长where子句?其中(星期日='sunday'和account.sun=True)或(星期日='sunday'和account.mon=True)或…感谢您的帮助。不幸的是,我没有创建(甚至没有维护)这个数据库。这是一个中等流行应用程序的数据库,他们将我外包给他们,为他们创建报告。。。我来了@在这种情况下,要确定它是否对某一天有效,只需检查相应的day列是否为true
where mon
检查周一是否有效。
select *
from schedule s
where current_time between start_time and end_time
  and days @> array['Tues']