1

I am trying to convert an SQL to sqlalchemy.

SELECT * FROM (SELECT user_id, GROUP_CONCAT(distinct product_code) as prod FROM purchase_detail 
GROUP BY user_id )  tab WHERE tab.prod ='000'

With the information in this link I tried to come up with this.

db_session.query(PurchaseDetail.user_id, 
           func.group_concat(PurchaseDetail.product_code.label("code")).
           group_by(PurchaseDetail.user_id).filter_by(code='000')

This obviously throws an error saying purchase_detail doesn't have a column called code. I kind of knew the label would not work here. How can I can give an alias to that grouped column and filter on that column?

Community
  • 1
  • 1
user3527975
  • 1,683
  • 8
  • 25
  • 43

0 Answers0