如何查詢數(shù)據(jù)庫內(nèi)歷史同期和總計(jì)腳本,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎ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í)。
當(dāng)前名稱:如何查詢數(shù)據(jù)庫內(nèi)歷史同期和總計(jì)腳本-創(chuàng)新互聯(lián)
鏈接URL:http://chinadenli.net/article34/eodse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)站建設(shè)、營(yíng)銷型網(wǎng)站建設(shè)、ChatGPT、動(dòng)態(tài)網(wǎng)站、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容