简体   繁体   English

如果满足不同表中的另一个条件,则创建一个列并用某个值填充它?

[英]Create a column and fill it with a certain value if another condition in a different table is met?

I've to write a query that retrieves the company name, first line of the street address, city, and a column named Address_Type with the value 'Billing' for customers where the address type in the SalesLT.CustomerAddress table is 'Main Office'.我必须编写一个查询来检索公司名称、街道地址的第一行、城市和名为 Address_Type 的列,其中 SalesLT.CustomerAddress 表中的地址类型为“Main Office”的客户的值为“Billing” .

I've a table called Sa.customer which has the Customer Info like First name, last name, address etc. and another table called SalesLT.CustomerAddress which has Customer ID, Address ID, Address_Type etc. In my query I've to make a new column named "Address Type" with value "Billing" if the condition address type in SalesLT.CustomerAddress table = 'Main Office' is met.我有一个名为 Sa.customer 的表,其中包含名字、姓氏、地址等客户信息,还有另一个名为 SalesLT.CustomerAddress 的表,其中包含客户 ID、地址 ID、地址类型等。在我的查询中,我要做如果满足 SalesLT.CustomerAddress 表中的条件地址类型 = 'Main Office',则名为“Address Type”的新列的值为“Billing”。

I've joined multiple tables wherever required and have applied the filter using WHERE command.我在需要的地方加入了多个表,并使用 WHERE 命令应用了过滤器。 It has to be a compounded query ie I have to fill the blank with a command and I cannot add or modify the code between the two lines.它必须是一个复合查询,即我必须用命令填充空白,并且我不能在两行之间添加或修改代码。

SELECT CompanyName, AddressLine1, City, ___ AS Address_Type
FROM SalesLT.Customer AS c
JOIN SalesLT.CustomerAddress AS ca  
  ON c.Customer_ID = ca.Customer_ID  ###-- join based on Customer_ID   
FROM SalesLT.Address AS a       ### -- join another table  
  ON a.Address_ID = ca.Address_ID   ###-- join based on AddressID
WHERE AddressType = 'Main Office'; ###-- filter for where the correct AddressType

You can utilize the SQL Server CASE statement to construct a value for your [Address Type] column by testing the value of the SalesLT.CustomerAddress field for that result tuple:您可以利用 SQL Server CASE语句通过测试该结果元组的 SalesLT.CustomerAddress 字段的值来为[Address Type]列构造一个值:

SELECT ...,
  CASE 
    WHEN SalesLT.CustomerAddress LIKE 'Main Office' THEN 'BILLING'
    ELSE ''
  END AS [Address Type],
  ...
FROM ...
WHERE ...

However, you will probably find that there are more ways to spell Main Office than you would think if you are dealing with user entered data!但是,如果您正在处理用户输入的数据,您可能会发现拼写 Main Office 的方法比您想象的要多! I would recommend TRIM() on the CustomerAddress field in the CASE statement as well.我也会在 CASE 语句中的 CustomerAddress 字段上推荐TRIM()

暂无
暂无

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

相关问题 如何在满足条件时从另一个表中获取值的表中创建列,但如果条件失败则分配不同的值 - How to create column in a table which takes values from another table when the condition is met but assigns a different value if condition fails 查询更新无条件的第一列,仅在满足特定条件时更新另一列 - Query to update 1 column with no condition and another column only if certain condition is met 如果满足 A 列中的条件,则用 B 列的值填充 A 列中的行 - Fill rows in column A with value of column B if condition in column A is met 如何在满足另一个条件时创建列的 AVG - How to I create an AVG of a column when another condition is met 使用表B中满足表A中特定条件的列中的数据更新表A中的列 - Updating column in Table A with data from a column in Table B where a certain condition from Table A is met SQL:如果满足给定条件,则在不同的表中查找值 - SQL: look up value in a different table if a given condition is met Select 某些列仅在满足条件时 - Select certain column only if condition met 在 SQL 中为一个表创建一个 function ,其中某个列值的参数返回同一行中的另一个列值? - Create a function in SQL for a table with a parameter for a certain column value which returns another column value in the same row for it? SQL-如果满足特定条件,则从表中进行SELECT,否则满足条件 - SQL - SELECT from a table if a certain condition is met, else SELECT from another 仅在满足另一列的条件时计数 - Count only if condition on another column met
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM