oracle ORA-00936中的SQL结果中的解码语句:缺少表达式

bqujaahr  于 2022-11-03  发布在  Oracle
关注(0)|答案(2)|浏览(181)

试图找出为什么这个解码不工作。我只是想创建一个列,将上面的行分为〈5和〉=5,并给予他们一个相应的答案。我真的不明白为什么它不工作,我一直得到ORA-00936:缺少表达式。

SELECT a.account_id "Account ID",
   a.first_name ||' '|| a.last_name "Name",
   b.date_and_time_created "Date Created",
   c.date_and_time_ordered "Date Ordered",
   c.date_and_time_ordered - b.date_and_time_created "Days Ordered After Created",
   DECODE ((c.date_and_time_ordered - b.date_and_time_created), <5, 'No Reminder Needed',
                                                              >=5, 'Reminder Needed', ' ') "Reminder"
FROM shopper a 
   JOIN shopping_cart b ON a.account_id = b.shopper_account_id
   JOIN orders c ON a.account_id = c.shopper_account_id
5sxhfpxr

5sxhfpxr1#

由于在decode语句中使用<>=而导致的问题,请改用以下语句:

SELECT a.account_id "Account ID",
   a.first_name ||' '|| a.last_name "Name",
   b.date_and_time_created "Date Created",
   c.date_and_time_ordered "Date Ordered",
   c.date_and_time_ordered - b.date_and_time_created "Days Ordered After Created",
   (case when ( c.date_and_time_ordered - b.date_and_time_created  <  5 ) then 'No Reminder Needed'
         when ( c.date_and_time_ordered - b.date_and_time_created  >= 5 ) then 'Reminder Needed'
         else ' '
     end ) "Reminder"
FROM shopper a 
   JOIN shopping_cart b ON a.account_id = b.shopper_account_id
   JOIN orders c ON a.account_id = c.shopper_account_id
olhwl3o2

olhwl3o22#

或者,如果您坚持使用DECODE(虽然我不知道您为什么要这样做,因为CASE在这里更合适),您可以使用SIGN函数,例如

SELECT DECODE (
          SIGN ( (c.date_and_time_ordered - b.date_and_time_created) - 5),
          -1, 'less than 5',
          'greater than or equal to 5')
          result
  FROM ...

相关问题