简体   繁体   中英

Calculating count over partition

I have a table that looks like this:

在此处输入图像描述

I want to make a new column which specifies count of mileage for each car. The output should look like this:

在此处输入图像描述

Basically, the new column is counting the occurrences for the mileage for EACH car type.

Here's what I'm using in my SQlite query:

 SELECT *, COUNT(Mileage) OVER (PARTITION BY Mileage ORDER BY Car) Count_mileage FROM car_table GROUP BY Car, Mileage

However, it does not give the accurate answer, as it takes count of mileage without partitioning into cars.

Can anybody help.

The partition should be over Car also and there is no need for the ORDER BY clause:

SELECT *,
       COUNT(*) OVER (PARTITION BY Car, Mileage) Count_mileage
FROM car_table

Also the GROUP BY clause is not needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM