初涉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语句实现呢?
The question is to choose "passengers' favorite car model". The following Postgresql code is not tested:
brand_cusid
is the model-passenger relationship table, which has beendistinct
processed.brand_cusid
是车型-乘客的关系表,已做distinct
处理。然后按
carbrand
分组并按行数从大到小排序,并显示前10个车型。注意这些车型有可能是并列第一的。这时可增加
limit
carbrand
and sort by row number from large to small, and display the top 10 models. 🎜🎜 🎜🎜Note that these models may be tied for first place. At this time, thelimit
quantity can be increased. 🎜🎜 🎜