SQL查询以在Oracle SQL中的3列的基础上获取输出

mzaanser  于 2023-10-16  发布在  Oracle
关注(0)|答案(2)|浏览(94)

我有一个场景,我有3列liqn_daysliq_monthsliqn_years

  • 如果liqn_days有值,其他值为0,则打印Monthly
  • 如果liq_months为3,其他为0,则按季度获取输出
  • 如果liq_months为6,其他列为0,则输出为半年
  • 如果liqn_years有一个值,而其他值为0,则输出将是每年的

样本数据:

liqn_days  liq_months  liqn_years 
---------------------------------    
 0          0             1
12          0             0
 0          3             0
  • 第一行的输出-每年
  • 第2行输出-每月
  • 第三行产出-季度

请协助我

5hcedyr0

5hcedyr01#

可以使用case表达式:

select t.*, (case when liqn_days > 0 and liq_months = 0 and liqn_yearsand = 0
                  then 'Monthly'
                  when liq_months = 3 and liqn_days = 0 and liqn_yearsand = 0
                  then 'quarterly'
                  when liq_months =  6 and liqn_days = 0 and liqn_yearsand = 0
                  then 'half yearly'
                  when liq_months =  0 and liqn_days = 0 and liqn_yearsand > 0
                  then ' yearly' 
                  else 'other'
             end)
from table t;
hmae6n7t

hmae6n7t2#

你也可以使用联盟所有

WITH da(Other_columns,liqn_years, liq_months, liqn_days) AS ( 
SELECT 'Other_colums', 0,0,1 FROM dual
UNION ALL SELECT 'Other_colums',12,0,0 FROM dual
UNION ALL SELECT 'Other_colums',0,3,0 FROM dual
)
SELECT Other_columns, 'Yearly' AS output  FROM da WHERE liqn_years > 0
UNION ALL
SELECT Other_columns, 'Monthly' AS output  FROM da WHERE liqn_years > 0
UNION ALL
SELECT Other_columns, 'quaterly' AS output  FROM da WHERE liqn_years > 0

结果

OTHER_COLUMNS|OUTPUT  |
-------------|--------|
Other_colums |Yearly  |
Other_colums |Monthly |
Other_colums |quaterly|

相关问题