views:

31

answers:

1

I need a database that support pivots to get pivot columns with sqlalchemy, something like that:

# find all possible values of the pivot
pivot_values = map(
    operator.itemgetter(0),
    select([pivot_on], from_obj=[report]).distinct().execute()
)

# build the new pivot columns
new_columns = [
    pivot_func(case([(pivot_on == value, column)])).label("%s %s" % (value, column))
    for value in pivot_values
    for column in pivot_columns
]

return select(
    non_pivot_columns + new_columns,
    from_obj=[report],
    group_by=group_by
)
A: 

I use the following recipe link text. I get the data and then pivot.