conditional select statement in oracle

前端 未结 3 1021
借酒劲吻你
借酒劲吻你 2021-01-21 12:43

I have two tables called order and public holiday as following:

Order Table:

OrderId      OrderDate
---------------------------
    1        10 Mar 2017
         


        
相关标签:
3条回答
  • 2021-01-21 13:09

    Changed the table name of order to ordero as order is a Keyword

     select ordero.orderid, ordero.orderdate, holiday.holidaydate,case when holiday.holidaydate is not null then 'Public Holiday'
     else to_char(ordero.orderdate,'Day') end "Day" 
     from ordero left outer join holiday on ordero.orderdate=holiday.holidaydate;
    
    0 讨论(0)
  • 2021-01-21 13:24

    Two things to remeber:

    1. ORDER is a keyword, so please use another table name
    2. CASE statement should be ended with END not with END AS

    Possible solution:

    SELECT O.OrderId,
      O.OrderDate,
      CASE
        WHEN H.HolidayDate IS NOT NULL
        THEN 'Public holiday'
        ELSE TO_CHAR(O.OrderDate, 'Day')
      END DAY
    FROM ORDER_TABLE O
    LEFT JOIN HOLIDAY_TABLE H
    ON H.HolidayDate=O.OrderDate;
    
    0 讨论(0)
  • 2021-01-21 13:25

    You can use an outer join like this:

    SELECT OrderId,
           OrderDate, 
           case when holidaydate is not null then  'Public holiday' 
                 else to_char(OrderDate, 'Day') end as DAY
    from orders
         left outer join holidays
            on OrderDate = holiday_date ;
    

    If the dates match holidaydate is not null so the CASE clause displays your desired string otherwise it displays the day of the orderdate .


    order is a reserved word. Presumably your real table has a different name, to avoid the ora-00903 error. I have used orders in my example, so you will need to edit my code to match your table name(s).

    0 讨论(0)
提交回复
热议问题