如何查詢數(shù)據(jù)庫內(nèi)歷史同期和總計(jì)腳本,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)涿州免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了1000多家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
需求一:統(tǒng)計(jì)當(dāng)前及歷史同期業(yè)務(wù)量并在一個(gè)字段內(nèi)展示;
需求二:統(tǒng)計(jì)指定時(shí)間段內(nèi)業(yè)務(wù)總量;
本文以水果銷量模擬真實(shí)的業(yè)務(wù)量
統(tǒng)計(jì)近7天內(nèi)和歷史同期各水果銷量
統(tǒng)計(jì)2019年全年各水果銷量總計(jì)
#!/usr/bin/ksh #運(yùn)行示例:sh fru_calc_day.sh #本腳本用于統(tǒng)計(jì)每日及同期歷史的水果銷量 #統(tǒng)計(jì)時(shí)間從前D1天開始,默認(rèn)為7,即統(tǒng)計(jì)從7天前開始 D1=7 #統(tǒng)計(jì)時(shí)間到前D2天,默認(rèn)為1,即截止到前一天 D2=1 #歷史數(shù)據(jù),默認(rèn)為12,即前12個(gè)月(去年) M=12 #日志時(shí)間格式 filedate=`date +"%Y%m%d%H%M"` #日志名 REPORT="/tmp/fru/report$filedate.log" #數(shù)據(jù)庫用戶名/密碼,根據(jù)實(shí)際情況填寫 DBUSER=dbuser DBPASSWD=password #數(shù)據(jù)庫環(huán)境變量,根據(jù)實(shí)際情況填寫 export ORACLE_SID=mydb export ORACLE_BASE=/oracle/app/10.2.0 export ORACLE_HOME=$ORACLE_BASE/db_1 export PATH=$ORACLE_HOME/bin:$ORACLE_HOME/OPatch:$PATH export NLS_LANG="SIMPLIFIED Chinese_CHINA.ZHS16GBK" FRU_CALC() { echo "***************************每日水果銷量統(tǒng)計(jì)***************************">> $REPORT sqlplus -s /nolog << EOS | sed '1d' | sed '/rows selected/d' conn $DBUSER/$DBPASSWD set linesize 999 set pages 999 col 日期 format a30 col 蘋果(歷史同期) format a30 col 梨子(歷史同期) format a30 col 香蕉(歷史同期) format a30 col 橙子(歷史同期) format a30 select f.day as "日期", f.app as "蘋果(歷史同期)", f.pea as "梨子(歷史同期)", f.ban as "香蕉(歷史同期)", f.ora as "橙子(歷史同期)" from (select t.day, t.app || '(' || y.app || ')' as app, t.pea || '(' || y.pea || ')' as pea, t.ban || '(' || y.ban || ')' as ban, t.ora || '(' || y.ora || ')' as ora from (select a.day, a.apple as app, a.pear as pea, a.banana as ban, a.orange as ora from fruits a where a.day between TRUNC(sysdate - $D1, 'DD') and TRUNC(sysdate - $D2, 'DD')) t, (select a.day, a.apple as app, a.pear as pea, a.banana as ban, a.orange as ora from fruits a where a.day between TRUNC(ADD_MONTHS(sysdate, -$M) - $D1, 'DD') and TRUNC(ADD_MONTHS(sysdate, -$M) - $D2, 'DD')) y where ADD_MONTHS(t.day, -$M) = y.day) f order by f.day; quit EOS } FRU_CALC >>$REPORT 2>&1 echo "*****************************end*****************************">> $REPORT
本文腳本的執(zhí)行shell為/usr/bin/ksh,可根據(jù)實(shí)際情況修改,運(yùn)行命令echo $SHELL可查看當(dāng)前用戶的shell類型。
#!/usr/bin/ksh #運(yùn)行示例:sh fru_calc_sum.sh 20190101 20191231 #本腳本用于統(tǒng)計(jì)月度、年度或任意時(shí)間段內(nèi)水果銷量匯總 #參數(shù)1,開始時(shí)間 startdate="$1" #參數(shù)2,結(jié)束時(shí)間 enddate="$2" #日志時(shí)間格式 filedate=`date +"%Y%m%d%H%M"` #日志名 REPORT="/tmp/fru/report$filedate.log" #數(shù)據(jù)庫用戶名/密碼,根據(jù)實(shí)際情況填寫 DBUSER=dbuser DBPASSWD=password #數(shù)據(jù)庫環(huán)境變量,根據(jù)實(shí)際情況填寫 export ORACLE_SID=mydb export ORACLE_BASE=/oracle/app/10.2.0 export ORACLE_HOME=$ORACLE_BASE/db_1 export PATH=$ORACLE_HOME/bin:$ORACLE_HOME/OPatch:$PATH export NLS_LANG="SIMPLIFIED Chinese_CHINA.ZHS16GBK" FRU_CALC() { echo "***********歷史水果銷量總計(jì):'$startdate'至'$enddate'***********">> $REPORT sqlplus -s /nolog << EOS | sed '1d' | sed '/rows selected/d' conn $DBUSER/$DBPASSWD set linesize 999 set pages 999 select sum(f.app) as "蘋果", sum(f.pea) as "梨子", sum(f.ban) as "香蕉", sum(f.ora) as "橙子" from (select a.day, sum(a.apple) as app, sum(a.pear) as pea, sum(a.banana) as ban, sum(a.orange) as ora from fruits a where a.day between to_date('$startdate', 'yyyymmdd') and to_date('$enddate', 'yyyymmdd') group by a.day) f; quit EOS } FRU_CALC >>$REPORT 2>&1 echo "*****************************end*****************************">> $REPORT
統(tǒng)計(jì)20190101至 20191231水果銷量匯總。
關(guān)于如何查詢數(shù)據(jù)庫內(nèi)歷史同期和總計(jì)腳本問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道了解更多相關(guān)知識(shí)。