初涉SQL,对于其中with和group by从句搭配sum,max方法的使用逻辑有一些疑问
例如,数据库中有以下几个table
Customer (cusid, cusname, cusphone, cuscity);
Driver (did, dname, dphone, dcity);
CarOwnership (did, carid);
Car (carid, carbrand, carsize);
Trips (cusid, carid, did, getontime, getofftime, price, distance);
要output出 carbrand。这个carbrand是最多distinct customer使用过的,即求每一种carbrand的distinct cusid数量sum,再求max这个数量的carbrand,应该如何使用sql语句实现呢?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
题主是想选出“乘客最喜爱的车型”。以下Postgresql代码未测试:
brand_cusid是车型-乘客的关系表,已做distinct处理。然后按
carbrand分组并按行数从大到小排序,并显示前10个车型。注意这些车型有可能是并列第一的。这时可增加
limit数量。