面对问题以获得适当的计数

时间:2018-02-27 12:16:49

标签: sql oracle count

我有一个“web.individual_usage_vw”的视图。 总人数= 39057。 SQL查询:select count(*) from web.individual_usage_vw;

在此视图中,它有几列具有数字数据。

所以我需要从视图中获取所有记录,其中数据是> 1。

所以我使用了下面的查询:

select count(*) from web.individual_usage_vw 
where "Business & economy" != 0
and "Executive rewards" != 0
and "Health & benefits" != 0
and "Investment" != 0
and "Corporate marketing" != 0
and "Retirement" != 0
and "In General" != 0
and "Mergers & acquisitions" != 0
and "Corporate strategy operations" != 0
and "Broad-based rewards" != 0
and "Leadership" != 0
and "Talent" != 0
and "Other" != 0;

这导致我计数 0

select count(*) from web.individual_usage_vw
where "Business & economy" = 0
and "Executive rewards" = 0
and "Health & benefits" = 0
and "Investment" = 0
and "Corporate marketing" = 0
and "Retirement" = 0
and "In General" = 0
and "Mergers & acquisitions" = 0
and "Corporate strategy operations" = 0
and "Broad-based rewards" = 0
and "Leadership" = 0
and "Talent" = 0
and "Other" = 0;

结果我有count = 36228

我无法调试此错误。

1 个答案:

答案 0 :(得分:1)

结果没有错。第二个查询仅在所有列(在谓词中)为0时获取行。在第一个查询中,您将获取所有列都具有非零值的行。丢失的记录(delta)可能包含一个或多个结果为零的列。

例如

<?php

if(isset($_POST['submit'])){
echo $value = $_POST["make"];
echo $text = $_POST["make_text"];

}
?>
<form method=post>
<select name='make' onchange="setTextField(this)">
<option value = '' > Please Select Value </option>
<option value = '5'> Text 5 </option>
<option value = '7'> Text 7 </option>
<option value = '9'> Text 9 </option>
</select>
<br/>
<input type=submit name=submit>

<input id="make_text" type = "hidden" name = "make_text" />
<script type="text/javascript">
    function setTextField(ddl) {
        document.getElementById('make_text').value = ddl.options[ddl.selectedIndex].text;
    }
</script>
</form>

因此存在差异。如果您需要返回第二个查询中未返回的所有行,请使用C1, C2 0 0 1 0 1 1 0 1 select * from t1 where c1 = 0 and c2=0; -- returns one row select * from t1 where c1 != 0 and c2!=0; -- returns one row and not three

粗略地说,这就是你所需要的。

NOT EXISTS
相关问题