“FROM keyword not found where expected”

前端 未结 1 1261
日久生厌
日久生厌 2021-01-29 12:16
 SELECT distinct
   REPLACE(CM_NAME, \'/\', \' \') as CM_NAME,  
   TO_CHAR(Booking_Date,\'MM/DD/YYYY\') AS Booking_Date where Booking_Date = 
   \'03/20/2018\',
   sum(         


        
相关标签:
1条回答
  • 2021-01-29 12:42

    The code you posted doesn't have a from clause, but you said you just hadn't included it. It does, however, have a where clause - which is in the wrong place. It is also comparing a date with a string, which isn't a good idea as it relies on implicit conversion and session NLS settings. (Perhaps you think it can compare the fixed string with the date you just converted to a string - but that isn't the case, at least in the same level of query, and would be inefficient anyway in this case.) And as you are using aggregate function you need a group-by clause...

    It would seem like you want:

    SELECT REPLACE(CM_NAME, '/', ' ') as CM_NAME,  
       TO_CHAR(Booking_Date,'MM/DD/YYYY') AS Booking_Date,
       sum(Air_Revenue) as TTL_AIRFARE,
       sum(Room_Revenue) as TTL_ROOM,
       sum(Car_Revenue) AS TTL_CAR,
       sum(Activity_Revenue) as TTL_ACTIVITY,
       0 as TTL_CRUISE
    FROM your_table
    WHERE Booking_Date = DATE '2018-03-20'
    GROUP BY REPLACE(CM_NAME, '/', ' '),
       Booking_Date
    

    or if you want yesterday's date without having to specify it you can use:

    WHERE Booking_Date = TRUNC(sysdate - 1)
    GROUP BY REPLACE(CM_NAME, '/', ' '),
       Booking_Date
    

    That will only match rows where the Booking_Date is at exactly midnight. If it actually includes other times then you can do:

    WHERE Booking_Date >= TRUNC(sysdate - 1)
    AND Booking_Date < TRUNC(sysdate)
    GROUP BY REPLACE(CM_NAME, '/', ' '),
       TO_CHAR(Booking_Date,'MM/DD/YYYY')
    

    which will include a single full day of data.

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