1

I am using SQLAlchemy for my projects and I run to a problem that requires a complex query. I got to admit SQL is not my strongest suit. My problem goes like this.

There are two tables Issues(parent) and Changes with one to many relation. The Changes have an entry "changed_on" and a foreign key "issue_id", as well as a "field" that was changed with "new" value.

(e.g. a Change can have issue_id = 1, field='status', new='closed' changed_on='25/01/2012' and issue_id = 1, field='status', new='resolved', changed_on='24/01/2012')

I want to count how many of these changes we have previous to an X date, but get only the one with the latest date(latest is relevant to the lookup date).

My code so far to get the count before some day X

for day in dates:
      q2 = Change.session.query(func.count(Change.id)).\
           filter(Change.changed_on <= day, Change.field == attr, Change.new == value).all()

This gets a correct count of the items of a particular field and value before 'day', but it does not take into account that the latest updated value.

I realize that in the filtering I would have to drop the Change.new == value but after that how do I filter it to get only the latest update of an X Change with a Y issue_id

EDIT :

Better way to do that would be for each day to get a dict like that

{
'01/04/2015': { 
           'open' :10, 
           'closed' : 15....}
    }

but some days an issue can go from new->closed, so we need to count only the closed ones(latest update)

1 Answer 1

1

You need to add group_by to your query in order to group by day and state. In SQL you need to use GROUP BY statement to count by fields.

    query(
        IssueChange.changed_on,
        Change.new,
        func.count(Change.id),
    ).filter(
        IssueChange.changed_on <= day,
        Change.field == attr,
        Change.new == value
    ).group_by(
        IssueChange.changed_on,
        Change.new 
    ).all()

And you will receive output like this:

[
 ('24/01/2012', 'resolved', 3),
 ('24/01/2012', 'closed', 2),
 ('25/01/2012', 'resolved', 5),
 ('25/01/2012', 'closed', 5)
]

added:

If you want one line per date you can use SQL subquery like:

SELECT parent.changed_on, 
    (SELECT COUNT(child.id) FROM change AS child WHERE child.changed_on = parent.changed_on AND child.new == 'resolved') AS Resolved,
    (SELECT COUNT(child.id) FROM change AS child WHERE child.changed_on = parent.changed_on AND child.new == 'closed') AS Closed
FROM change AS parent

But in SQLAlchemy it is quite tricky... (

3
  • Quite in the right way. Thanks! but that would double count the issues with the same issue_id, right? Is there any way to get all Change entries with same issue_id and count only the "newest" Mar 2, 2015 at 14:56
  • 1
    @MayTheSchwartzBeWithYou If you add issue_id to select and group you get count of particular issue: query(Change.Issue_id...).filter(Change.changed_on > '22/01/2012', ....).group_by(Change.issue_id, ...[other fields which in select])
    – denex
    Mar 3, 2015 at 9:52
  • a big thank you @denex. I solved it with data structures and your guidance. I will post solution tomorrow. Cheers Mar 4, 2015 at 23:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.