Oracle SQL排除列为空的行

huangapple go评论48阅读模式
英文:

Oracle SQL exclude rows when columns are empty

问题

以下是您的Oracle SQL查询的翻译:

我有这个Oracle SQL查询,它选择包含值的多个列:

	SELECT DISTINCT
		 name,
		 age,
		 weight,
		 height
	FROM
		my_table

我想从报表中排除不包含任何值的行,但如果它们的值为0.00,则应报告。

**编辑:**我添加了我漏掉的另一种情况,只有当它们都不包含值时才应排除行。行d应该出现在报表中

示例:


name  |   age    |   weight |   height
------|---------:|---------:|---------:
a     |   10     |  20      |   105
b     |   0      |  0       |    0
c     |          |          |   
d     |          |    15    |   110

在执行SQL语句后,应报告如下:

name  |   age    |   weight |   height
------|---------:|---------:|---------:
a     |   10     |  20      |   105
b     |   0      |  0       |     0
d     |          |    15    |   110
英文:

I have this Oracle SQL query which selects several columns containing values:

SELECT DISTINCT
	 name,
	 age,
	 weight,
	 height
FROM
	my_table

I want to exclude rows from the report that contain no values but they should be reported if they have 0.00 as a value.

EDIT: I add another case that I missed, the rows should be only excluded if all of them contain no value. Row d should be in the report.

Example:

name age weight height
a 10 20 105
b 0 0 0
c
d 15 110

After the SQL statement, this should be reported:

name age weight height
a 10 20 105
b 0 0 0
d 15 110

答案1

得分: 1

以下是翻译好的部分:

应该是:

    选择不同的
         名字,
         年龄,
         体重,
         身高
    从
        我的表
    其中 长度(年龄) > 0 或 长度(体重) > 0 或 长度(身高) > 0
     或 年龄 不为 null 或 体重 不为 null 或 身高 不为 null

如果列的数据类型是数字,您可以仅检查 NULL 值。

    选择不同的
         名字,
         年龄,
         体重,
         身高
    从
        我的表
    其中 年龄 不为 null 或 体重 不为 null 或 身高 不为 null
英文:

It should be:

SELECT DISTINCT
     name,
     age,
     weight,
     height
FROM
    my_table
WHERE LENGTH(age) > 0 or LENGTH(weight) > 0 OR LENGTH(height) > 0
 OR age is not null or weight is not null or height is not null

If the data type of the columns is numeric, you can simply check for NULL values only.

SELECT DISTINCT
     name,
     age,
     weight,
     height
FROM
    my_table
WHERE age is not null or weight is not null or height is not null

答案2

得分: 1

你可以在 WHERE 子句中使用 Not Null 条件,

SELECT DISTINCT
     name,
     age,
     weight,
     height
FROM
    my_table
WHERE age is not null and weight is not null and height is not null;

如果你想查看具有任何一个度量值的记录,其他度量值为空,可以使用 'or' 替代 'and'。

英文:

You can use Not Null condition in a where clause,

SELECT DISTINCT
     name,
     age,
     weight,
     height
FROM
    my_table
where age is not null and weight is not null and height is not null;

You can use 'or' instead of 'and' if you want to see records which has values for any one of the metric and others have blank.

huangapple
  • 本文由 发表于 2023年2月27日 16:18:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定