错误:必须对属性进行分组或在聚合函数 NETEZZA SQL 中
Posted
技术标签:
【中文标题】错误:必须对属性进行分组或在聚合函数 NETEZZA SQL 中【英文标题】:Error: Attribute must be grouped or in aggregate function NETEZZA SQL 【发布时间】:2020-12-29 11:45:49 【问题描述】:我正在编写 SQL 来创建如下的日历表并收到错误“属性 T1.CALENDAR_DATE 必须分组或在聚合函数中使用
我写的代码是
WITH CALENDAR AS
(
SELECT
EXTRACT(EPOCH FROM DATE(NOW()-1)-T1.CALENDAR_DATE)/7 AS ROLLING_WEEK,
MIN(T1.WEEK_OF_YEAR) AS WEEK,
MAX(T1.CALENDAR_DATE) AS LAST_DAY_WEEK,
MIN(T1.CALENDAR_DATE) AS FIRST_DAY_WEEK
FROM DIM_GINZ_DATE AS T1
WHERE T1.ROLLING_MONTH BETWEEN 0 AND 84
GROUP BY T1.ROLLING_WEEK
)
SELECT *
FROM CALENDAR
这里有什么问题?
谢谢:)
【问题讨论】:
尝试使用:group by EXTRACT(EPOCH FROM DATE(NOW()-1)-T1.CALENDAR_DATE)/7
。
【参考方案1】:
您应该在GROUP by
中使用T1.CALENDAR_DATE
或在EXTRACT(EPOCH FROM DATE(NOW()-1)-T1.CALENDAR_DATE)/7
上使用一些聚合函数
WITH CALENDAR AS
(
SELECT
EXTRACT(EPOCH FROM DATE(NOW()-1)-T1.CALENDAR_DATE)/7 AS ROLLING_WEEK,
-- something like this need to be implemented if you dont want to group by T1.CALENDAR_DATE
-- EXTRACT(EPOCH FROM DATE(NOW()-1)-MIN(T1.CALENDAR_DATE))/7 AS ROLLING_WEEK
MIN(T1.WEEK_OF_YEAR) AS WEEK,
MAX(T1.CALENDAR_DATE) AS LAST_DAY_WEEK,
MIN(T1.CALENDAR_DATE) AS FIRST_DAY_WEEK
FROM DIM_GINZ_DATE AS T1
WHERE T1.ROLLING_MONTH BETWEEN 0 AND 84
GROUP BY T1.ROLLING_WEEK
, T1.CALENDAR_DATE -- like this
-- or use EXTRACT(EPOCH FROM DATE(NOW()-1)-T1.CALENDAR_DATE)/7
)
SELECT *
FROM CALENDAR
如果select
子句中没有未聚合的列(如果使用),则它们不能在select
子句中使用。
【讨论】:
以上是关于错误:必须对属性进行分组或在聚合函数 NETEZZA SQL 中的主要内容,如果未能解决你的问题,请参考以下文章
Postgresql 错误:列必须出现在 GROUP BY 子句中或在聚合函数中使用
仅休眠错误:“列必须出现在 GROUP BY 子句中或在聚合函数中使用”