Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 行聚合上的Case语句_Sql_Google Bigquery - Fatal编程技术网

Sql 行聚合上的Case语句

Sql 行聚合上的Case语句,sql,google-bigquery,Sql,Google Bigquery,我正在使用标准SQL进行Google大查询 我有页面浏览数据。所有相关的是 | user_id | entity_id | url | URL的格式可以是/entities/entity\u id/show或entities/entity\u id/reply/new 一个用户可能会出现在任何一种、两种或两种类型的URL中,并且可能会重复出现 我的目标是一张像这样的桌子 | user_id | entity_id | view_type | | user_id | entity_id |

我正在使用标准SQL进行Google大查询

我有页面浏览数据。所有相关的是

| user_id | entity_id | url |
URL的格式可以是
/entities/entity\u id/show
entities/entity\u id/reply/new

一个用户可能会出现在任何一种、两种或两种类型的URL中,并且可能会重复出现

我的目标是一张像这样的桌子

| user_id | entity_id | view_type |
| user_id | entity_id |            url             |
----------------------------------------------------
|       1 |        23 | '/entities/23/replies/new' |
|       1 |        23 |        '/entities/23/show' |
|       2 |        30 |        '/entities/30/show' |
其中,
view\u type
为“显示”或“新建”

每个用户/实体对只能有一行<如果
用户id
/
实体id
对曾经出现在表单
/entities/entity\u id/回复/new
的一个url旁边,则code>视图类型应为“new”,但如果该对没有“new”url,则为“show”。如果原始表中没有
用户id
/
实体id
对的示例,则最终表中应该没有这些示例

我将包括一个with语句,其中包含用于重复性的示例数据

WITH data AS (
    select 1 as user_id, 23 as entity_id, '/entities/23/replies/new' as url

    UNION ALL

    select 1 as user_id, 23 as entity_id, '/entities/23/show' as url

    UNION ALL

    select 2 as user_id, 30 as entity_id, '/entities/30/show' as url
)
SELECT * from data
那就摆了一张这样的桌子

| user_id | entity_id | view_type |
| user_id | entity_id |            url             |
----------------------------------------------------
|       1 |        23 | '/entities/23/replies/new' |
|       1 |        23 |        '/entities/23/show' |
|       2 |        30 |        '/entities/30/show' |
我可以通过两个
语句来实现我的目标,这两个
语句对任何一种url执行
选择distinct
,然后返回并执行一个
case
语句,该语句对给定的
用户
/
实体
对有无连接进行操作

我的意思是:

WITH data AS (
    select 1 as user_id, 23 as entity_id, '/entities/23/replies/new' as url

    UNION ALL

    select 1 as user_id, 23 as entity_id, '/entities/23/show' as url

    UNION ALL

    select 2 as user_id, 30 as entity_id, '/entities/30/show' as url
), news AS (
    SELECT DISTINCT user_id, entity_id, 1 as found
    FROM data 
    WHERE url like '%new'
), shows AS (
    SELECT DISTINCT user_id, entity_id, 1 as found 
    FROM data
    WHERE url like '%show'
)
SELECT DISTINCT d.user_id, 
    d.entity_id,
    CASE WHEN n.found = 1 then 'new'
        WHEN s.found = 1 then 'show' end as view_type
FROM data d
LEFT JOIN news n on n.user_id = d.user_id and n.entity_id = d.entity_id
LEFT JOIN shows s on s.user_id = d.user_id and s.entity_id = d.entity_id
显然,样例数据使它看起来比实际更令人畏惧,但这仍然是一个相当笨拙、不可读的查询,如果我添加另一个<代码> VIEWIONTYPE ,我想考虑一下,这是一个痛苦的延伸。

我想一定有更好的办法

我突然想到,我可以尝试将
用户id
/
实体id
对的所有URL填充到一个数组中,然后用
case
语句对数组进行操作,比如“如果数组中的任何元素匹配“new”然后“new”,等等。)。但我不确定如何做“任何元素regex匹配”“如果可能的话


我很感激任何人能给予我的洞察力

一种方法是聚合:

SELECT user_id, entity_id, 
       (CASE WHEN COUNTIF(url like '%new') > 0 THEN 'new' ELSE 'show'
        END) as view_type
FROM data 
GROUP BY user_id, entity_id

是的,这正是我所希望的。谢谢