Given:
- a number
- a SQL Alchemy table (
<class 'sqlalchemy.sql.schema.Table'>) - and a list (that can vary in length) of SQL Alchemy columns (
<class 'sqlalchemy.sql.elements.ColumnClause'>)
I need to build a boolean mask that is True on the records where the row-wise sum of the columns is equal to the number.
Example:
- number = 6
- column_list = [sa.column(col_1), sa.column(col_2), sa.column(col_3)]
- table:
| col_1 | col_2 | col_3 | col_4 |
|---|---|---|---|
| 1 | 2 | 3 | 3 |
| 2 | 0 | 2 | 8 |
| 6 | 0 | 0 | 1 |
Returns: [True, False, True]
Caveat: The computation must be performed prior to executing the query (e.g. in the where clause, case statement, etc), not after performing fetchall or similar.
I'm not even sure where to start. I know I can select the columns:
sa.select(column_list).select_from(table)
Also, a case statement will probably be needed:
sa.case([(ROW_WISE_SUM == number, True)], else=False)
Edit: I think I'm getting closer but I still can't figure out how to handle the list of column clauses:
sa.case((sa.func.sum(column_list) == number, True), else_=False)
psycopg2.ProgrammingError: can't adapt type 'ColumnClause'
.
.
.
[SQL: CASE WHEN (sum(%(sum_1)s) = %(sum_2)s) THEN %(param_1)s ELSE %(param_2)s END]
[parameters: {'sum_1': [<sqlalchemy.sql.elements.ColumnClause at 0x7fe62bf075d0; col_1>, <sqlalchemy.sql.elements.ColumnClause at 0x7fe62bf6e090; col_2>, <sqlalchemy.sql.elements.ColumnClause at 0x7fe62b0fabd0; col_3>], 'sum_2': 6, 'param_1': True, 'param_2': False}]
(Background on this error at: http://sqlalche.me/e/14/f405)