如何查询 return 用“1”值代替文本

How do I get a query to return a "1" value in place of text

完整代码如下:

WITH Part1 AS 
(
    SELECT 
        session_id, occurred_at, activity_name
    FROM      
        Activity_Data_Table$
    WHERE   
        (occurred_at > CURRENT_TIMESTAMP - 31) 
        AND (activity_name = 'create')
    GROUP BY 
        session_id, occurred_at, activity_name
), 
Part2 AS 
(
    SELECT 
        session_id, occurred_at, activity_name
    FROM      
        Activity_Data_Table$ AS Activity_Data_Table$_1
    WHERE   
        (occurred_at > CURRENT_TIMESTAMP - 31) 
        AND (activity_name = 'Resolve')
    GROUP BY 
        session_id, occurred_at, activity_name
)
SELECT 
    Part1_1.occurred_at, Part1_1.activity_name, 
    Part2_1.activity_name
FROM   
    Part1 AS Part1_1 
LEFT OUTER JOIN 
    Part2 AS Part2_1 ON Part1_1.session_id = Part2_1.session_id

这是我得到的:

它returns 3列:

Date, activity 1, activity 2
           

我想要的,它可以在其他列中或替代:在 2 activity 列中,如果有非空值(创建或解析),那么我想插入一个 '1 ' 否则为 0 或空值。尝试了无数种插入计数的方法,但它就是行不通,但我相信技术更好的人这是 2 秒。

谢谢

您可以使用 CASE 语句,例如

SELECT ...
     , CASE 
         WHEN Part1_1.activity_name IS NOT NULL
         THEN 1
         ELSE 0
       END AS activity1_name_available
     , CASE 
         WHEN Part2_1.activity_name IS NOT NULL
         THEN 1
         ELSE 0
       END AS activity2_name_available
  FROM ...

您也可以组合它们,例如,如果您需要一个 1(如果其中一个可用,或者两者都可用):

SELECT ...
     , CASE 
         WHEN Part1_1.activity_name IS NOT NULL
           OR Part2_1.activity_name IS NOT NULL
         THEN 1
         ELSE 0
       END AS either_available
     , CASE 
         WHEN Part1_1.activity_name IS NOT NULL
          AND Part2_1.activity_name IS NOT NULL
         THEN 1
         ELSE 0
       END AS both_available
  FROM ...

无需查询 table 两次并合并结果。

SELECT 
    session_id,
    occurred_at,
    MAX(CASE WHEN activity_name = 'create'  THEN 1 ELSE 0 END) AS has_create,
    MAX(CASE WHEN activity_name = 'Resolve' THEN 1 ELSE 0 END) AS has_resolve
FROM
    Activity_Data_Table$
WHERE   
        occurred_at   >  CURRENT_TIMESTAMP - 31
    AND activity_name IN ('create', 'Resolve')
GROUP BY
    session_id,
    occurred_at

这只会 return 个至少有一个或其他 activity 个会话。如果你想允许没有 activity 的会话,只需删除 AND activity_name IN ('create', 'Resolve')

编辑:

我刚刚意识到我不知道这两个活动是否同时发生。如果他们这样做,很好,上面的查询有效。如果它们可以不同,这样的事情可能会有所帮助...

SELECT 
    session_id,
    MIN(occurred_at)   AS occured_at_first,
    MAX(occurred_at)   AS occured_at_last,
    MAX(CASE WHEN activity_name = 'create'  THEN occurred_at END) AS occured_at_create,
    MAX(CASE WHEN activity_name = 'Resolve' THEN occurred_at END) AS occured_at_resolve,        
    MAX(CASE WHEN activity_name = 'create'  THEN  1  ELSE  0 END) AS has_create,
    MAX(CASE WHEN activity_name = 'Resolve' THEN  1  ELSE  0 END) AS has_resolve
FROM
    Activity_Data_Table$
WHERE   
    occurred_at   >  CURRENT_TIMESTAMP - 31
GROUP BY
    session_id

这取决于你need/want。


编辑:

每日转化率...

WITH
  created_sessions AS
(
  SELECT 
    session_id,
    occurred_at,
    MAX(CASE WHEN activity_name = 'Resolve' THEN 1.0 ELSE 0.0 END) AS has_resolve
  FROM
    Activity_Data_Table$
  WHERE   
        occurred_at   >  CURRENT_TIMESTAMP - 31
    AND activity_name IN ('create', 'resolve')
  GROUP BY
    session_id,
    occurred_at
  HAVING
    MAX(CASE WHEN activity_name = 'create'  THEN 1 ELSE 0 END) = 1
)
SELECT
  occurred_at,
  AVG(has_resolve) AS conversion
FROM
  created_sessions
GROUP BY
  occurred_at
ORDER BY
  occurred_at