python - SQLAlchemy: How to group by two fields and filter by date -


Then I have a datestamp and a table with two fields that I need to ensure they are unique in the previous month. .

  table.id table.datestamp table.field1 table.field2  

There should not be a duplicate record with the previous field 1 + 2

The steps given in my head are:

  1. Group by two areas
  2. To ensure that the previous month's data, note that this unique group is not Happen.

I have found this far, but I do not think it works:

  result = session.key (table) .group_by ( \ Table.field1, table.field2, func.month (table.timestamp))  

But I am unsure of how to do this in the sqlalchemy can someone advise me?

Thanks a lot!

Also see the right direction, inline annotations:

  qry = (Session.query1 (table.c.field1, table.c.field2, ##Sprightime * works on sqlite for year-month; # @Todo: Proper work for mysql (as it is in question) # It is also unclear that only part of the month is sufficient, so that May be included in May 2001 and May -2009, or FREC should be used on YEAR-MONTH .strftime ('% Y-% m', Table.c.datestamp), func.count (),) # Optionally only in the last 2 months Check the data of Heine (may be partial month). Filter (Table.campstumps & lt; datetime .date.today () - datetime.timedelta (60)) .group_by (table.c.field1, table.c Field2, func.strftime ('% y-% m', table.code.) Amp), # comment out of this line to see all the groups. (Func.count ()> 1))  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -