简体   繁体   English

SQL最频繁的值取决于另一个值

[英]SQL most frequent values depending on another value

I'm using SQL*Plus and i need some help. 我正在使用SQL * Plus,我需要一些帮助。 I have 1 table with tons of information of different cars with different drivers. 我有1张桌子,上面摆放着不同驾驶员和不同汽车的大量信息。

What i want to find is the most frequent driver for each car plate . 我想找到的是每个车牌上最频繁的驾驶员 I have the CAR_PLATE and DRIVER values. 我有CAR_PLATEDRIVER值。 I've been trying to use group by and count , but I haven't been able to do it. 我一直在尝试使用group bycount ,但是我还没有做到这一点。

TEBLE (
..OTHER VALUES
DRIVER
CAR_PLATE
)

The data in it adds tons of different cars with different drivers but some repeat. 里面的数据增加了许多不同驾驶员的不同汽车,但有些重复。

the most frequent driver for each car plate 每个车牌上最频繁的驾驶员

This tells you that you need to group by CAR_PLATE and need the count of DRIVER . 这告诉您需要按CAR_PLATE并且需要DRIVER的计数。 Since you need the count of DRIVER driver also needs to be in the group by clause. 由于您需要DRIVER程序,因此DRIVER程序的数量也需要在group by子句中。 Since you want the most frequent one, you will also need to order by on the count. 由于您想要最频繁的一种,因此您还需要按order by

SELECT
  CAR_PLATE,
  DRIVER,
  count(DRIVER) AS driver_count
FROM tableName
GROUP BY CAR_PLATE, DRIVER
ORDER BY driver_count DESC;

Here is an SQL fiddle to play with: http://sqlfiddle.com/#!15/d08ea/4 这是一个SQL提琴玩法: http : //sqlfiddle.com/#!15/d08ea/4

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

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