简体   繁体   English

打印两个表中的所有字段,并使用 ORACLE CASE 语句基于列将一些列值设置为空

[英]Print all fields from two tables and set some column values to null based on a column using ORACLE CASE statement

I need to print all columns but for some I need to print null using CASE in Oracle我需要打印所有列,但对于某些列,我需要在 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-This is not a table PS-这不是一张桌子

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.在下面的代码中,根据 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Select 新列基于两个表中两列的值,如果不存在数据,则为 null - Select a new column based on values of two column from two tables with null if no data is present 从两个表中选择并设置空列 - Select from two tables and set null column 从oracle的列的值集中减去一些常数值 - minus a set of some constant values from a set of values of a column in oracle 在一个语句中根据两个连接的列值选择行 - ORACLE - Choose rows based on two connected column values in one statement - ORACLE 如何在oracle中的表中将所有列从null设置为not null - How to set all column from null to not null in table in oracle 使用 Oracle 如何根据两个不同的 if 语句有条件地从两个不同的表中选择一列? - Using Oracle how do I conditionally select a column from two different tables based on two difference if statements? 查找具有指定列名的所有表并将值设置为NULL - Find all tables with specified column name and set values to NULL 使用 CASE 语句根据表 B 中的值更新表 A 中的列 - Update a column in table A based on values in table B using a CASE statement SQL - 使用基于两个表的表达式更新表列值 - SQL - Update table column values using expression based on two tables 使用 CASE 语句根据变量选择列 - Selecting column based on variable using CASE statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM