sql-server - SQL where between date 子句引用多个日期范围
问题描述
我有两个要合并的 SQL 客户表。
CustomerDate:
Column 1: customerkey int
Column 2: RecentPurchase smalldatetime- the date of their most recent purchase
Column 3: PriorYear smalldatetime - the RecentPurchase date minus a year
CustomerTrans:
Column 1: Transactionkey int
Column 2: customerkey int
Column 3: TransactionDate smalldatetime- Date of Transaction
Column 4: Amount - Total Amount Spent
我想获取客户在最近一次购买和上一年日期之间花费的总金额。我已经尝试了以下查询,但它没有返回任何结果。
select t.customerkey,
sum(t.amount)
from customertrans t
inner join customerdate d on t.customerkey = d.customerkey
where TransactionDate between RecentPurchase and PriorYear
group by t.customerkey
如何运行查询,以便每个客户 TransactionDate 由 CustomerDate 表中的日期确定?
解决方案
也许你只是想要:
where TransactionDate between PriorYear and RecentPurchase
代替 :
where TransactionDate between RecentPurchase and PriorYear
推荐阅读
- angular-material - 垫选择选项覆盖窗格的可调节宽度
- authentication - 使用 Windows 凭据进行 UWP NTLM 身份验证
- php - 在 php 和数据库中上传个人资料照片
- cassandra-2.1 - 启动期间遇到异常 - 清空 cassandra.yaml 文件
- amazon-s3 - 从默认 VPC 中的 Lambda 访问 AWS S3
- python - 如何最好地从代码块中删除冗余调用,应该在块成功存在时执行?
- twitter-bootstrap - 超出列宽的元素
- python - 将 OData 查询编码为 URL
- ios - 尝试在 Flutter 中设置背景图像时出现 MediaQuery.of() 错误
- vba - CorelDraw VBA 在 CQL 中使用变量