skip to Main Content

This is my query

SELECT c.car_name,
       MAX(DATEDIFF(r.return_date, r.pickup_date)) + 1 AS No_of_Days,
       DENSE_RANK() OVER (ORDER BY MAX(DATEDIFF(r.return_date, r.pickup_date)) + 1 DESC) as Dense_Rank
FROM cars AS c
JOIN rentals AS r ON c.car_id = r.car_id
WHERE MONTH(pickup_date) = 5
GROUP BY c.car_id
ORDER BY Dense_Rank;

Error is

ERROR 1064 (42000) at line 80: You have an error in your SQL syntax;
check the m anual that corresponds to your MySQL server version for
the right syntax to use near (ORDER BY
MAX(DATEDIFF(r.return_date,r.pickup_date))+1 DESC) as Dense_Rank from
at line 2

2

Answers


  1. is this query will result the same error?

    SELECT c.car_name,
           MAX(DATEDIFF(r.return_date, r.pickup_date)) + 1 AS No_of_Days,
           DENSE_RANK() OVER (ORDER BY MAX(DATEDIFF(r.return_date, r.pickup_date)) + 1 DESC) as Dense_Rank
    FROM cars AS c
    JOIN rentals AS r ON c.car_id = r.car_id
    WHERE MONTH(pickup_date) = 5
    GROUP BY c.car_id
    ORDER BY MAX(DATEDIFF(r.return_date, r.pickup_date)) + 1 DESC;
    
    Login or Signup to reply.
  2. Try the following query

    SELECT a.car_name,
       a.No_of_Days,
       DENSE_RANK() OVER (ORDER BY a.No_of_Days DESC) as Dense_Rank
    FROM(
    SELECT c.car_name,(MAX(DATEDIFF(r.return_date, r.pickup_date)) + 1) AS 
    No_of_Days
    FROM cars AS c
    JOIN rentals AS r ON c.car_id = r.car_id
    WHERE MONTH(pickup_date) = 5
    GROUP BY c.car_id) a
    ORDER BY Dense_Rank;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search