Skip to content

Instantly share code, notes, and snippets.

@SantjagoCorkez
Created April 24, 2015 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SantjagoCorkez/db207a7b533d1d6f05ae to your computer and use it in GitHub Desktop.
Save SantjagoCorkez/db207a7b533d1d6f05ae to your computer and use it in GitHub Desktop.
state_column = case([
(OrderLog.state.in_([
Order.State.cc_declined, Order.State.cc_declined_after_call_interested,
Order.State.cc_declined_after_call_not_interested,
Order.State.cc_declined_before_call_interested,
Order.State.cc_declined_before_call_not_interested
]), 'refuses'),
(OrderLog.state.in_([
Order.State.cc_error
]), 'errors'),
(OrderLog.state.in_([
Order.State.cc_not_available
]), 'no_answer'),
(OrderLog.state.in_([
Order.State.cc_delayed
]), 'call_later'),
(OrderLog.state.in_([
Order.State.cc_confirmed
]), 'success')
], else_='unknown').label('state')
data = Session.query(
count(OrderLog.id).label('count'),
# OrderLog.state.label('state'),
state_column,
sa_sum(case([(Order.state.in_([
Order.State.cc_declined, Order.State.cc_declined_after_call_interested,
Order.State.cc_declined_after_call_not_interested,
Order.State.cc_declined_before_call_interested,
Order.State.cc_declined_before_call_not_interested,
Order.State.cc_error]), 1)], else_=0)).label('refused'),
group_raw_column,
group_column,
)
if group_join_tables and group_join_clauses:
for i in zip(group_join_tables, group_join_clauses):
data = data.outerjoin(*i)
data = data.filter(
OrderLog.order_id == Order.id,
OrderLog.state.in_([
Order.State.cc_confirmed, Order.State.cc_not_available, Order.State.cc_error, Order.State.cc_delayed,
Order.State.cc_declined_before_call_not_interested, Order.State.cc_declined_before_call_interested,
Order.State.cc_declined_after_call_not_interested, Order.State.cc_declined_after_call_interested,
Order.State.cc_declined
]),
*filters
).group_by(state_column, '"raw_key"', '"key"')
"""
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) column reference "state" is ambiguous
LINE 3: ..._logs.operator_id IN (42, 46, 120, 133)) GROUP BY state, "ra...
^
[SQL: 'SELECT count(order_logs.id) AS count, CASE WHEN (order_logs.state IN (%(state_1)s, %(state_2)s, %(state_3)s, %(state_4)s, %(state_5)s)) THEN %(param_1)s WHEN (order_logs.state IN (%(state_6)s)) THEN %(param_2)s WHEN (order_logs.state IN (%(state_7)s)) THEN %(param_3)s WHEN (order_logs.state IN (%(state_8)s)) THEN %(param_4)s WHEN (order_logs.state IN (%(state_9)s)) THEN %(param_5)s ELSE %(param_6)s END AS state, sum(CASE WHEN (orders.state IN (%(state_10)s, %(state_11)s, %(state_12)s, %(state_13)s, %(state_14)s, %(state_15)s)) THEN %(param_7)s ELSE %(param_8)s END) AS refused, order_logs.ts_spawn - (order_logs.ts_spawn - %(ts_spawn_1)s) %% %(param_9)s AS raw_key, order_logs.ts_spawn - (order_logs.ts_spawn - %(ts_spawn_1)s) %% %(param_9)s AS key \nFROM order_logs, orders \nWHERE order_logs.order_id = orders.id AND order_logs.state IN (%(state_16)s, %(state_17)s, %(state_18)s, %(state_19)s, %(state_20)s, %(state_21)s, %(state_22)s, %(state_23)s, %(state_24)s) AND order_logs.ts_spawn >= %(ts_spawn_2)s AND order_logs.ts_spawn < %(ts_spawn_3)s AND ((order_logs.flags & %(flags_1)s) > %(param_10)s OR order_logs.operator_id IN (%(operator_id_1)s, %(operator_id_2)s, %(operator_id_3)s, %(operator_id_4)s)) GROUP BY state, "raw_key", "key"'] [parameters: {'param_6': 'unknown', 'state_20': 1201, 'state_9': 1999, 'state_8': 1002, 'param_3': 'no_answer', 'state_1': 1003, 'state_3': 1203, 'state_2': 1202, 'state_5': 1201, 'state_4': 1200, 'state_7': 1001, 'state_6': 1100, 'state_15': 1100, 'state_14': 1201, 'state_17': 1001, 'state_16': 1999, 'state_11': 1202, 'state_10': 1003, 'ts_spawn_2': 1429822800, 'ts_spawn_3': 1429909200, 'state_19': 1002, 'state_18': 1100, 'state_23': 1202, 'param_10': 0, 'flags_1': 1, 'param_1': 'refuses', 'ts_spawn_1': -10800, 'param_9': 3600, 'state_13': 1200, 'state_24': 1003, 'operator_id_1': 42, 'param_7': 1, 'operator_id_3': 120, 'operator_id_4': 133, 'state_21': 1200, 'state_22': 1203, 'param_2': 'errors', 'param_5': 'success', 'param_4': 'call_later', 'param_8': 0, 'state_12': 1203, 'operator_id_2': 46}]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment