2

Does SQLALCHEMY has a function to be used in queries for array_length?

ie:

ssn.query(models.MedicationLog.dose_date,
                         func.array_length(models.MedicationLog.
                                    dose_taken_times).label('dose_taken_times'))
5
  • Do you mean length or array_agg? Here is a SO Post about how to use this. Commented May 26, 2016 at 5:45
  • Length. Since the count will not give length of the array. But let me check the aggregate. Thanks @AKS Commented May 26, 2016 at 5:48
  • 2
    array_length takes two arguments array_length(array, dimension) Commented May 26, 2016 at 5:53
  • @AKS Thanks. It works, only change I had to do was use max aggregate function as I dont have the dose_taken_times in the group by. Commented May 26, 2016 at 6:50
  • Please add the changes you made in the answer and accept it. Commented May 26, 2016 at 6:57

1 Answer 1

3

Fixed it with using max and array_length

data = ssn.query(models.MedicationLog.dose_date,
                         func.sum(func.array_length(
                             models.MedicationLog.dose_taken_times, ARRAY_DEPTH
                         )).label('dose_taken_times')).order_by(models.MedicationLog.dose_date).\
        group_by(models.MedicationLog.dose_date).all()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.