I need to print all columns but for some I need to print null using CASE in Oracle
| 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-This is not a table
I was trying to write this query -
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;
This is not working .
============================================================
The tables and columns just for reference:
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);
You could print the output as a single column that consists of multiple concatenated columns. From your (deleted) previous question you indicated that this is for a label, so you don't need multiple columns. In the code below, different columns are concatenated based on the value of customer_type.
This could work:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.