-1

I have a data set that looks like below:

CarType Date Car
Honda 5/28/2022 car1
Honda 5/28/2022 car1
Honda 8/11/2022 car2
Honda 8/11/2022 car2
BMW 5/28/2022 car1
BMW 5/28/2022 car1
BMW 8/11/2022 car2
BMW 8/11/2022 car2

I want to using row_number(), rank() function create an additional column 'Expected' that looks like this:

CarType Date Car Expected
Honda 5/28/2022 car1 1
Honda 5/28/2022 car1 1
Honda 8/11/2022 car2 2
Honda 8/11/2022 car2 2
BMW 5/28/2022 car1 1
BMW 5/28/2022 car1 1
BMW 8/11/2022 car2 2
BMW 8/11/2022 car2 2

I tried a query below. But does not seem to give me the right results

select CarType, Date, Car, ROW_NUMBER() OVER (PARTITION BY CarType, Car ORDER BY Date ASC) AS RW from table

genie
  • 195
  • 1
  • 2
  • 11

1 Answers1

0
SELECT CarType, 
       Date, 
       Car, 
       DENSE_RANK() OVER (PARTITION BY CarType 
                          ORDER BY Car) AS RW 
from table
Akina
  • 39,301
  • 5
  • 14
  • 25