gpt4 book ai didi

mysql - 从 PostgreSQL 翻译到 MariaDB 的这个查询有什么问题?

转载 作者:行者123 更新时间:2023-11-29 09:44:04 25 4
gpt4 key购买 nike

我正在努力将以下 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 时未正确输出“新用户”列。我做错了什么?

最佳答案

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

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

关于mysql - 从 PostgreSQL 翻译到 MariaDB 的这个查询有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56028012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com