在DateAdd SQL中使用Max函数。错误 - where子句中的无效汇总函数[max(date)]

发布于 2025-01-27 11:47:28 字数 514 浏览 2 评论 0原文

我有一个表“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

⒈起吃苦の倖褔 2025-02-03 11:47:28
SELECT CUSTOMERID
FROM
  CSSALES
GROUP BY CUSTOMERID
HAVING
  MAX(TRANSACTIONDATE) < ADD_MONTHS(CURRENT_DATE(),-1)

Shawnt00是正确的日期,如果您只希望在1个日历月内没有活跃的客户,则交易表中的最大日期是无关紧要的。

在雪花中使用current_date()以获取今天的日期部分,然后add_months(date,int)获得几个月。其他功能起作用两个,但它们很容易。如果您只希望客户删除列删除副本的customerid组。

SELECT CUSTOMERID
FROM
  CSSALES
GROUP BY CUSTOMERID
HAVING
  MAX(TRANSACTIONDATE) < ADD_MONTHS(CURRENT_DATE(),-1)

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 then ADD_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.

自控 2025-02-03 11:47:28

我想我将只重复Matt的代码,但是...

使用CTE进行一些测试数据:

WITH CSALES(CUSTOMERID, TRANSACTIONDATE) as (
    SELECT * FROM VALUES
    (1, '2022-05-08'::date), -- to recent
    (1, '2021-05-08'::date),
    
    (2, '2021-05-08'::date), -- old enough
    (2, '2020-05-08'::date)
)

我们可以使用具有的。

SELECT C.CUSTOMERID, MAX(C.TRANSACTIONDATE) as last_trans
FROM CSALES C
GROUP BY 1
HAVING last_trans < DATEADD(month,-1,current_date());

正如马特(Matt)所指出“> add_months ,我已经使用了 dateadd

customerIdlast_trans
22021-05-08

现在,此代码的工作原理与

SELECT CUSTOMERID
FROM (
    SELECT C.CUSTOMERID, MAX(C.TRANSACTIONDATE) as last_trans
    FROM CSALES C
    GROUP BY 1
)
WHERE last_trans < DATEADD(month,-1,current_date());

以下内容

相同: whit visulyId
2

,尽管我们现在已经隐藏了最后一次交易,如果那是想要的话,并添加了一些额外的选择层,没有高级价值。

因此,如果我们想将last_tran隐藏在具有版本中,我们可以是因为我们已经有工作代码,我们可以将最大值推入HAVE(并且我们具有Matt的代码),

SELECT C.CUSTOMERID
FROM CSALES C
GROUP BY 1
HAVING MAX(C.TRANSACTIONDATE) < DATEADD(month,-1,current_date());

该代码允许演示代码:

CustomerID
2

Date Options :

有几种改变日期/时间的方法,具体取决于您喜欢订购逻辑,我倾向于更喜欢dateadd:

SELECT 
    current_date() as cd_a,
    CURRENT_DATE as cd_b,
    DATEADD(month, -1, cd_a) as one_month_ago_a,
    ADD_MONTHS(cd_a, -1) as one_month_ago_b;

给予:

cd_acd_bone_month_ogo_aone_month_ogo_b
2022-05-07 2022-072022-07 2022-072022-072022-04-07 2022-2022-2022-2022-2022-2022-2022-2022-2022-2022-2022-2022- 04-07

I think I am about to just repeat Matt's code, but...

With a CTE for some test data:

WITH CSALES(CUSTOMERID, TRANSACTIONDATE) as (
    SELECT * FROM VALUES
    (1, '2022-05-08'::date), -- to recent
    (1, '2021-05-08'::date),
    
    (2, '2021-05-08'::date), -- old enough
    (2, '2020-05-08'::date)
)

We can use HAVING for a post aggregation filter.

SELECT C.CUSTOMERID, MAX(C.TRANSACTIONDATE) as last_trans
FROM CSALES C
GROUP BY 1
HAVING last_trans < DATEADD(month,-1,current_date());

As Matt noted there are few ways to find the "one month ago today" he used ADD_MONTHS, I have used DATEADD

CUSTOMERIDLAST_TRANS
22021-05-08

Now this code works the same as:

SELECT CUSTOMERID
FROM (
    SELECT C.CUSTOMERID, MAX(C.TRANSACTIONDATE) as last_trans
    FROM CSALES C
    GROUP BY 1
)
WHERE last_trans < DATEADD(month,-1,current_date());

which gives:

CUSTOMERID
2

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)

SELECT C.CUSTOMERID
FROM CSALES C
GROUP BY 1
HAVING MAX(C.TRANSACTIONDATE) < DATEADD(month,-1,current_date());

which gives for the demo code:

CUSTOMERID
2

Date Options:

There are a couple ways to alter date/time, depending how you like to order you logic, I tend to prefer DATEADD:

SELECT 
    current_date() as cd_a,
    CURRENT_DATE as cd_b,
    DATEADD(month, -1, cd_a) as one_month_ago_a,
    ADD_MONTHS(cd_a, -1) as one_month_ago_b;

gives:

CD_ACD_BONE_MONTH_AGO_AONE_MONTH_AGO_B
2022-05-072022-05-072022-04-072022-04-07
夕色琉璃 2025-02-03 11:47:28
SELECT
  C.CUSTOMERID
FROM
  CSALES C
GROUP BY
  C.CUSTOMERID
HAVING
  MAX(C.TRANSACTIONDATE)
  <
  DATEADD(
    month,
    -1,
    (SELECT MAX(TRANSACTIONDATE) FROM CSALES)
  )

或者,假设您有一个客户表...

SELECT
  *
FROM
  CUSTOMER   C
WHERE
  NOT EXISTS (
    SELECT *
      FROM CSALES   CS
     WHERE CS.CUSTOMERID       = C.ID
       AND CS.TRANSACTIONDATE >= DATEADD(
                                   month,
                                   -1,
                                   (SELECT MAX(TRANSACTIONDATE) FROM CSALES)
                                 )
  )

emo:

SELECT
  C.CUSTOMERID
FROM
  CSALES C
GROUP BY
  C.CUSTOMERID
HAVING
  MAX(C.TRANSACTIONDATE)
  <
  DATEADD(
    month,
    -1,
    (SELECT MAX(TRANSACTIONDATE) FROM CSALES)
  )

Or, assuming you have a customer table...

SELECT
  *
FROM
  CUSTOMER   C
WHERE
  NOT EXISTS (
    SELECT *
      FROM CSALES   CS
     WHERE CS.CUSTOMERID       = C.ID
       AND CS.TRANSACTIONDATE >= DATEADD(
                                   month,
                                   -1,
                                   (SELECT MAX(TRANSACTIONDATE) FROM CSALES)
                                 )
  )

Demo : dbfiddle

撩心不撩汉 2025-02-03 11:47:28

有多种可能性,您必须检查哪个速度更快

 选择C.customerid
来自csales c

c.customerid不在
((
    从csales中选择cs.customerid作为CS交叉联接(选择Max(TransActionDate)Maxdate从Csales)T1
    cs.transactiondate&gt; dateadd(月,-1,maxdate)
);
 
  GO
 
 | customerid |
| ----------:|
| 4 |
 选择不同的c.customerid
来自csales c交叉连接(选择最大(交易)最大的csales)t1
在不存在的地方(从csales中选择1个customerid = c.customerid and TransactionDate&gt; dateadd(月,-1,maxdate))
;
去
 
 | customerid |
| ----------:|
| 4 |

db&lt;

there are multiple possibilities, you must check which is faster

SELECT C.CUSTOMERID
FROM CSALES C

WHERE C.CUSTOMERID  NOT IN
(
    SELECT CS.CUSTOMERID FROM CSALES as CS CROSS JOIN (SELECT MAX(TRANSACTIONDATE) maxdate FROM CSALES) t1
    WHERE CS.TRANSACTIONDATE > DATEADD(month, -1, maxdate)
);
GO
| CUSTOMERID |
| ---------: |
|          4 |
SELECT DISTINCT C.CUSTOMERID
FROM CSALES C CROSS JOIN (SELECT MAX(TRANSACTIONDATE) maxdate FROM CSALES) t1
WHERE  NOT EXISTS (SELECT 1 FROM CSALES WHERE CUSTOMERID = c.CUSTOMERID AND TRANSACTIONDATE > DATEADD(month, -1, maxdate))
;
GO
| CUSTOMERID |
| ---------: |
|          4 |

db<>fiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文