(session, type)
return subquery
Creates a subquery for `type` that represents a combined save + repost count.

    For example, to get the tracks with the largest combined save and repost count, use:
        subquery = create_save_repost_count_subquery(session, 'track')
        session
            .query(tracks)
            .join(subquery, tracks.track_id = subquery.c.id)
            .order_by(desc(subquery.c.count))

    Args:
        session: SQLAlchemy session.
        type: (string) The type of save/repost (album, playlist, track)

    Returns: A subquery with two fields `id` and `count`.
Creates a subquery for `type` that represents a combined save + repost count.
Creates a subquery for type that represents a combined save + repost count .
def create_save_repost_count_subquery(session, type):
    """
    Creates a subquery for `type` that represents a combined save + repost count.

    For example, to get the tracks with the largest combined save and repost count, use:
        subquery = create_save_repost_count_subquery(session, 'track')
        session
            .query(tracks)
            .join(subquery, tracks.track_id = subquery.c.id)
            .order_by(desc(subquery.c.count))

    Args:
        session: SQLAlchemy session.
        type: (string) The type of save/repost (album, playlist, track)

    Returns: A subquery with two fields `id` and `count`.
    """
    # Get reposts by item id
    reposts_count_subquery = (
        session.query(
            Repost.repost_item_id,
        )
        .filter(
            Repost.is_current == True,
            Repost.is_delete == False,
            Repost.repost_type == type,
        )
        .subquery()
    )

    # Query saves joined against reposts grouped by id and calculate
    # a combined count for each
    subquery = (
        session.query(
            Save.save_item_id.label("id"),
            (
                func.count(Save.save_item_id)
                + func.count(reposts_count_subquery.c.repost_item_id)
            ).label("count"),
        )
        # Join against reposts filtering to matching ids.
        # Inner-join drops no-match ids.
        .join(
            reposts_count_subquery,
            Save.save_item_id == reposts_count_subquery.c.repost_item_id,
        )
        .filter(
            Save.is_current == True, Save.is_delete == False, Save.save_type == type
        )
        .group_by(Save.save_item_id)
        .subquery()
    )
    return subquery
def create_save_repost_count_subquery ( session , type ) : # Get reposts by item id reposts_count_subquery = ( session . query ( Repost . repost_item_id , ) . filter ( Repost . is_current == True , Repost . is_delete == False , Repost . repost_type == type , ) . subquery ( ) ) # Query saves joined against reposts grouped by id and calculate # a combined count for each subquery = ( session . query ( Save . save_item_id . label ( "id" ) , ( func . count ( Save . save_item_id ) + func . count ( reposts_count_subquery . c . repost_item_id ) ) . label ( "count" ) , ) # Join against reposts filtering to matching ids. # Inner-join drops no-match ids. . join ( reposts_count_subquery , Save . save_item_id == reposts_count_subquery . c . repost_item_id , ) . filter ( Save . is_current == True , Save . is_delete == False , Save . save_type == type ) . group_by ( Save . save_item_id ) . subquery ( ) ) return subquery