[Tutor] Question on a select statement with ODBC
Al Bull
a.bull at pubdmgroup.com
Tue Oct 21 20:57:57 CEST 2014
Windows 7.0
Python 3.3.4
I am accessing a database table via ODBC. The table I am accessing can
have multiple records per ord_dbasub. Is there a way I can structure the
select statement to retrieve only the most current record (based on
ord_date)?
I used to program in PL/I and assembly in the old days but my knowledge of
SQL and Python is still pretty limited. Here is what I have so far..
import pyodbc
cnxn = pyodbc.connect("DSN=Quickfill DEMO Database")
cursor = cnxn.cursor()
ord_rows = cursor.execute("select ord_dbasub, ord_pub,
ord_date,ord_service,"
"ord_agency, ord_woa, ord_status,"
"ord_channel, ord_source, ord_giftcomp,"
"ord_cnreason "
"from ord "
"Where ord_pub='QWKFIL'"
"order by ord_dbasub, ord_date").fetchall()
for row in ord_rows:
# Print for testing. Remove later
print (row.ord_dbasub, row.ord_date, row.ord_pub)
# Add code here to find the most recent order per DBASUB and delete other
orders
# Also add exclusion code to be applied to survivor record
This code works so far. I just need to determine how to remove all but the
most current record per ord_dbasub. Assuming I can't remover it via the
select statement I'll have to traverse the array (Tuples in Python,
correct?) and remove the ones I don't need. What's the best way to do
that?
I have to admit that the concept of tuples & dictionaries has me a little
bit confused. I'm used to working with arrays and arrays of structures.
Thanks in advance for your help!
Al Bull, Chief Technology Officer/Owner
Publishers Data Management Group
a.bull at pubdmgroup.com
815-732-5297
More information about the Tutor
mailing list