查询包括NULL和''在SUM值中,即使应明确排除它们

时间:2016-07-01 21:25:10

标签: sql postgresql apache-spark-sql

此查询正在处理已解析的http请求,并创建了查看某些类型的页面元素的次数,然后按多个条件进行分组。如果SUM没有controller_context_idNULL之外的其他值,则应明确排除在''函数中计算的记录。但是,在运行查询之后,我仍然会获得包含未填充的controller_context_id的行,其中包含controller_type所用的总计数字。这不应该发生。我希望所有controller_context_id列中没有num_page_views_*的行都为0。

我错过了查询逻辑的基本内容吗?

SELECT
            TRUNC(request_timestamp, 'month') AS request_timestamp,
            account_id,
            account_guid,
            cluster_id,
            shard_id,
            unique_id,
            context_id,
            controller_type,
            controller_context_id,
            concat_user_id,
            user_id,
            COUNT(account_id) AS num_page_views,
            SUM(CASE
                    WHEN controller_type LIKE 'pages%' AND
                         controller_context_id <> '' AND
                         controller_context_id IS NOT NULL
                    THEN 1
                    ELSE 0 END) AS num_page_views_pages,
            SUM(CASE
                    WHEN controller_type LIKE 'files%' AND
                         controller_context_id <> '' AND
                         controller_context_id IS NOT NULL
                    THEN 1
                    ELSE 0 END) AS num_page_views_files,
            SUM(CASE
                    WHEN controller_type LIKE 'modules%' AND
                         controller_context_id <> '' AND
                         controller_context_id IS NOT NULL
                    THEN 1
                    ELSE 0 END) AS num_page_views_modules,
            SUM(CASE
                    WHEN controller_type LIKE 'assignments%' AND
                         controller_context_id <> '' AND
                         controller_context_id IS NOT NULL
                    THEN 1
                    ELSE 0 END) AS num_page_views_assignments,
            SUM(CASE
                    WHEN controller_type LIKE 'quizzes%' AND
                         controller_context_id <> '' AND
                         controller_context_id IS NOT NULL
                    THEN 1
                    ELSE 0 END) AS num_page_views_quizzes,
            SUM(CASE
                    WHEN controller_type LIKE 'discussion_topics%' AND
                         controller_context_id <> '' AND
                         controller_context_id IS NOT NULL
                    THEN 1
                    ELSE 0 END) AS num_page_views_discussion_topics,
            SUM(CASE
                    WHEN controller_type LIKE 'outcome%' AND
                         controller_context_id <> '' AND
                         controller_context_id IS NOT NULL
                    THEN 1
                    ELSE 0 END) AS num_page_views_outcomes,
            COUNT(DISTINCT session_id) AS num_sessions
        FROM requests
        GROUP BY
          TRUNC(request_timestamp, 'month'),
          account_id,
          account_guid,
          cluster_id,
          shard_id,
          unique_id,
          context_id,
          context_id,
          controller_type,
          controller_context_id,
          concat_user_id,
          user_id

0 个答案:

没有答案