Amazon redshift 无法根据初始操作查询所需的数据

Amazon redshift 无法根据初始操作查询所需的数据,amazon-redshift,Amazon Redshift,所以我认为最好的办法是把它全部说清楚,我尝试了很多查询,但都失败了,所以我求助于你们这些出色的人,他们已经做了很久了。我从中提取的数据有4列:Order\u num(存储订单号)、Order\u status\u username(用户名)、Order\u status\u text(给出发送的消息)和Order\u status\u Time(发送消息的时间)。所以我需要两件事,首先我需要这个查询的结果: Select count(distinct order_num ) as Total F

所以我认为最好的办法是把它全部说清楚,我尝试了很多查询,但都失败了,所以我求助于你们这些出色的人,他们已经做了很久了。我从中提取的数据有4列:Order\u num(存储订单号)、Order\u status\u username(用户名)、Order\u status\u text(给出发送的消息)和Order\u status\u Time(发送消息的时间)。所以我需要两件事,首先我需要这个查询的结果:

Select count(distinct order_num ) as Total
From pos.order_status 
Where order_num in (Select order_num From pos.order_status Where order_status_username = 'stanleyb' and trunc(order_status_added) = '2020-10-01' and order_status_text ilike 'Sent to florist%')

Union 

Select count(distinct order_num) as Stayed
From pos.order_status 
where order_status_text Ilike '%to [Delivered]' and 
order_num in (Select order_num From pos.order_status Where order_status_username = 'stanley' and trunc(order_status_added) = '2020-10-01' and order_status_text ilike 'Sent to florist%') 
提供他手动发送的订单总数。现在我需要Stanley发出的订单数量,在他发出订单后得到了一条特定的信息。每次订单在我们和供应商之间来回发送数据时,数据都有一个新行。我尝试使用:

Select count(distinct order_num) as Stayed
From pos.order_status 
where order_status_text Ilike '%to [Delivered]' and 
order_num in (Select order_num From pos.order_status Where order_status_username = 'stanleyb' and trunc(order_status_added) = '2020-10-01' and order_status_text ilike 'Sent to florist%')

但这不允许我这样做,因为它显示的状态与订单号相关,是在Stanley发送消息之后发送的。我试着加入,我想我错过了什么。工会工作正常,但仍然存在一个问题,那就是确保它只在斯坦利发送了包括“发送给花店%”在内的信息之后才发送数据。如果您有任何见解,请继续学习。

获取Stanly的所有记录,并在
pos.order\u status
上加入这些记录,以获取以下记录:

  • 拥有相同的
    订单数量
  • 在他的消息之后发生
  • 有所需的文本
我使用了详细的表别名,试图使逻辑更清晰

SELECT COUNT(statlys\u message.order\u num后不同)
从…起
pos.order\u状态stanlys\u消息
持续发送消息后加入pos.order_状态
在\u stanlys\u message.order\u num=stanlys\u message.order\u num之后
以及在_stanlys_message.order_status_time>stanlys_message.order_status_time之后
以及在_stanlys_message.order_status_文本i如“%to[Delivered]”之后
哪里
stanlys_message.order_status_username='stanleyb'
和TRUNC(添加了stanlys_消息、订单状态)='2020-10-01'
还有stanlys_message.order_status_text我喜欢‘发送给花店%’

伙计,这让我觉得红移吓跑了所有人。赢家,赢家,鸡肉晚餐。非常感谢。这很有趣,我有基础想出这样的东西,但我倾向于把它复杂化。