简体   繁体   English

计算分区计数

[英]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: output 应如下所示:

在此处输入图像描述

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:这是我在 SQlite 查询中使用的内容:

 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:分区也应该在Car之上,并且不需要ORDER BY子句:

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

Also the GROUP BY clause is not needed.也不需要GROUP BY子句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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