本文實(shí)例講述了MySQL累積聚合原理與用法。分享給大家供大家參考,具體如下:
網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站介紹好的網(wǎng)站是理念、設(shè)計(jì)和技術(shù)的結(jié)合。創(chuàng)新互聯(lián)建站擁有的網(wǎng)站設(shè)計(jì)理念、多方位的設(shè)計(jì)風(fēng)格、經(jīng)驗(yàn)豐富的設(shè)計(jì)團(tuán)隊(duì)。提供PC端+手機(jī)端網(wǎng)站建設(shè),用營(yíng)銷思維進(jìn)行網(wǎng)站設(shè)計(jì)、采用先進(jìn)技術(shù)開源代碼、注重用戶體驗(yàn)與SEO基礎(chǔ),將技術(shù)與創(chuàng)意整合到網(wǎng)站之中,以契合客戶的方式做到創(chuàng)意性的視覺(jué)化效果。
累積聚合為聚合從序列內(nèi)第一個(gè)元素到當(dāng)前元素的數(shù)據(jù),如為每個(gè)員工返回每月開始到現(xiàn)在累積的訂單數(shù)量和平均訂單數(shù)量
行號(hào)問(wèn)題有兩個(gè)解決方案,分別是為使用子查詢和使用連接。子查詢的方法通常比較直觀,可讀性強(qiáng)。但是在要求進(jìn)行聚合時(shí),子查詢需要為每個(gè)聚合掃描一次數(shù)據(jù),而連接方法通常只需要掃描一次就可以得到結(jié)果。下面的查詢使用連接來(lái)得到結(jié)果
SELECT a.empid, a.ordermonth,a.qty AS thismonth, SUM(b.qty) AS total, CAST(AVG(b.qty) AS DECIMAL(5,2)) AS avg FROM emporders a INNER JOIN emporders b ON a.empid=b.empid AND b.ordermonth <= a.ordermonth GROUP BY a.empid,a.ordermonth,a.qty ORDER BY a.empid,a.ordermonth
如果只是查詢2015年的累積訂單,可以加上以where條件
WHERE DATE_FORMAT(a.ordermonth,'%Y')='2015' AND DATE_FORMAT(b.ordermonth,'%Y')='2015'
運(yùn)行結(jié)果如下
此外可能還需要篩選數(shù)據(jù),例如只需要返回每個(gè)員工到達(dá)某一目標(biāo)之前每月訂單的情況。這里假設(shè)統(tǒng)計(jì)每個(gè)員工的合計(jì)訂單數(shù)量達(dá)到1000之前的累積情況。
這里可以使用HAVING過(guò)濾器來(lái)完成查詢
SELECT a.empid, a.ordermonth,a.qty AS thismonth, SUM(b.qty) AS total, CAST(AVG(b.qty) AS DECIMAL(5,2)) AS avg FROM emporders a INNER JOIN emporders b ON a.empid=b.empid AND b.ordermonth <= a.ordermonth WHERE DATE_FORMAT(a.ordermonth,'%Y')='2015' AND DATE_FORMAT(b.ordermonth,'%Y')='2015' GROUP BY a.empid,a.ordermonth,a.qty HAVING total<1000 ORDER BY a.empid,a.ordermonth
這里并沒(méi)有統(tǒng)計(jì)到達(dá)到1000時(shí)該月的情況,如果要進(jìn)行統(tǒng)計(jì),則情況又有點(diǎn)復(fù)雜。如果指定了total <= 1000,則只有該月訂單數(shù)量正好為1000才進(jìn)行統(tǒng)計(jì),否則不會(huì)對(duì)該月進(jìn)行統(tǒng)計(jì)。因此這個(gè)問(wèn)題的過(guò)濾,可以從另外一個(gè)方面來(lái)考慮。當(dāng)累積累積訂單小于1000時(shí),累積訂單與上個(gè)月的訂單之差是小于1000的,同時(shí)也能對(duì)第一個(gè)訂單數(shù)量超過(guò)1000的月份進(jìn)行統(tǒng)計(jì)。故該解決方案的SQL語(yǔ)句如下
SELECT a.empid, a.ordermonth,a.qty AS thismonth, SUM(b.qty) AS total, CAST(AVG(b.qty) AS DECIMAL(5,2)) AS avg FROM emporders a INNER JOIN emporders b ON a.empid=b.empid AND b.ordermonth <= a.ordermonth WHERE DATE_FORMAT(a.ordermonth,'%Y')='2015' AND DATE_FORMAT(b.ordermonth,'%Y')='2015' GROUP BY a.empid,a.ordermonth,a.qty HAVING total-a.qty < 1000 ORDER BY a.empid,a.ordermonth
運(yùn)行結(jié)果如下
如果只想返回達(dá)到累積訂單數(shù)為1000的當(dāng)月數(shù)據(jù),不返回之前的月份,則可以對(duì)上述SQL語(yǔ)句
進(jìn)一步過(guò)濾,再添加累積訂單數(shù)量大于等于1000的條件。該問(wèn)題的SQL語(yǔ)句如下,
SELECT a.empid, a.ordermonth,a.qty AS thismonth, SUM(b.qty) AS total, CAST(AVG(b.qty) AS DECIMAL(5,2)) AS avg FROM emporders a INNER JOIN emporders b ON a.empid=b.empid AND b.ordermonth <= a.ordermonth WHERE DATE_FORMAT(a.ordermonth,'%Y')='2015' AND DATE_FORMAT(b.ordermonth,'%Y')='2015' GROUP BY a.empid,a.ordermonth,a.qty HAVING total-a.qty < 1000 AND total >= 1000 ORDER BY a.empid,a.ordermonth
運(yùn)行結(jié)果如下
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲(chǔ)過(guò)程技巧大全》及《MySQL數(shù)據(jù)庫(kù)鎖相關(guān)技巧匯總》
希望本文所述對(duì)大家MySQL數(shù)據(jù)庫(kù)計(jì)有所幫助。