我试图找出一个查询,该查询将获得一个订单列表,其中包含产品A和B,C和D,X和Y等的组合。所以我需要一个列表来显示只有具有指定组合的订单。像下面的代码:
CREATE TABLE new
STORED AS PARQUET AS
SELECT DISTINCT
CASE t1.customer_number = RIGHT(t1.customer_number, LEN(t1.customer_number) - 2)
from Old;
customer_number should become short_cust_no
33764703 764703
36764624 764624
36763795 763795
37764829 764829
39766002 766002
答案 0 :(得分:1)
如果您的表格数据
| ORDER_ID | PRODUCT_NAME | PRODUCT |
|----------|--------------|---------|
| 1 | xxx01 | A、B |
| 2 | xxx02 | B、C |
| 3 | xxx03 | C、D |
| 4 | xxx04 | D、X |
| 5 | xxx05 | E、Z |
试试这个脚本:
select order_id,product_name ,product
from orders
where product like ('%A%B%')
or product like ('%C%D%')
如果您的表格数据如此;
| ORDER_ID | PRODUCT_NAME | PRODUCT |
|----------|--------------|---------|
| 1 | xxx01 | A |
| 1 | xxx02 | B |
| 2 | xxx03 | C |
| 2 | xxx04 | D |
| 3 | xxx05 | E |
试试这个脚本:
select order_id
from orders
group by order_id having
count( case when product in ('A','B') then 1 else null end ) > 1
or
count( case when product in ('C','D') then 1 else null end ) > 1
答案 1 :(得分:0)
让我将您的问题解释为需要特定的产品对。如果是,您可以使用group by
和having
。假设您的数据中没有重复项:
select o.order_id
from orders o
group by o.order_id
having sum(case when o.product in ('A', 'B') then 1 else 0 end) = 2 or
sum(case when o.product in ('C', 'D') then 1 else 0 end) = 2 or
sum(case when o.product in ('X', 'Y') then 1 else 0 end) = 2;
如果您希望订单具有两个产品和那些货币对,请添加条件count(*)
= 2.或者,如果您想要深奥,可以更改{{ 1}}类似于else
。