简体   繁体   English

如果一列为空,则使用默认值在两列上进行外部联接

[英]Outer Join on two columns with default value if one column is null

I have two tables - one a transaction table, and the other a look-up table. 我有两个表-一个是事务表,另一个是查询表。 These tables join on two columns, one of which always has a value, while the other may not. 这些表连接在两列上,其中一列始终具有一个值,而另一列则可能没有。

Sample this 试一下

transaction table

category | subcategory | marks A | 01 | 10 A | 02 | 20 B | 03 | 30 B | 04 | 40 C | 05 | 50

lookup table

category | subcategory | cut-off A | | 15 A | 01 | 25 B | 03 | 35 B | | 55 C | | 75

I wish to get the cut-off next to each entry in the transaction table by joining the category and subcategory columns. 我希望通过合并categorysubcategory列来获得transaction表中每个条目旁边的subcategory For cases where subcategory does not have an exact match, the record corresponding to null needs to be picked. 对于subcategory不完全匹配的情况,需要选择与null对应的记录。

Desired output format: 所需的输出格式:

output format

category | subcategory | marks | cut-off A | 01 | 10 | 25 A | 02 | 20 | 15 B | 03 | 30 | 35 B | 04 | 40 | 55 C | 05 | 50 | 75

I have been trying to think along the lines of a query like the one below, the problem of course is it doesn't work the way I want it to as it doesn't handle the null case right. 我一直在尝试像下面这样的查询思路,问题当然是它不能按我想要的方式工作,因为它不能正确处理空值。

select t.category, t.subcategory, t.marks, l.cut-off from transaction t left outer join lookup l on t.category = l.category and t.subcategory = l.subcategory

Do I need multiple queries (like a union all that handles null and not null separately? Are there options that work with single query? 我是否需要多个查询(例如,一个union all not null分别处理nullnot null ?是否有适用于单个查询的选项?

您可以这样做:

SELECT t.category, t.subcategory, t.marks, l.cut-off FROM transaction t left outer join lookup l on t.category = l.category and t.subcategory = ISNULL(l.subcategory, t.subcategory)

Using a sub query (NVL for oracle) 使用子查询(对于Oracle为NVL)

SELECT t.category,
  t.subcategory,
  t.marks,
  NVL( 
      (SELECT l.cut_OFF from lookup l where l.category = t.category and l.subcategory = t.subcategory), 
      (SELECT l.cut_OFF from lookup l where l.category = t.category and l.subcategory is null))
FROM transaction t

暂无
暂无

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

相关问题 在两列上的左外连接仅在一列上连接[暂停] - Left outer join on two columns only joining on one column [on hold] 加入两列,如果 null 则只加入一列 - Join on two columns, if null then only join on one 将两列合并为一列 - join two columns to one column 除非为空,否则如何基于一列联接两个表,在这种情况下,应基于两个不同的列联接 - How do you join two tables based on one column unless it's null, in which case join based on two different columns 如何正常连接两列上的两个表,但如果其中一列包含null,那么结果必须包含仅与另一列匹配的行? - How to join two tables on two columns normally, but if one of columns contain null, then result must contain rows that match another column only? 如何将一个表中的两个或更多列与另一个表中的一个列联接在一起,即使第一个表列中的值为NULL - How to join two or more columns from one table with one column from another table, even there are NULL values in first table columns SQL:空列上的FULL OUTER JOIN - SQL : FULL OUTER JOIN on null columns 如何将一列中的值连接起来? - How to join the columns value in one column? SQL:一列的默认值是两列相加 - SQL: Default value of a column to be addition of two columns 如何为外部连接产生的空值选择列中的前一个非空值 - How to chose the previous non null value in a column for null values resulting from an outer join
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM