如何根据日期范围从表中获取数据?

发布于 2025-02-09 22:35:38 字数 368 浏览 2 评论 0原文

是否有可能有一个SQL查询,该查询可以每日日期和客户提取数据? 用例: 表1有客户购​​买的商品,并提供购买数据 表2具有客户可能取得的所有支持门票。

我希望向购买的客户(在表1中)过滤表2数据,但在购买前10天或之后也与我们联系。

因此,如果鲍勃在6/1/2022购买螺丝刀(在表1上)。我希望在5/21/22到6/10/22之间从Bob中拨打任何电话,

以帮助说明:

Is it possible to have a sql query which pulls data per date and customer?
Use case:
Table 1 has items purchased by customers, with data of purchase
Table 2 has all of the support tickets customers may have reached out for.

I am looking to filter table 2 data to customers who have purchased (in table 1) but also have contacted us either 10 days before or after the purchase.

So if Bob purchases a screw driver on 6/1/2022 (on table 1). i am looking to pull any calls from Bob between 5/21/22 to 6/10/22

A picture to help illustrate:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

囚我心虐我身 2025-02-16 22:35:38

使用日期范围的未经测试的代码:

SELECT
   support_id, customer, date_of_support, details
FROM 
   table_1 AS t1
JOIN 
   table_2 AS t2
ON 
   t1.customer = t2.customer 
WHERE 
    daterange(date_of_purchase - '10 days'::interval, date_of_purchase + '10 days'::interval, '[]') @> date_of_support

'[]'包含在depaterange中,以使上限包含在内。换句话说,它会碰到上部date向上一个。

更新

从此处 date/time functions/operators 在雅典娜/普雷斯托中,这似乎是可能的:

...
WHERE 
   date_of_support BETWEEN date_of_purchase - interval '10 days' 
    AND date_of_purchase + interval '10' day;

Untested code using date range:

SELECT
   support_id, customer, date_of_support, details
FROM 
   table_1 AS t1
JOIN 
   table_2 AS t2
ON 
   t1.customer = t2.customer 
WHERE 
    daterange(date_of_purchase - '10 days'::interval, date_of_purchase + '10 days'::interval, '[]') @> date_of_support

The '[]' is included in the daterange to make the upper bound inclusive. In other words it will bump the upper date up one.

UPDATE

From here Date/time functions/operators this would seem to be possible in Athena/Presto:

...
WHERE 
   date_of_support BETWEEN date_of_purchase - interval '10 days' 
    AND date_of_purchase + interval '10' day;

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