looping and grouping over a query

27 views
Skip to first unread message

Hugo Ahlenius

unread,
Aug 18, 2010, 4:49:06 AM8/18/10
to pyodbc
Hi,

I am a bit of a dirty hack in python, so please play gently with me!

I am using pyodbc to retrieve data from an Access Database, and
looping over the results

for row in cursor:
(do something)

Which works well. But I would also like to perform an inner loop over
records grouped by the values of a (sorted) column.

Imagine that the query data looks like:
UserID|Name|Project
1|John|A
1|John|B
2|Jane|A
2|Jane|C
3|Phil|A

Would there be an easy/smooth way of performing an inner loop over the
'name' column in the records.

pseudo code:
for row in cursor:
print row.Name
for (group over Name):
print row.Project

Gerald Britton

unread,
Aug 18, 2010, 3:49:19 PM8/18/10
to pyo...@googlegroups.com
One idea might be to specify ORDER BY in your SQL query and include
the Name column in the appropriate place. Then you could simply start
your inner loop on every name change.

> --
> You received this message because you are subscribed to the Google Groups "pyodbc" group.
> To post to this group, send email to pyo...@googlegroups.com.
> To unsubscribe from this group, send email to pyodbc+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pyodbc?hl=en.
>
>

--
Gerald Britton

Reply all
Reply to author
Forward
0 new messages