From Data in the following Table Loan_Amendment:
ID | Amount | AddDeduct | AmendDate
-----------+----------------+-----------+------------
LT0000160 | 1000.000000000 | 2 | 2018-02-08
LT0000170 | 1500.00000000 | 1 | 2018-02-10
LN0000577 | 1000.000000000 | 1 | 2018-03-20
LN0000587 | 2000.000000000 | 2 | 2018-03-20
I tried to select sum Amount
field which have the same year and month of AmendDate
.
Therefore, I tried select query in SQLALchemy
as the following like this:
AmendObj = db.session.query(func.sum(Loan_Amendment.Amount).label('Amount'),
Loan_Amendment.AmendDate).\
group_by(Loan_Amendment.AmendDate).\
first()
However, from the above query I have tried I can only group by exact same date (year, month, and day) of AmendDate
.
Updated:
My model look like below:
class Loan_Amendment(Object):
ID = db.Column(db.String(13), primary_key=True)
AddDeduct = db.Column(db.String(1))
Amount = db.Column(db.Numeric(25, 9))
AmendDate = db.Column(db.String(20))
Currently, I' using Flask-SQLAlchemy==2.0
and SQLAlchemy==0.9.9
and DB type is postgresql
.
Which way I can select sum and group by only month and year from the AmendDate
?
postgresql
DB. Thanks – Houy Narun Apr 4 '18 at 9:05