簡體   English   中英

postgres: COUNT, DISTINCT 沒有為 window 函數實現

[英]postgres: COUNT, DISTINCT is not implemented for window functions

當我使用COUNT + window function(OVER)時,我正在嘗試使用COUNT(DISTINC column) OVER(PARTITION BY column) ) 。 我收到一個類似標題中的錯誤,無法正常工作。

我已經研究過如何處理這個錯誤,但我還沒有找到如何處理下面這樣一個復雜查詢的示例。 我找不到如何處理如下所示的復雜查詢的示例,我不知道如何處理它。

第 65 行存在COUNT部分的問題。如此復雜的查詢如何在不減速的情況下解決?

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 會查找重復項並將其刪除,但在大數據中處理此查詢將花費大量時間,您應該在編程部分處理中間記錄我認為它會比它快。 感謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM