gpt4 book ai didi

sql - Oracle SQL - SELECT CASE WHEN 列=(选择语句)

转载 作者:行者123 更新时间:2023-12-03 08:33:51 27 4
gpt4 key购买 nike

我想使用 SQL case 表达式选择“up_year”。

如果我这样做就可以了:

SELECT CASE WHEN column1  = '2020' then 'up_year' else 'past_year' else end ;

但是当尝试这样做时:不想每年都改变。

SELECT 
CASE WHEN column1 = (select extract(year from sysdate) from dual)
then 'up_year'
else 'past_year' else end ;

最佳答案

您可以使用with例如子句

  with y as (select extract(year from sysdate) year from dual)
SELECT CASE WHEN column1 = y.year then 'up_year' else 'past_year' end from y -- and your table

A common table expression (CTE) is a named temporary result set that exists within the scope of a single statement and that can be referred to later within that statement

您可以使用 select into 将其分为两个步骤提取输出的变量然后使用它

关于sql - Oracle SQL - SELECT CASE WHEN 列=(选择语句),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64483097/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com