简体   繁体   English

同一表中的多项选择(具有Null)

[英]Multiple Selects (with Null) from the same table

So I have to write a SQL in DB2, and I cant figure out how to do it. 因此,我必须在DB2中编写一个SQL,但我不知道该怎么做。 Something like pick these field codes and their values from the Finance Table as long as they are above $10,000 只要它们超过10,000美元,就可以从Finance Table中选择这些域代码及其值。

Select 
(A.Value),
(B.Value),
(C.Value)

...

From 
Client  K,
Finance A,
Finance B,
Finance C
...

The problem is in the where statement. 问题出在where语句中。 I cannot put: 我不能放:

Where
K.Client  = A.Client
AND A.FieldCode = 1
AND A.Value > 10000
AND K.Client  = B.Client
AND B.FieldCode = 2
AND B.Value > 10000
...

and so on... because that doesnt include Nulls, so it drastically reduces the Result Set, the more times I call the Finance table. 等等...因为不包含Null,所以我调用Finance表的次数越多,结果集就大大减少。

How do I keep the above formatting and get it to include nulls so that it will display the line as long as either Finance A OR Finance B OR Finance C etc exists? 我如何保持上述格式并使其包含空值,以便只要存在Finance A或Finance B或Finance C等,它就会显示该行?

(Note: doing the first most obvious thing like repeatedly calling the Finance table once, say Finance A, but no B,C,D etc does not work for this problem because the results (from A,B,C,D etc) cannot be spaced out over many lines). (注意:首先要做的最明显的事情,例如重复调用一次Finance表,例如Finance A,但没有B,C,D等不适用于此问题,因为结果(来自A,B,C,D等)无法相隔多行)。

This is a compressed version of what I am doing: 这是我正在做的压缩版本:

SELECT 
  A.CLIENT_ID,
  A.PERIOD_ID,
  FN0.AMOUNT,
  FN2.AMOUNT
FROM ASSESMENT A
LEFT OUTER JOIN FINANCE FN0
     ON A.CLIENT_ID = FN0.CLIENT_ID
    AND A.PERIOD_ID = FN0.PERIOD_ID
LEFT OUTER JOIN FINANCE FN1
     ON A.CLIENT_ID = FN1.CLIENT_ID
    AND A.PERIOD_ID = FN1.PERIOD_ID
WHERE
  FN0.FLD_CD  = 1258860
 AND FN1.FLD_CD = 1258861

The problem Im still having is, if I blank out the FN1 related lines, I get a lot more returned values. 我仍然遇到的问题是,如果我删掉FN1相关行,则会得到更多的返回值。 AKA it is still not including NULL values, and only returning values if ALL field codes have values. 同样,它仍然不包括NULL值,并且仅在所有域代码都具有值的情况下才返回值。

You need Left Outer Join instead of Comma separated INNER Join 您需要Left Outer Join而不是逗号分隔的内INNER Join

SELECT A.Value,
       B.Value,
       C.Value
FROM   Client K
       LEFT JOIN Finance A
              ON K.Client = A.Client
                 AND A.Value > 10000
                 AND A.FieldCode = 1
       LEFT JOIN Finance B
              ON K.Client = B.Client
                 AND B.Value > 10000
                 AND B.FieldCode = 2
       LEFT JOIN Finance C
              ON K.Client = c.Client 
                 ......

Update : 更新:

Move the Left table filters to ON condition else it will be implicitly converted to INNER JOIN . 将Left table过滤器移到ON条件,否则它将隐式转换为INNER JOIN Try like this 这样尝试

SELECT A.client_id, 
       A.period_id, 
       FN0.amount, 
       fn2.amount 
FROM   assesment A 
       LEFT OUTER JOIN finance FN0 
                    ON A.client_id = FN0.client_id 
                       AND A.period_id = FN0.period_id 
                       AND FN0.fld_cd = 1258860 
       LEFT OUTER JOIN finance FN1 
                    ON A.client_id = FN1.client_id 
                       AND A.period_id = FN1.period_id 
                       AND FN1.fld_cd = 1258861 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM