本文實例講述了MySQL滑動訂單問題原理與解決方法。分享給大家供大家參考,具體如下:
創(chuàng)新互聯(lián)服務項目包括東阿網(wǎng)站建設、東阿網(wǎng)站制作、東阿網(wǎng)頁制作以及東阿網(wǎng)絡營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關系等,向廣大中小型企業(yè)、政府機構等提供互聯(lián)網(wǎng)行業(yè)的解決方案,東阿網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務的客戶以成都為中心已經(jīng)輻射到東阿省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!
先根據(jù)以下代碼來創(chuàng)建表MonthlyOrders并導入一定的數(shù)據(jù)
CREATE TABLE MonthlyOrders( ordermonth DATE, ordernum INT UNSIGNED, PRIMARY KEY (ordermonth) );
INSERT INTO MonthlyOrders SELECT '2010-02-01',23; INSERT INTO MonthlyOrders SELECT '2010-03-01',26; INSERT INTO MonthlyOrders SELECT '2010-04-01',24; INSERT INTO MonthlyOrders SELECT '2010-05-01',27; INSERT INTO MonthlyOrders SELECT '2010-06-01',26; INSERT INTO MonthlyOrders SELECT '2010-07-01',32; INSERT INTO MonthlyOrders SELECT '2010-08-01',34; INSERT INTO MonthlyOrders SELECT '2010-09-01',30; INSERT INTO MonthlyOrders SELECT '2010-10-01',31; INSERT INTO MonthlyOrders SELECT '2010-11-01',32; INSERT INTO MonthlyOrders SELECT '2010-12-01',33; INSERT INTO MonthlyOrders SELECT '2011-01-01',31; INSERT INTO MonthlyOrders SELECT '2011-02-01',34; INSERT INTO MonthlyOrders SELECT '2011-03-01',34; INSERT INTO MonthlyOrders SELECT '2011-04-01',38; INSERT INTO MonthlyOrders SELECT '2011-05-01',39; INSERT INTO MonthlyOrders SELECT '2011-06-01',35; INSERT INTO MonthlyOrders SELECT '2011-07-01',49; INSERT INTO MonthlyOrders SELECT '2011-08-01',56; INSERT INTO MonthlyOrders SELECT '2011-09-01',55; INSERT INTO MonthlyOrders SELECT '2011-10-01',74; INSERT INTO MonthlyOrders SELECT '2011-11-01',75; INSERT INTO MonthlyOrders SELECT '2011-12-01',14;
滑動訂單問題是指為每個月返回上一年度(季度或月度)的滑動訂單數(shù),即每個月份N,返回N-11到月份N的訂單總數(shù)。這里,假設月份序列中不存在間斷。
執(zhí)行下面的SQL查詢實現(xiàn)每個月返回上一年度的滑動訂單總數(shù)
SELECT DATE_FORMAT(a.ordermonth, '%Y%m') AS frommonth, DATE_FORMAT(b.ordermonth, '%Y%m') AS tomonth, SUM(c.ordernum) AS orders FROM monthlyorders a INNER JOIN monthlyorders b ON DATE_ADD(a.ordermonth, INTERVAL 11 MONTH) = b.ordermonth INNER JOIN monthlyorders c ON c.ordermonth BETWEEN a.ordermonth AND b.ordermonth GROUP BY a.ordermonth,b.ordermonth;
運行結果如下圖
該查詢首先對MonthlyOrders表進行自連接。a表用做下邊界(frommonth),b表用做上邊界(tomonth)。連接的條件為:
DATE_ADD(a.ordermonth, INTERVAL 11 MONTH) = b.ordermonth
例如,a表中的2010年2月將匹配2011年1月。
完成自連接之后,需要對訂單進行統(tǒng)計。這時需要再進行一次自連接,得到范圍內每個月的訂單數(shù)量。因此連接的條件為
c.ordermonth BETWEEN a.ordermonth AND b.ordermonth
基于上述方法,我們還可以統(tǒng)計每個季度訂單的情況,以此作為和同比增長的比較依據(jù)。
SELECT DATE_FORMAT(a.ordermonth, '%Y%m') AS frommonth, DATE_FORMAT(b.ordermonth, '%Y%m') AS tomonth, SUM(c.ordernum) AS orders FROM monthlyorders a INNER JOIN monthlyorders b ON DATE_ADD(a.ordermonth, INTERVAL 2 MONTH) = b.ordermonth AND MONTH(a.ordermonth) % 3 = 1 INNER JOIN monthlyorders c ON c.ordermonth BETWEEN a.ordermonth AND b.ordermonth GROUP BY a.ordermonth,b.ordermonth;
運行結果如下圖
更多關于MySQL相關內容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務操作技巧匯總》、《MySQL存儲過程技巧大全》及《MySQL數(shù)據(jù)庫鎖相關技巧匯總》
希望本文所述對大家MySQL數(shù)據(jù)庫計有所幫助。
分享標題:mysql滑動訂單問題原理與解決方法實例分析
URL地址:http://m.rwnh.cn/article22/jcjicc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、動態(tài)網(wǎng)站、服務器托管、商城網(wǎng)站、標簽優(yōu)化、Google
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)