迴圈統計截至每日歷史累計值(Python)

2021-10-08 18:13:51 字數 3816 閱讀 8962

select

date

,sum(if

(op_type =

'行為b'

,coin_amount,0)

)as loan_***

,sum(

if(op_type =

'行為b'

,coin_amount,

if(op_type =

'行為c'

,-coin_amount,

if(op_type =

'行為a'

,-coin_amount,0)

)))as cdp_debt_***

from

(select date_format(date_sub(curdate(),

interval

%s day),

'%%y-%%m-%%d')as

date

,'行為b'

as op_type

,loan_*** as coin_amount

from 表1

where

date

<= date_format(date_sub(curdate(),

interval

%s day),

'%%y-%%m-%%d'

)union

allselect date_format(date_sub(curdate(),

interval

%s day),

'%%y-%%m-%%d')as

date

,'行為a'

as op_type

,(lq_pay_***-fine_***)

as coin_amount

from 表2

where

date

<= date_format(date_sub(curdate(),

interval

%s day),

'%%y-%%m-%%d'

)union

allselect date_format(date_sub(curdate(),

interval

%s day),

'%%y-%%m-%%d')as

date

,'行為c'

as op_type

,repay_*** as coin_amount

from 表3

where

date

<= date_format(date_sub(curdate(),

interval

%s day),

'%%y-%%m-%%d'))r

group

bydate

# -*- coding: utf-8 -*-

"""created on fri jul 31 14:16:16 2020

@author: songyou han

"""# sql 查詢語句模板

sql_model =

''' select date

,sum(if(op_type = '行為b',coin_amount,0)) as loan_***

,sum(if(op_type = '行為b',coin_amount,if(op_type ='行為c',-coin_amount,if(op_type = '行為a',-coin_amount,0)))) as cdp_debt_***

from(

select date_format(date_sub(curdate(),interval %s day),'%%y-%%m-%%d') as date

,'行為b' as op_type

,loan_*** as coin_amount

from 表1

where date <= date_format(date_sub(curdate(),interval %s day),'%%y-%%m-%%d')

union all

select date_format(date_sub(curdate(),interval %s day),'%%y-%%m-%%d') as date

,'行為a' as op_type

,(lq_pay_***-fine_***) as coin_amount

from 表2

where date <= date_format(date_sub(curdate(),interval %s day),'%%y-%%m-%%d')

union all

select date_format(date_sub(curdate(),interval %s day),'%%y-%%m-%%d') as date

,'行為c' as op_type

,repay_*** as coin_amount

from 表3

where date <= date_format(date_sub(curdate(),interval %s day),'%%y-%%m-%%d')

)rgroup by date

'''import pymysql

# 開啟資料庫連線

source_db = pymysql.connect(

"10.0.0.40"

,"wayki-rw"

,"wayic@r19w"

,"bicoredata"

, charset=

'utf8'

)# destination_db = db = pymysql.connect("localhost", "root", "root", "experiment", charset='utf8' )

# 使用cursor()方法獲取操作游標

src_cursor = source_db.cursor(

)# des_cursor = destination_db.cursor()

for interval in

range

(284):

interval_list =

str(interval)

) interval_list = interval_list*

6 interval_list =

tuple

(interval_list)

try:

#保持資料庫連線

source_db.ping(reconnect=

true

)# 執行sql語句

src_cursor.execute(sql_model,interval_list)

# 獲取所有記錄列表

results = src_cursor.fetchall(

)for row in results:

date = row[0]

loan_cumulative =

format

(int

(row[1]

)/100000000

,'.1f'

) loan_current =

format

(int

(row[2]

)/100000000

,'.1f'

)# 列印結果

print

(date,loan_cumulative,loan_current)

except

:print

("error: unable to fecth data"

)# 關閉資料庫連線

source_db.close(

)

每日考勤統計

最近 一直在做公司的每日考勤這一塊,用的是mysql 在寫的時候遇到了不少的麻煩 其一在選取有效工作日時 attendance time 存的是考勤資料,atte holidays 這裡面存的是節假日的資料 我用了一條sql語句來查詢 select distinct date format tt.a...

Zabbix監控之每日告警統計報告

vi zabbix report.sh bin sh sword make 160518 array1 普通資訊 array2 警告資訊 array3 一般問題 array4 嚴重問題 array5 災難 set x now date s fix expr now 86400 now1 date d...

練習題 統計迴圈素數

題目內容 數字197可以被稱為迴圈素數,因為197的三個數字迴圈移位後的數字 197,971,719均為素數。100以內這樣的數字包括13個,2,3,5,7,11,13,17,31,37,71,73,79,97。要求任意正整數n以內一共有多少個這樣的迴圈素數。輸入格式 乙個正整數n。輸出格式 n以內...