如果任何列包含 x 值,如何过滤 table
How to filter table by if any of the columns contains a x value
如何编写 where 子句过滤等于 Not or NO
.
的任何行
挑战是,数据有很多列,所以我需要避免为每一列写一个特定的位置,比如 where a not in ("Not","NO") and b a not in ("Not","NO")
等等。
这不起作用,因为列太多了。
Table:
name a b c d e (so on... there are lots more column)
joe good yes Not NO yes
mike yes yes yes good well
louis Not yes NO yes NO
jhon yes NO well Not yes
期望的输出:
name a b c d e
mike yes yes yes good well
使用以下方法
select *
from `project.dataset.table` t
where not regexp_contains(format('%T', t), r'(?i)"not"|"no"')
等式具有对称性,所以你也可以这样做
where 'Not' not in (a,b,c,d,e) and 'No' not in (a,b,c,d,e);
试试这个解决方案。
样本Table:
SELECT * FROM [dbo].MainTable
解决方案:
DECLARE @TableName nvarchar(256) = 'MainTable';
DECLARE @sql nvarchar(max) = '';
SELECT @sql = @sql +
CASE
WHEN @sql = '' THEN ''
ELSE ' AND '
END + '[' + cols.COLUMN_NAME +']' + ' NOT IN (''Not'',''No'')'
FROM INFORMATION_SCHEMA.COLUMNS cols
WHERE cols.TABLE_NAME = @TableName;
select @sql = 'SELECT * FROM ' + @TableName + ' WHERE ' + @sql
execute(@sql)
结果:
如何编写 where 子句过滤等于 Not or NO
.
挑战是,数据有很多列,所以我需要避免为每一列写一个特定的位置,比如 where a not in ("Not","NO") and b a not in ("Not","NO")
等等。
这不起作用,因为列太多了。
Table:
name a b c d e (so on... there are lots more column)
joe good yes Not NO yes
mike yes yes yes good well
louis Not yes NO yes NO
jhon yes NO well Not yes
期望的输出:
name a b c d e
mike yes yes yes good well
使用以下方法
select *
from `project.dataset.table` t
where not regexp_contains(format('%T', t), r'(?i)"not"|"no"')
等式具有对称性,所以你也可以这样做
where 'Not' not in (a,b,c,d,e) and 'No' not in (a,b,c,d,e);
试试这个解决方案。
样本Table:
SELECT * FROM [dbo].MainTable
解决方案:
DECLARE @TableName nvarchar(256) = 'MainTable';
DECLARE @sql nvarchar(max) = '';
SELECT @sql = @sql +
CASE
WHEN @sql = '' THEN ''
ELSE ' AND '
END + '[' + cols.COLUMN_NAME +']' + ' NOT IN (''Not'',''No'')'
FROM INFORMATION_SCHEMA.COLUMNS cols
WHERE cols.TABLE_NAME = @TableName;
select @sql = 'SELECT * FROM ' + @TableName + ' WHERE ' + @sql
execute(@sql)
结果: