Error [42s22] [oracle][odbc][ora]ora 00904: "year": Invalid Identifier

According to the text, the EXTRACT function in Oracle can retrieve specific datetime components, such as year, month, and day, from a given datetime value. It treats dates as ANSI dates and does not include time elements. In addition, the EXTRACT function can only extract certain components, such as timezone_hour and timezone_minute, from timestamps with time zone data types. The function can be used along with other functions, such as YEAR() and STR_TO_DATE(), to retrieve desired datetime values. The Year() function is specifically designed to extract the year from a date in Oracle SQL. Examples of using the EXTRACT function to retrieve datetime parts from a given value are provided, and it is mentioned that this function can be used when other query methods fail. However, the EXTRACT function can only be used with datetime and interval expressions.

It seems that Oracle SQL doesn't recognize the "YEAR" function directly. Instead, you can use the EXTRACT function to retrieve the year from a date in Oracle SQL.

Here's the modified query for both questions using the EXTRACT function to retrieve the year from the date:

  1. To find the name and count of the manufacturer of the most sold boots in April 2015:
SELECT p.ManufacturerName AS Manufacturer, COUNT(*) AS ManuCount,
  CASE
    WHEN pr.Category = 'Boots' THEN 'Yes'
    ELSE 'No'
  END AS Is_Boots
FROM Sale s
JOIN SaleItem si ON s.SaleID = si.SaleID
JOIN Product pr ON si.ProductID = pr.ProductID
JOIN Manufacturer p ON pr.ManufacturerID = p.ManufacturerID
WHERE pr.Category = 'Boots'
  AND EXTRACT(MONTH FROM s.SaleDate) = 4
  AND EXTRACT(YEAR FROM s.SaleDate) = 2015
GROUP BY p.ManufacturerName, pr.Category
ORDER BY Is_Boots DESC, ManuCount DESC;
  1. To find the name and count of the manufacturer of the most sold non-boots item in April 2015, along with the item category:
SELECT p.ManufacturerName AS Manufacturer, COUNT(*) AS ManuCount,
  CASE
    WHEN pr.Category = 'Boots' THEN 'Yes'
    ELSE 'No'
  END AS Is_Boots,
  pr.Category
FROM Sale s
JOIN SaleItem si ON s.SaleID = si.SaleID
JOIN Product pr ON si.ProductID = pr.ProductID
JOIN Manufacturer p ON pr.ManufacturerID = p.ManufacturerID
WHERE pr.Category != 'Boots'
  AND EXTRACT(MONTH FROM s.SaleDate) = 4
  AND EXTRACT(YEAR FROM s.SaleDate) = 2015
GROUP BY p.ManufacturerName, pr.Category
ORDER BY Is_Boots DESC, ManuCount DESC;

These modified queries should now be compatible with Oracle SQL and provide you with the answers you need.

Work fast from anywhere

Stay up to date and move work forward with BrutusAI on macOS/iOS/web & android. Download the app today.