在DateAdd SQL中使用Max函数。错误 - where子句中的无效汇总函数[max(date)]
我有一个表“ csales”,上面有列,例如customerId,TransactionDate,数量,价格。我试图从交易列中存在的日期列表中找到1个月内没有活跃的客户。我已经尝试了以下代码,但是我不确定该方法,并且该代码正在给出汇编错误,
SELECT C.CUSTOMERID
FROM CSALES C
WHERE C.CUSTOMERID NOT IN
(
SELECT CS.CUSTOMERID FROM CSALES as CS
WHERE CS.TRANSACTIONDATE > DATEADD(month, -1, MAX(CS.TRANSACTIONDATE )
);
我遇到以下错误,
SQL compilation error: Invalid aggregate function in where clause [MAX(CS.TRANSACTIONDATE)]
我应该在代码中进行什么更改以反映要求?最大(日期)是正确的方法吗?
I have a table 'CSALES' having columns such as customerid,transactiondate,quantity,price. I'm trying to find customers who have not been active in 1 month from a list of dates present in the transactiondate column. I've tried the following code but I'm unsure about the approach and the code is giving a compilation error
SELECT C.CUSTOMERID
FROM CSALES C
WHERE C.CUSTOMERID NOT IN
(
SELECT CS.CUSTOMERID FROM CSALES as CS
WHERE CS.TRANSACTIONDATE > DATEADD(month, -1, MAX(CS.TRANSACTIONDATE )
);
I'm getting the following error
SQL compilation error: Invalid aggregate function in where clause [MAX(CS.TRANSACTIONDATE)]
What changes should I make in the code to reflect the requirement? Would MAX(date) be a right approach ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Shawnt00是正确的日期,如果您只希望在1个日历月内没有活跃的客户,则交易表中的最大日期是无关紧要的。
在雪花中使用
current_date()
以获取今天的日期部分,然后add_months(date,int)
获得几个月。其他功能起作用两个,但它们很容易。如果您只希望客户删除列删除副本的customerid组。Shawnt00 is right the max date in the transaction table is irrelevant if you just want any customer that hasn't been active in 1 calendar month.
In snowflake use
CURRENT_DATE()
to get the date portion of Today thenADD_MONTHS(date,int)
to get months. Other functions work two but these are pretty easy. If you only want customers to remove duplicate CUSTOMERIDS group by the column.我想我将只重复Matt的代码,但是...
使用CTE进行一些测试数据:
我们可以使用具有的。
正如马特(Matt)所指出“> add_months ,我已经使用了 dateadd
现在,此代码的工作原理与
以下内容
,尽管我们现在已经隐藏了最后一次交易,如果那是想要的话,并添加了一些额外的选择层,没有高级价值。
因此,如果我们想将last_tran隐藏在具有版本中,我们可以是因为我们已经有工作代码,我们可以将最大值推入HAVE(并且我们具有Matt的代码),
该代码允许演示代码:
Date Options :
有几种改变日期/时间的方法,具体取决于您喜欢订购逻辑,我倾向于更喜欢dateadd:
给予:
I think I am about to just repeat Matt's code, but...
With a CTE for some test data:
We can use HAVING for a post aggregation filter.
As Matt noted there are few ways to find the "one month ago today" he used ADD_MONTHS, I have used DATEADD
Now this code works the same as:
which gives:
Albeit we now have hidden away the last transaction, if that was what was wanted, and added some extra select layers for no high level value.
And thus if we want to hide the last_tran in the HAVING version, we can because we have already working code, we can just push the MAX into the HAVING (and we have Matt's code)
which gives for the demo code:
Date Options:
There are a couple ways to alter date/time, depending how you like to order you logic, I tend to prefer DATEADD:
gives:
或者,假设您有一个客户表...
emo:
Or, assuming you have a customer table...
Demo : dbfiddle
有多种可能性,您必须检查哪个速度更快
db<
there are multiple possibilities, you must check which is faster
db<>fiddle here