SQL Developer查询昨日数据时遇Missing Expression Error求助
Hey there! Let's figure out why you're getting that "Missing Expression" error and fix your SQL query properly. There are a few syntax and logical issues in your original code, so let's break them down one by one:
Key Issues in Your Original Query
- You forgot the
FROMclause: The database has no idea which table to pull data from – this is a critical omission that's definitely causing part of the error. - Incorrect clause order: Your
WHEREstatement is stuck right in the middle of yourSELECTcolumns, which violates basic SQL syntax rules. Clauses need to follow the order:SELECT→FROM→WHERE→GROUP BY(when using aggregates likeSUM). - Misusing
DISTINCTwith aggregate functions: When you're usingSUMto calculate totals, you don't needDISTINCTat the start of the query. Instead, you need aGROUP BYclause to group your results by the non-aggregated columns (in this case,CM_NAMEandBooking_Date). - Potential date mismatch: Using
sysdate-1will give you the current time minus 24 hours, not the entire previous calendar day. If yourBooking_Dateincludes a time component, this might miss records from yesterday morning, or include records from today if the time is early enough.
Fixed Query
Here's the corrected version with explanations embedded:
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, SUM(Air_Revenue + Room_Revenue + Car_Revenue + Activity_Revenue) AS TTL_BOOKING FROM your_table_name -- Replace this with your actual table name! WHERE TRUNC(Booking_Date) = TRUNC(SYSDATE) - 1 -- Truncates time to match full yesterday GROUP BY REPLACE(CM_NAME, '/', ' '), TO_CHAR(Booking_Date, 'MM/DD/YYYY')
What Changed?
- Added the
FROMclause with a placeholder – make sure to swapyour_table_namefor the real name of the table you're querying. - Moved the
WHEREclause to its correct position, and usedTRUNC()on both dates to ignore the time component, ensuring we capture all records from the entire previous day. - Removed
DISTINCTand added aGROUP BYclause that includes every non-aggregated column in theSELECTstatement. This tells the database how to group rows before calculating the sums. - Formatted the query with line breaks for readability – this makes it way easier to catch syntax errors later on.
内容的提问来源于stack exchange,提问作者Kamran Malik
相关产品推荐
相关产品推荐

