sql >> Databasteknik >  >> RDS >> PostgreSQL

Gruppera några resultat sql-fält i ett gemensamt objekt

Frågan nedan borde göra jobbet:

from sqlalchemy import func
# ...

query = (
    select(
        [
            func.json_build_object(
                "id",
                events.c.id,
                "title",
                events.c.title,
                "location",
                func.json_agg(
                    func.json_build_object(
                        "city",
                        locations.c.city,
                        "street",
                        locations.c.street,
                        "building",
                        locations.c.building,
                    )
                ),
                "location_all_columns_example",
                func.json_agg(func.json_build_object(
                    *itertools.chain(*[(_.name, _) for _ in locations.c])
                )),
                "activity",
                func.json_agg(
                    func.json_build_object(
                        "name",
                        activities.c.name,
                    )
                ),
            )
        ]
    )
    .select_from(events.join(locations).join(activities))
    .where(
        and_(
            events.c.id == pk,
            locations.c.id == events.c.location_id,
            activities.c.id == events.c.activities_id,
        )
    )
    .order_by(desc(events.c.created_at))
    .group_by(events.c.id)  # !!! <- IMPORTANT
)

Observera att du behöver group_by klausul.




  1. SQL:Binär till IP-adress

  2. Autoinkrement i liquibase

  3. IF-sats inuti where-sats i SQL

  4. MySQL antal objekt inom i klausul