Skip to content

Instantly share code, notes, and snippets.

@inklesspen
Created January 6, 2016 18:35
Show Gist options
  • Save inklesspen/01b5aa34c4c76112ca26 to your computer and use it in GitHub Desktop.
Save inklesspen/01b5aa34c4c76112ca26 to your computer and use it in GitHub Desktop.
for drawks
# http://stackoverflow.com/questions/16172084/postgresql-join-only-most-specific-cidr-match/16172369#16172369
subselect = sa.select([
test_systems.c.address.label('hostaddr'),
test_networks.c.address.label('netaddr'),
test_networks.c.description.label('description'),
sa.func.row_number().over(partition_by=test_systems.c.address, order_by=sa.func.masklen(test_networks.c.address).desc()).label('row')
], from_obj=test_systems.outerjoin(test_networks, test_systems.c.address.op('<<')(test_networks.c.address))).alias('x')
query = sa.select([
x.c.hostaddr,
x.c.netaddr,
x.c.description
]).where(x.c.row == 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment