您好,登錄后才能下訂單哦!
這篇文章給大家介紹SQL對數(shù)據(jù)進(jìn)行按月統(tǒng)計或?qū)?shù)據(jù)進(jìn)行按星期統(tǒng)計的代碼怎么編寫,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
對于所有的需求,當(dāng)你不知道怎么處理的時候,你就先用最簡單的方法,或者說的明白一點,用最原始的方法,先實現(xiàn)業(yè)務(wù)需求再說?! ?/p>
一、對提現(xiàn)隊列數(shù)據(jù)表“ims_checkout_task”進(jìn)行匯總統(tǒng)計,按月匯總統(tǒng)計每個月的提現(xiàn)總額,提現(xiàn)總次數(shù)。
1、SQL操作如下:
SELECT id,SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 11 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '0',SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 10 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '1',SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 9 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '2',SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 8 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '3',SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 7 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '4',SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 6 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '5',SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 5 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '6',SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 4 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '7',SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 3 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '8',SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 2 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '9',SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '10',SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 0 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '11'FROM ims_checkout_task
2、數(shù)據(jù)庫返回如下:
3、關(guān)鍵詞:case when
//流程控制語句case語法,例如,如果sex字段值為1,則輸出男;如果sex值為2,則輸出女;否則輸出其他CASE sexWHEN '1' THEN '男'WHEN '2' THEN '女'ELSE '其他' END//所以上面的SQL為,如果條件成立,則輸出提現(xiàn)金額money字段,否則輸出0.
時間處理
//對時間戳格式化成 2018-10FROM_UNIXTIME(addTime,'%Y-%m')//SQL獲取當(dāng)前時間格式 2019-08 ,根據(jù)expr值不同,依次獲取前一個月1,前兩個月2 ···DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 0 MONTH),'%Y-%m')// 函數(shù)用于以不同的格式顯示日期/時間數(shù)據(jù)。DATE_FORMAT(date,format)//函數(shù)從日期減去指定的時間間隔。DATE_SUB(date,INTERVAL expr type)//函數(shù)返回當(dāng)前的日期。CURDATE()
例如
SELECT NOW(),CURDATE(),CURTIME()
例如
SELECT DAYOFWEEK(NOW()),WEEKDAY(now()),DATE_FORMAT(NOW(),"%w"),NOW()
二、對積分訂單數(shù)據(jù)表按周匯總統(tǒng)計訂單量,比如今天是周二,返回周一到周二的每天單量匯總數(shù)據(jù),依次類推
1、SQL操作如下:
SELECT id,SUM(case when DATE_FORMAT(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),'%w') = 1 AND WEEK(date_add(curdate(),interval 6 day),2) = WEEK(date_add(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),interval 6 day),2) AND `pay_status` = 1 then 1 else 0 end) as '0',SUM(case when DATE_FORMAT(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),'%w') = 2 AND WEEK(date_add(curdate(),interval 6 day),2) = WEEK(date_add(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),interval 6 day),2) AND `pay_status` = 1 then 1 else 0 end) as '1',SUM(case when DATE_FORMAT(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),'%w') = 3 AND WEEK(date_add(curdate(),interval 6 day),2) = WEEK(date_add(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),interval 6 day),2) AND `pay_status` = 1 then 1 else 0 end) as '2',SUM(case when DATE_FORMAT(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),'%w') = 4 AND WEEK(date_add(curdate(),interval 6 day),2) = WEEK(date_add(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),interval 6 day),2) AND `pay_status` = 1 then 1 else 0 end) as '3',SUM(case when DATE_FORMAT(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),'%w') = 5 AND WEEK(date_add(curdate(),interval 6 day),2) = WEEK(date_add(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),interval 6 day),2) AND `pay_status` = 1 then 1 else 0 end) as '4',SUM(case when DATE_FORMAT(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),'%w') = 6 AND WEEK(date_add(curdate(),interval 6 day),2) = WEEK(date_add(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),interval 6 day),2) AND `pay_status` = 1 then 1 else 0 end) as '5',SUM(case when DATE_FORMAT(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),'%w') = 0 AND WEEK(date_add(curdate(),interval 6 day),2) = WEEK(date_add(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),interval 6 day),2) AND `pay_status` = 1 then 1 else 0 end) as '6'FROM ims_integral_order
2、數(shù)據(jù)庫返回如下:
3、關(guān)鍵詞
//格式化時間戳,返回星期數(shù),注意周日返回值為0DATE_FORMAT(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),'%w')//返回當(dāng)前時間為一年中第幾周WEEK(DATE_ADD(CURDATE(),interval 6 day),2)//獲取指定日期是一年中的第幾周WEEK(date,mode)//函數(shù)向日期添加指定的時間間隔。DATE_ADD(date,INTERVAL expr type)//所以上面的查詢條件為星期和第幾周同時滿足
關(guān)于SQL對數(shù)據(jù)進(jìn)行按月統(tǒng)計或?qū)?shù)據(jù)進(jìn)行按星期統(tǒng)計的代碼怎么編寫就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。