简体   繁体   English

如何使用互斥的 where 语句选择数据

[英]How select data using mutually exclusive where statements

I want to execute a select statement based on a condition, change the where condition which are mutually exclusive我想根据条件执行select语句,改变互斥的where条件

I want to get the data from a table based on the date.我想根据日期从表中获取数据。 If the current date is 16th then i want the data for the dates 1-12 of the current month and if its 4th then i want the data for the previous whole month.如果当前日期是 16 号,那么我想要当月的 1-12 号日期的数据,如果是第四号,那么我想要前一个月的数据。 I dont understand how to use if or when with this.我不明白如何使用 if 或 when 。 How do i add a condition based on which i could execute one of the which statement.我如何添加一个基于我可以执行 which 语句之一的条件。 I am trying to automate a report that will run on the 16th and on the 4th of every month and the data that will be added to the report will be as explained before.我正在尝试自动化一个将在每个月的 16 日和 4 日运行的报告,并且将添加到报告中的数据将如前所述。 I am using a oracle database我正在使用 oracle 数据库

The logic would look like this using ANSI/ISO syntax:使用 ANSI/ISO 语法的逻辑如下所示:

where (extract(day from current_date) = 16 and
       extract(month from datecol) = extract(month from current_date) and extract(year from datecol) = extract(year from current_date)
      ) or
      (extract(day from current_date) = 4 and
       extract(month from datecol) = extract(month from current_date - interval '1 month') and extract(year from datecol) = extract(year from current_date - interval '1 month')
      )

Of course, date functions differ by database, so this might need to be adapted for your particular database.当然,日期函数因数据库而异,因此可能需要针对您的特定数据库进行调整。

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

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