SQL选择不同的排除NULL行

问题描述

SQL查询:

SELECT DISTINCT [File Version Number],[Enrollment Employee ID] 
FROM Participant

结果:

+---------------------+------------------------+
| Fule Version Number | Enrollment Employee ID |
+---------------------+------------------------+
| null                | null                   |
| null                | 1100527                |
| null                | 5032506                |
| v2.2.0              | null                   |
+---------------------+------------------------+

所需结果:排除具有NULL数据的行;第一行。

我知道我们可以在两列上使用where子句来过滤空行,但是如果我们要选择大量的列并且我们不想使用长where子句怎么办?

任何解决方案或解决方法将不胜感激。谢谢。

解决方法

如果要排除所有值为NULL的行,则需要WHERE子句:

Select Distinct [File Version Number],[Enrollment Employee ID] 
From Participant
where [File Version Number] is not null or [Enrollment Employee ID] is not null;

如果您关心的是编写查询,则可以通过使用数据库的元数据表来简化此操作。您可以使用SQL或电子表格或其他工具查询“列”元数据以构造where子句。

,

如果要在纯SQL(无动态SQL)中执行此操作,则将需要枚举列名。

基本解决方案是使用or条件:

where col1 is not null or col2 is not null ... or coln is not null;

您还可以使用coalesce()-HABO推荐的方式:

where coalesce(col,col2,...,coln) is not null

concat_ws()也浮现在脑海-与coalesce()的逻辑确实相同:

where concat_ws(col1,coln) is not null

最后,我们还可以使用cross apply取消透视,然后聚合:

select ...
from participant p
cross apply (
    select count(col) cnt
    from (values (p.col1),(p.col2),(p.coln)) x(col)
) x
where cnt > 0
,

您可以尝试使用INNER JOIN,例如:

sequences = pad_sequences(
    tokenizer.texts_to_sequences(dummy_docs),padding='post'
)

embedding_dim = 8
embedding_layer = Embedding(
    input_dim=len(tokenizer.word_index) + 1,output_dim=embedding_dim,input_shape=(sequences.shape[1],)
)

model = Sequential(
    [
        embedding_layer,LSTM(6,activation='tanh'),RepeatVector(sequences.shape[1]),activation='tanh',return_sequences=True),Dense(embedding_dim)
    ]
)

model.compile(loss='mse',optimizer='adam',metrics=['accuracy'])
model.summary()

y = embedding_layer(sequences)
model.fit(sequences,y,epochs=10,batch_size=1)

y_hat = model.predict(sequences)

但是在这种情况下,您将不得不加入任何可能包含NULL值的每个字段。

所以我建议您仍然应该添加where语句:

SELECT DISTINCT [File Version Number],[Enrollment Employee ID] 
FROM Participant AS p1
INNER JOIN Participant AS p2 ON p1.[Enrollment Employee ID] = p2.[Enrollment Employee ID]
WHERE FileVersionNumber IS NOT NULL AND EnrollmentEmployeeID IS NOT NULL;

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...