![](/img/trans.png)
[英]Select a new column based on values of two column from two tables with null if no data is present
[英]Print all fields from two tables and set some column values to null based on a column using ORACLE CASE statement
我需要打印所有列,但对于某些列,我需要在 Oracle 中使用 CASE 打印空值
| Db fields | Male |Female|Trans |
| ------------- | ------- |------|------|
|customer_id | Y | | |
|customer_name | Y | Y | Y |
|address | | Y | |
|city | Y | Y | Y |
|state | Y | Y | Y |
|zip_code | | Y | |
|customer_type | Y | | Y |
PS-这不是一张桌子
我试图写这个查询-
select
case
when c.customer_type='Female' then customer_id is null
when c.customer_type='Trans' then customer_id is null
case(address)
when c.customer_type='Male' then address is null
when c.customer_type='Trans' then address is null
case(zip_code)
when c.customer_type='Male' then zip_code is null
when c.customer_type='Trans' then zip_code is null
case(customer_type)
when c.customer_type='Female' then customer_type is null
end
c.customer_id, c.customer_name, c.address, c.city, c.state,c.zip_code,c.customer_type, e.employee_number, e.employee_name from customers c, employees e where e.customer_id=c.customer_id where c.customerid=1;
这是行不通的。
================================================== ==========
表和列仅供参考:
CREATE TABLE customers(
customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
address varchar2(50),
city varchar2(50),
state varchar2(25),
zip_code varchar2(10),
customer_type varchar2(10),
CONSTRAINT customers_pk PRIMARY KEY (customer_id)
);
insert into customers values(1,'A','Japan','Tokyo','J',001,'Male');
insert into customers values(2,'B','UK','London','U',002,'Female');
insert into customers values(3,'C','China','Tibet','S',003,'Trans');
insert into customers values(4,'D','South korea','Souel','S',004,'Female');
CREATE TABLE employees(
employee_number number(10) NOT NULL,
employee_name varchar2(50) NOT NULL,
customer_id number(10),
CONSTRAINT employees_pk PRIMARY KEY (employee_number),
CONSTRAINT fk_customers FOREIGN KEY (customer_id) REFERENCES customers(customer_id));
insert into employees values(101,'titu',1);
insert into employees values(102,'kitu',2);
insert into employees values(103,'pitu',3);
insert into employees values(104,'yitu',4);
您可以将输出打印为由多个串联列组成的单列。 从您(已删除)上一个问题中,您指出这是用于标签的,因此您不需要多列。 在下面的代码中,根据 customer_type 的值连接了不同的列。
这可以工作:
SELECT
CASE
WHEN customer_type = 'Male' THEN
c.customer_id ||' -> '|| c.customer_name ||' -> '|| c.city ||' -> '|| c.state ||' -> '|| c.customer_type
WHEN customer_type = 'Female' THEN
c.customer_name ||' -> '|| c.address ||' -> '|| c.city ||' -> '|| c.state ||' -> '|| c.zip_code
WHEN customer_type = 'Trans' THEN
c.customer_name ||' -> '|| c.city ||' -> '|| c.state ||' -> '|| c.customer_type
END as "Output"
FROM customers c
JOIN employees e ON e.customer_id=c.customer_id;
Output
---------------------------
1 -> A -> Tokyo -> J -> Male
B -> UK -> London -> U -> 2
C -> Tibet -> S -> Trans
D -> South korea -> Souel -> S -> 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.