简体   繁体   中英

postgres: COUNT, DISTINCT is not implemented for window functions

I am trying to use COUNT(DISTINC column) OVER(PARTITION BY column) when I am using COUNT + window function(OVER) . I get an error like the one in the title and can't get it to work.

I have looked into how to deal with this error, but I have not found an example of how to deal with such a complex query as the one below. I cannot find an example of how to deal with such a complex query as shown below, and I am not sure how to handle it.

The COUNT part of the problem exists on line 65. How can such a complex query be resolved without slowing down?

WITH RECURSIVE "cte" AS((
        SELECT
            "videos_productvideocomment"."id",
            "videos_productvideocomment"."user_id",
            "videos_productvideocomment"."video_id",
            "videos_productvideocomment"."parent_id",
            "videos_productvideocomment"."text",
            "videos_productvideocomment"."commented_at",
            "videos_productvideocomment"."edited_at",
            "videos_productvideocomment"."created_at",
            "videos_productvideocomment"."updated_at",
            "videos_productvideocomment"."id" AS "root_id"
        FROM
            "videos_productvideocomment"
        WHERE
            (
                "videos_productvideocomment"."parent_id" IS NULL
            AND "videos_productvideocomment"."video_id" = 'f264433c-c0af-49cc-8b40-84453da71b2d'
            )
    ) UNION(
    SELECT
        "videos_productvideocomment"."id",
        "videos_productvideocomment"."user_id",
        "videos_productvideocomment"."video_id",
        "videos_productvideocomment"."parent_id",
        "videos_productvideocomment"."text",
        "videos_productvideocomment"."commented_at",
        "videos_productvideocomment"."edited_at",
        "videos_productvideocomment"."created_at",
        "videos_productvideocomment"."updated_at",
        "cte"."root_id" AS "root_id"
    FROM
        "videos_productvideocomment"
        INNER JOIN
            "cte"
        ON  "videos_productvideocomment"."parent_id" = "cte"."id"
))
SELECT
    *,
    EXISTS(
        SELECT
            (1) AS "a"
        FROM
            "videos_productvideolikecomment" U0
        WHERE
            (
                U0."comment_id" = t."id"
            AND U0."user_id" = '3bd3bc86-0335-481e-9fd2-eb2fb1168f48'
            )
        LIMIT 1
    ) AS "liked"
FROM
    (
        SELECT DISTINCT
            "cte"."id",
            "cte"."created_at",
            "cte"."updated_at",
            "cte"."user_id",
            "cte"."text",
            "cte"."commented_at",
            "cte"."edited_at",
            "cte"."parent_id",
            "cte"."video_id",
            "cte"."root_id" AS "root_id",
            COUNT(DISTINCT "cte"."root_id") OVER(PARTITION BY "cte"."root_id") AS "reply_count", <--- here
            COUNT("videos_productvideolikecomment"."id") OVER(PARTITION BY "cte"."id") AS "liked_count"
        FROM
            "cte"
            LEFT OUTER JOIN
                "videos_productvideolikecomment"
            ON  (
                    "cte"."id" = "videos_productvideolikecomment"."comment_id"
                )
    ) t
WHERE
    t."id" = t."root_id"
ORDER BY
    CASE
        WHEN t."user_id" = '3bd3bc86-0335-481e-9fd2-eb2fb1168f48' THEN 0
        ELSE 1
    END ASC,
    "liked_count" DESC

DISTINCT will look for duplicates and remove it, but in big data it will take a lot of time to process this query, you should process the middle of the record in the programming part I think it will be fast than. Thank

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM