我有 2 张桌子,一张看起来像这样 第一个是订单
Customer_ID | 订单号 | 状态 | |
---|---|---|---|
A | 11 | 完成 | |
A | 12 | 完成 | |
乙 | 13 | 完成 | |
乙 | 14 | 完成 | |
乙 | 15 | 完成 | |
C | 16 | 完成 | |
乙 | 17 | 取消 | |
A | 18 | 取消 |
第二个是客户
Customer_ID | 客户状态 | join_date |
---|---|---|
A | 15 | 2022-02-09 |
b | 15 | 2022-02-10 |
c | 10 | 2022-02-10 |
我尝试将查询用作子查询,但它没有用我是新手,但仍在挣扎
SELECT T1.customer_id, count (T1.ORDER_ID) as Orders_count
FROM orders T1 LEFT join customers T2
on T1.Customer_ID = T2.Customer_ID
where T1.STATUS= 6 AND T2.Customer_status= 50
AND T2.join_date between timestamp'2022-02-10 'and timestamp '2022-02-11'
GROUP BY T1.Customer_ID ORDER BY T1.Customer_ID
我想知道将用户分类为符合条件和不符合条件的用户,符合条件的是用户状态为 15 并下了超过 1 个订单并且他们的加入日期是 2022-02-10 其他人不符合条件,我希望表格显示出来两者,我都在使用 redash
group by
和 having
可用于创建下了很多订单的客户列表。
然后一个
left join
加入这个名单,看看谁有资格。
select c.*, if(s.Customer_ID is not null, 'YES','NO') as Eligible
from customers c
left join (
select Customer_ID
from orders
group by Customer_ID
having count(1) > 1
) as s on s.Customer_ID = c.Customer_ID and c.Customer_status = 15 and c.join_date = '2022-02-10'