简体   繁体   English

仅根据周末/工作日/两者的标志获取行

[英]Only get rows based on flag for weekend / weekday / both

I have a whole bunch of tariffs, some work on weekends, some work on weekdays some on both.我有一大堆关税,有些在周末工作,有些在工作日工作,有些则两者兼而有之。 Sometimes I'll be querying on NOW() but sometimes I'll be querying on datetime column.有时我会在 NOW() 上进行查询,但有时我会在datetime列上进行查询。

id   | Weekday | Weekend | Price
 1   | 1       | 0       | 0.04   
 2   | 0       | 1       | 0.02          

date
2020-04-15 00:00:00
2012-04-16 00:00:00

The date is from another table and is not related to the Price / days of week. date来自另一个表,与价格/星期几无关。

I know I can get the weekend date s by我知道我date通过

SELECT * FROM tariff where EXTRACT(ISODOW FROM date) IN (6,7)

however I can't think of how I'd get rows that are either weekend / weekdays or both given a date.但是我想不出我将如何获得周末/工作日或两者都给定日期的行。

** edit ** ** 编辑 **

Updated the tables to show the dates are seperate.更新了表格以显示日期是分开的。 What I'm trying to get is the tariff that corresponds to the date in that table, whether it's on a week day or a weekend (or both but I can extrapolate that).我想要得到的是与该表中的日期相对应的关税,无论是在工作日还是周末(或两者兼而有之,但我可以推断出来)。

The weekend 1 is the tariff that is used for weekends, weekdays 1 , all days is both.周末1是用于周末、工作日1的关税,所有日子都是。

Cannot give you a query, supply anything to query.不能给你查询,提供任何查询。 Nor can we be sure that the columns Weekday and Weekend mean as you didn't tell us.我们也不能确定 Weekday 和 Weekend 列的含义,正如您没有告诉我们的那样。 But if we take them as boolean indicator where 1 means desired may some thing like will work for you.但是,如果我们将它们作为 boolean 指标,其中 1 表示需要,则可能会有类似的东西对您有用。

    select ...
      from ...
     where ...
       and (   (weekday = 1 and weekend =1) 
            or (weekday = 1 and extract(isodow from date) not in (6,7))
            or (weekend = 1 and extract(isodow from date)    in (6,7)) 
           ) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM