从PostgreSQL转换为MariaDB的查询有什么问题?

时间:2019-05-07 17:49:06

标签: mysql sql postgresql mariadb

我正在努力将以下PostgreSQL查询转换为MariaDB。我转换后的查询错误地返回了“新用户”字段。我在做什么错了?

原始的PostgreSQL工作查询:

WITH 

users AS (
  SELECT user_id,
         MIN(occurred_at) AS activated_at
    FROM modeanalytics.retention_events 
   WHERE occurred_at <= NOW()
   GROUP BY 1
),

events AS (
  SELECT user_id,
         event_name,
         occurred_at
    FROM modeanalytics.retention_events 
   WHERE occurred_at <= NOW()
)
SELECT *
  FROM (
SELECT *,
       MAX(CASE WHEN user_period = 0 THEN retained_users ELSE NULL END) OVER (PARTITION BY "Signup Date") AS "New Users",

       retained_users/
          MAX(CASE WHEN user_period = 0 THEN retained_users ELSE NULL END) OVER (PARTITION BY "Signup Date")::FLOAT AS retention_rate,

       (LAG(retained_users) OVER (PARTITION BY "Signup Date" ORDER BY "Signup Date") - retained_users)/
          MAX(CASE WHEN user_period = 0 THEN retained_users ELSE NULL END) OVER (PARTITION BY "Signup Date")::FLOAT AS churn_rate,

       (LAG(retained_users) OVER (PARTITION BY "Signup Date" ORDER BY "Signup Date") - retained_users)/
          LAG(retained_users) OVER (PARTITION BY "Signup Date" ORDER BY "Signup Date")::FLOAT AS churn_rate_from_previous_period
  FROM (

SELECT DATE_TRUNC('month',u.activated_at) AS "Signup Date",

       (EXTRACT('year' FROM e.occurred_at) - EXTRACT('year' FROM u.activated_at)) * 12 + 
       (EXTRACT('month' FROM e.occurred_at) - EXTRACT('month' FROM u.activated_at)) - 
       CASE WHEN (CEILING(DATE_PART('day',e.occurred_at) - DATE_PART('day',u.activated_at))) < 0 THEN 1 ELSE 0 END AS user_period,

       COUNT(DISTINCT e.user_id) AS retained_users
  FROM users u
  JOIN events e
    ON e.user_id = u.user_id
   AND e.occurred_at >= u.activated_at
 WHERE u.activated_at >= DATE_TRUNC('month',NOW()) - INTERVAL '25 month'
 GROUP BY 1,2
       ) x
       ) z
 WHERE user_period != 0
 ORDER BY 1,2

这是我最近为MariaDB重写的尝试:

WITH 

users AS (
  SELECT id,
         MIN(onboarded_at) as onboarded_at
    FROM users
   WHERE onboarded_at <= NOW()
   GROUP BY 1
),

events AS (
  SELECT receiver_id,
         type,
         created_at
    FROM events 
   WHERE created_at <= NOW()
)
SELECT *
  FROM (
SELECT *,
       MAX(CASE WHEN user_period = 0 THEN retained_users ELSE NULL END) OVER (PARTITION BY "Signup Date") AS "New Users",

       (retained_users /
          MAX(CASE WHEN user_period = 0 THEN retained_users*1.0 END) OVER (PARTITION BY "Signup Date")) AS retention_rate,

       (LAG(retained_users) OVER (PARTITION BY "Signup Date" ORDER BY "Signup Date") - retained_users)/
          MAX(CASE WHEN user_period = 0 THEN retained_users ELSE NULL END) OVER (PARTITION BY "Signup Date") AS churn_rate,

       (LAG(retained_users) OVER (PARTITION BY "Signup Date" ORDER BY "Signup Date") - retained_users)/
          LAG(retained_users) OVER (PARTITION BY "Signup Date" ORDER BY "Signup Date") AS churn_rate_from_previous_period
  FROM (

SELECT
        DATE_FORMAT(u.onboarded_at, '%Y-%m-01') AS "Signup Date",

       (EXTRACT(YEAR FROM e.created_at) - EXTRACT(YEAR FROM u.onboarded_at)) * 12 + 
       (EXTRACT(MONTH FROM e.created_at) - EXTRACT(MONTH FROM u.onboarded_at)) - 
       CASE WHEN (CEILING(DAY(e.created_at) - DAY(u.onboarded_at))) < 0 THEN 1 ELSE 0 END AS user_period,
       COUNT(DISTINCT e.receiver_id) AS retained_users
  FROM users u
  JOIN events e
    ON e.receiver_id = u.id
   AND e.created_at >= u.onboarded_at
 WHERE u.onboarded_at >= DATE_FORMAT(CURDATE(), '%Y-%m-01 00:00:00') - INTERVAL 5 MONTH
 GROUP BY 1,2
       ) x
       ) z
 WHERE user_period != 0
 ORDER BY 1,2

我为MariaDB重写的尝试未正确输出“新用户”列。我在做什么错了?

1 个答案:

答案 0 :(得分:1)

有关表名和列名(包含空格),您需要使用反引号而不是双引号,请参见this question

普通字符串应该用单引号引起来。