我正在尝试在Teradata中编写一个查询,该查询将按月返回每个商店中发生的事务数。我发现的主要问题是交易日期存储为日期(显然),我想按月份对它们进行分组。以下是我对查询的尝试。
SELECT
COUNT(txn_nbr),
str_nbr,
EXTRACT(MONTH FROM txn_dt) AS txn_month, EXTRACT (YEAR FROM txn_dt) AS txn_year
FROM tbl_name
WHERE str_nbr IN (xxxx, xxxx, xxxx)
AND fill_sold_dt BETWEEN '2016-12-31' AND '2017-08-31'
GROUP BY 4,3
我收到的错误消息是“SELECT Failed.3504:Selected non-aggregate必须是关联组的一部分。”请问,有人可以帮我正确编写查询吗?我不是那种经验丰富的SQL,我会非常感激。
答案 0 :(得分:2)
需要按str_nbr分组:
SELECT
COUNT(txn_nbr),
str_nbr,
EXTRACT(MONTH FROM txn_dt) AS txn_month,
EXTRACT (YEAR FROM txn_dt) AS txn_year
FROM tbl_name
WHERE str_nbr IN (xxxx, xxxx, xxxx)
AND fill_sold_dt BETWEEN '2016-12-31' AND '2017-08-31'
GROUP BY 2,3,4