SELECT
c.customer_Id, SUM(total_amt), DATEDIFF(YEAR, DOB, GETDATE()) as DOB
FROM
Transactions T
INNER JOIN
Customer C ON T.cust_id = C.customer_Id
GROUP BY
c.customer_Id, DATEDIFF(YEAR, DOB, GETDATE())
HAVING
(DATEDIFF(YEAR, DOB, GETDATE()) BETWEEN 25 AND 35)
AND (SELECT tran_date
FROM Transactions
HAVING tran_date BETWEEN DATEADD (DAY , -30, MAX(tran_date)) AND MAX(tran_date))
执行以上代码时出现此错误:
消息4145,级别15,状态1,第136行在 ')'附近的期望条件下指定的非布尔类型表达式
如果要汇总每个客户的25岁至35岁之间的每个客户最近30天的交易记录,那么您基本上想在汇总之前进行过滤:
SELECT c.customer_Id, SUM(total_amt), DATEDIFF(YEAR, DOB, GETDATE()) as DOB
FROM Customer C JOIN
(SELECT T.*,
MAX(tran_date) OVER (PARTITION BY cust_id) as max_tran_date
FROM Transactions T
) T
ON T.cust_id = C.customer_Id
WHERE DOB >= DATEADD(YEAR, -35, GETDATE()) AND
DOB < DATEADD(YEAR, -24, GETDATE()) AND
t.tran_date >= DATEADD(DAY, -30, max_tran_date)
GROUP BY c.customer_Id, DATEDIFF(YEAR, DOB, GETDATE());
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。