CData Python Connector for Zoho Books

Build 22.0.8479

Aggregate Functions

func モジュールを使用して、SQLAlchemy 内で特定の集計関数を使用することもできます。まず、それをインポートする必要があります。

from sqlalchemy.sql import func

インポートすると、次の集計機能が利用可能になります。

COUNT

この例では、セッションオブジェクトのquery() メソッドを使用して一連のグループのレコード数をカウントします。

rs = session.query(func.count(INVOICES.Id).label("CustomCount"), INVOICES.InvoiceId).group_by(INVOICES.InvoiceId)
for instance in rs:
	print("Count: ", instance.CustomCount)
	print("InvoiceId: ", instance.InvoiceId)
	print("---------")

あるいは、セッションオブジェクトのexecute() メソッドを使用してCOUNT を実行することもできます。

rs = session.execute(INVOICES_table.select().with_only_columns([func.count(INVOICES_table.c.Id).label("CustomCount"), INVOICES_table.c.InvoiceId])group_by(INVOICES_table.c.InvoiceId))
for instance in rs:

SUM

この例では、一連のグループの数値カラムの累積を計算します。

rs = session.query(func.sum(INVOICES.AnnualRevenue).label("CustomSum"), INVOICES.InvoiceId).group_by(INVOICES.InvoiceId)
for instance in rs:
	print("Sum: ", instance.CustomSum)
	print("InvoiceId: ", instance.InvoiceId)
	print("---------")

あるいは、セッションオブジェクトのexecute() メソッドを使用してSUM を呼び出すこともできます。

rs = session.execute(INVOICES_table.select().with_only_columns([func.sum(INVOICES_table.c.AnnualRevenue).label("CustomSum"), INVOICES_table.c.InvoiceId]).group_by(INVOICES_table.c.InvoiceId))
for instance in rs:

AVG

この例では、セッションオブジェクトのquery() メソッドを使用して一連のグループの数値カラムの平均値を計算します。

rs = session.query(func.avg(INVOICES.AnnualRevenue).label("CustomAvg"), INVOICES.InvoiceId).group_by(INVOICES.InvoiceId)
for instance in rs:
	print("Avg: ", instance.CustomAvg)
	print("InvoiceId: ", instance.InvoiceId)
	print("---------")

あるいは、セッションオブジェクトのexecute() メソッドを使用してAVG を呼び出すこともできます。

rs = session.execute(INVOICES_table.select().with_only_columns([func.avg(INVOICES_table.c.AnnualRevenue).label("CustomAvg"), INVOICES_table.c.InvoiceId]).group_by(INVOICES_table.c.InvoiceId))
for instance in rs:

MAX およびMIN

この例では、一連のグループの数値カラムの最大値および最小値を検索します。

rs = session.query(func.max(INVOICES.AnnualRevenue).label("CustomMax"), func.min(INVOICES.AnnualRevenue).label("CustomMin"), INVOICES.InvoiceId).group_by(INVOICES.InvoiceId)
for instance in rs:
	print("Max: ", instance.CustomMax)
	print("Min: ", instance.CustomMin)
	print("InvoiceId: ", instance.InvoiceId)
	print("---------")

あるいは、セッションオブジェクトのexecute() メソッドを使用してMAX やMIN を呼び出すこともできます。

rs = session.execute(INVOICES_table.select().with_only_columns([func.max(INVOICES_table.c.AnnualRevenue).label("CustomMax"), func.min(INVOICES_table.c.AnnualRevenue).label("CustomMin"), INVOICES_table.c.InvoiceId]).group_by(INVOICES_table.c.InvoiceId))
for instance in rs:

Copyright (c) 2023 CData Software, Inc. - All rights reserved.
Build 22.0.8479