我有一个表,其中有4列(customer, gender, division, product)性别具有独特的价值链接到每个客户:male,female或null。
该部门有独特的价值链接到每种产品:男性或女性。
男性顾客可以购买女性产品,反之亦然,因为女性购买男性产品
我想要创建一个表,只选择有男性产品的记录/行为男性,只有女性产品为女性(如果性别为空,选择女性产品记录)有什么简单的方法吗?
我做了一个复杂的过程。首先,使用客户信息将男性和其他客户分开(创建了2个表,cust_male和cust_other)
然后,如果cust_male表中的customer使用join,则返回行(其中除法=‘men’);如果cust_other表中的customer返回女性部门产品行(其中division=' women '),则返回‘cust_male’两个部分。
希望我能有一个简单的方法或代码来解决这个问题。
我们可以使用代码创建tep表
create table tep (id, gender, division, product) as
(
select 1, ‘male’, ‘men’, ‘aaa’ from dual
union all select 2, ‘female’, ‘women’, ’bbb’ from dual
union all select 2, ‘female’, ‘men’, ‘ccc’ from dual
union all select 1, ‘male’, ‘women’, ‘ddd’ from dual
union all select 3, ‘female’, ‘women’, ’ddd’ from dual
union all select 4, ‘null’, ‘women’, ’eee’ from dual
union all select 4, ‘null, ‘men, ’ccc’ from dual
);我的方法是
create table cust_male as
select id from tep where gender='male';
create table cust_other as
select id from tep where (gender ='female') or (gender='null');
select * from tep t
inner join cust_male m
on m.id=t.id
where division ='men'
union all
select * from tep t
inner join cust_other f
on f.id=t.id
where division ='women'希望我不需要再创建这两个表,并且简单地实现只为男性选择男性产品行,为女性或空客户选择女性部门产品行
发布于 2019-04-23 10:55:28
我想要创建一个表,只选择有男性产品的记录/行为男性,只有女性产品为女性(如果性别为空,选择女性产品记录)有什么简单的方法吗?
您希望过滤数据,因此这听起来像一个where子句:
select tep.*
from tep
where (gender = 'male' and division = 'men') or
((gender = 'female' or gender is null) and division = 'women')https://stackoverflow.com/questions/55809639
复制相似问题