CData Python Connector for PostgreSQL

Build 23.0.8839

Aggregate Functions

func モジュールを使用して、SQLAlchemy 内で特定の集計関数を使用することもできます。

このモジュールをインポートするには、以下を実行します。

from sqlalchemy.sql import func

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

COUNT

以下の例では、セッションオブジェクトのquery() メソッドを使用して一連のグループのレコード数をカウントします。
rs = session.query(func.count("postgres"."schema01".Orders.Id).label("CustomCount"), "postgres"."schema01".Orders.ShipName).group_by("postgres"."schema01".Orders.ShipName)
for instance in rs:
	print("Count: ", instance.CustomCount)
	print("ShipName: ", instance.ShipName)
	print("---------")

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

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

SUM

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

rs = session.query(func.sum("postgres"."schema01".Orders.Freight).label("CustomSum"), "postgres"."schema01".Orders.ShipName).group_by("postgres"."schema01".Orders.ShipName)
for instance in rs:
	print("Sum: ", instance.CustomSum)
	print("ShipName: ", instance.ShipName)
	print("---------")

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

rs = session.execute("postgres"."schema01".Orders_table.select().with_only_columns([func.sum("postgres"."schema01".Orders_table.c.Freight).label("CustomSum"), "postgres"."schema01".Orders_table.c.ShipName]).group_by("postgres"."schema01".Orders_table.c.ShipName))
for instance in rs:

AVG

この例では、セッションオブジェクトのquery() メソッドを使用して一連のグループの数値カラムの平均値を計算します。
rs = session.query(func.avg("postgres"."schema01".Orders.Freight).label("CustomAvg"), "postgres"."schema01".Orders.ShipName).group_by("postgres"."schema01".Orders.ShipName)
for instance in rs:
	print("Avg: ", instance.CustomAvg)
	print("ShipName: ", instance.ShipName)
	print("---------")

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

rs = session.execute("postgres"."schema01".Orders_table.select().with_only_columns([func.avg("postgres"."schema01".Orders_table.c.Freight).label("CustomAvg"), "postgres"."schema01".Orders_table.c.ShipName]).group_by("postgres"."schema01".Orders_table.c.ShipName))
for instance in rs:

MAX およびMIN

この例では、一連のグループの数値カラムの最大値および最小値を検索します。
rs = session.query(func.max("postgres"."schema01".Orders.Freight).label("CustomMax"), func.min("postgres"."schema01".Orders.Freight).label("CustomMin"), "postgres"."schema01".Orders.ShipName).group_by("postgres"."schema01".Orders.ShipName)
for instance in rs:
	print("Max: ", instance.CustomMax)
	print("Min: ", instance.CustomMin)
	print("ShipName: ", instance.ShipName)
	print("---------")

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

rs = session.execute("postgres"."schema01".Orders_table.select().with_only_columns([func.max("postgres"."schema01".Orders_table.c.Freight).label("CustomMax"), func.min("postgres"."schema01".Orders_table.c.Freight).label("CustomMin"), "postgres"."schema01".Orders_table.c.ShipName]).group_by("postgres"."schema01".Orders_table.c.ShipName))
for instance in rs:

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 23.0.8839