久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

mysql - 關于sql語句中的with從句和group by分組

瀏覽:121日期:2022-06-14 14:35:34

問題描述

初涉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語句實現呢?

問題解答

回答1:

題主是想選出“乘客最喜愛的車型”。以下Postgresql代碼未測試:

select carbrand, count(*) as customersfrom ( select distinct carbrand, cusid from Trips inner join Car using (carid)) as brand_cusidgroup by carbrandorder by customers desclimit 10

brand_cusid是車型-乘客的關系表,已做distinct處理。

然后按carbrand分組并按行數從大到小排序,并顯示前10個車型。

注意這些車型有可能是并列第一的。這時可增加limit數量。

相關文章:
主站蜘蛛池模板: 六枝特区| 永平县| 吴江市| 安陆市| 玛多县| 景洪市| 旬邑县| 镇宁| 富源县| 古蔺县| 宁化县| 仪征市| 略阳县| 福州市| 温泉县| 慈溪市| 噶尔县| 新巴尔虎左旗| 民和| 大悟县| 肇庆市| 澳门| 紫云| 涟源市| 尉犁县| 建宁县| 惠州市| 长葛市| 岗巴县| 云阳县| 彭阳县| 柞水县| 司法| 曲麻莱县| 凯里市| 临湘市| 磐安县| 柯坪县| 信丰县| 灵石县| 岑溪市|