简体   繁体   English

根据 CASE 语句执行 SELECT

[英]Do a SELECT based on a CASE statement

I am trying to execute a select statement depending on the CASE我正在尝试根据 CASE 执行选择语句

SELECT CASE WHEN TO_CHAR(today, "%a") = 'Mon' 
THEN (SELECT COUNT(*) FROM  jobs WHERE datein > today-2) 
ELSE (SELECT COUNT(*) FROM  jobs WHERE datein = today) 
END CASE;

As a result I would expect if the day is 'Mon' (monday) then execute the Select from (monday - 2 days) and bring all the columns/rows not just 1 COUNT , ELSE execute the select for the current day.因此,我希望如果这一天是“星期一”(星期一)然后执行 Select from (monday - 2 days) 并带来所有列/行而不仅仅是 1 COUNT ,否则执行当天的选择。

In case the day is monday the result should be如果当天是星期一,结果应该是

id | jobs | date
1  |12,500| 12-7-2019
2  |10,800| 12-8-2019

In case the day is different from monday should be the same but for today's data如果当天与星期一不同,则应相同但对于今天的数据

id | jobs | date
3  |35,000| 12-9-2019

If this is not possible is there any other method?(considering my limited access)如果这是不可能的,还有其他方法吗?(考虑到我的访问权限有限)

Database server: INFORMIX 12.1 I can read only the database using Toad for Data Analysts or IBM Data Studio.数据库服务器:INFORMIX 12.1 我只能使用 Toad for Data Analysts 或 IBM Data Studio 读取数据库。

Without a schema and data sample are hard to know exactly what you want, but if we assume that you want to get all rows from the table jobs depending on date, you can execute this query没有模式和数据样本很难确切知道您想要什么,但是如果我们假设您想根据日期从表作业中获取所有行,则可以执行此查询

SELECT jobs.*
  FROM jobs
 WHERE jobs.datein >=  CASE
                         WHEN WEEKDAY(TODAY) = 1 THEN TODAY-2
                         ELSE TODAY
                       END

I don't have the tools you mention, but this query run ok on Informix 12.10 and 14.10 from dbaccess.我没有你提到的工具,但是这个查询在 dbaccess 的 Informix 12.10 和 14.10 上运行正常。

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

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