该查询仅在存在 :: DATE
时有效。
-- Wrap the query you wrote in a CTE named reg_dates
WITH reg_dates AS (
SELECT
user_id,
MIN(order_date) AS reg_date
FROM orders
GROUP BY user_id)
SELECT
-- Count the unique user IDs by registration month
DATE_TRUNC('month', reg_date) :: DATE AS delivr_month,
COUNT(DISTINCT user_id) AS regs
FROM reg_dates
GROUP BY delivr_month
ORDER BY delivr_month ASC;
为什么需要这样做?当我运行下面的查询时,没有 :: DATE
,它不起作用。
-- Wrap the query you wrote in a CTE named reg_dates
WITH reg_dates AS (
SELECT
user_id,
MIN(order_date) AS reg_date
FROM orders
GROUP BY user_id)
SELECT
-- Count the unique user IDs by registration month
DATE_TRUNC('month', reg_date) AS delivr_month,
COUNT(DISTINCT user_id) AS regs
FROM reg_dates
GROUP BY delivr_month
ORDER BY delivr_month ASC;
答案 0 :(得分:0)
您的 RDBMS 很可能是 PostgreSQL,在您的情况下,::
将日期类型转换为日期,此外,::
表示为 CAST(expression AS type)
。
同样,
CAST (DATE_TRUNC('month', reg_date) AS DATE) AS delivr_month
答案 1 :(得分:0)
“不起作用”是什么意思?请注意,PostgreSQL 中的 date_trunc
返回一个 datetime
。因此,如果您的查询需要一个日期,这就是您需要 ::date
的原因。