where By条件不适用于Group By Rollup

时间:2016-04-14 03:39:36

标签: sql oracle

运行此SQL时出现问题:

Create Table test_temp (
   gsid Number,
   sl Number);
Insert Into test_temp Values(53010100,15);
Insert Into test_temp Values(53010000,10);
Insert Into test_temp Values(53000000,5);

Select * From (
Select Sum(sl), gsid, substr(gsid, 0, 4) sj_gsid, substr(gsid, 0, 2) pro_gsid
From test_temp
Group By Rollup(substr(gsid, 0, 2), substr(gsid, 0, 4), gsid))
Where  sj_gsid <>'5300'

在Oracle 11g R2(Linux)上。

我认为会有5行返回。像这样:

what I wanted

但是,最后,我得到3行返回:

wrong result

发生了什么事?

1 个答案:

答案 0 :(得分:0)

您只返回三行,因为您丢失的两行对于sj_gsid为NULL。你明白NULL&lt;&gt; &#39; 5300&#39;不是TRUE,而是UNKNOWN,只返回条件为TRUE的行,对吗?如果您希望包含这些内容,则必须将make_operator(op, *args, doSave=True)添加到or sj_gsid is null条件

相关问题