首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据其他列的标记/值选择不同的值

根据其他列的标记/值选择不同的值
EN

Stack Overflow用户
提问于 2019-04-23 10:53:37
回答 1查看 49关注 0票数 2

我有一个表,其中有4列(customer, gender, division, product)性别具有独特的价值链接到每个客户:malefemalenull

该部门有独特的价值链接到每种产品:男性或女性。

男性顾客可以购买女性产品,反之亦然,因为女性购买男性产品

我想要创建一个表,只选择有男性产品的记录/行为男性,只有女性产品为女性(如果性别为空,选择女性产品记录)有什么简单的方法吗?

我做了一个复杂的过程。首先,使用客户信息将男性和其他客户分开(创建了2个表,cust_male和cust_other)

然后,如果cust_male表中的customer使用join,则返回行(其中除法=‘men’);如果cust_other表中的customer返回女性部门产品行(其中division=' women '),则返回‘cust_male’两个部分。

希望我能有一个简单的方法或代码来解决这个问题。

我们可以使用代码创建tep表

代码语言:javascript
复制
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
);

我的方法是

代码语言:javascript
复制
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'

希望我不需要再创建这两个表,并且简单地实现只为男性选择男性产品行,为女性或空客户选择女性部门产品行

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-23 10:55:28

我想要创建一个表,只选择有男性产品的记录/行为男性,只有女性产品为女性(如果性别为空,选择女性产品记录)有什么简单的方法吗?

您希望过滤数据,因此这听起来像一个where子句:

代码语言:javascript
复制
select tep.*
from tep
where (gender = 'male' and division = 'men') or
      ((gender = 'female' or gender is null) and division = 'women')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55809639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档