计算' NA'在一行 - MySQL

时间:2014-11-11 16:22:22

标签: mysql

如果在两个不同的列中存在多于或等于2 NA的行,那么我想选择该行并显示其所有列。有没有SQL可以做到这一点?

原始数据:

 Overall_Score_ID   Company_ID  Operating_Margin_Score  Negative_Earning_Surprise_Score Acquisition_Score   TSR_Score   Enterprise_Score
 54                 399473107   0.0                     2.3770491803278686              5.666666666666666   NA          1.1475409836065573
 116                92847A200   0.3305785123966942      3.0327868852459017              8.75                NA          2.0491803278688527
 337                57164Y107   2.0689655172413794      8.85057471264368                7.558139534883721   NA          3.2558139534883725
 415                007767106   NA                      9.52                            NA                  NA          NA
 462                36112J107   NA                      1.28                            NA                  NA          NA

输出:

Overall_Score_ID    Company_ID  Operating_Margin_Score  Negative_Earning_Surprise_Score Acquisition_Score   TSR_Score   Enterprise_Score
415                 007767106   NA                      9.52                            NA                  NA          NA
462                 36112J107   NA                      1.28                            NA                  NA          NA

现在我只有一个查询来计算列数并显示> = 2的列,我知道它完全错误,但我想我可以将其修改为我想要的查询?

select * from overall_scores
group by overall_Score_id
having count(*) >=2;

2 个答案:

答案 0 :(得分:2)

这可能对您有用:

select *, 
((Operating_Margin_Score='NA') +
(Negative_Earning_Surprise_Score='NA') +
(Acquisition_Score='NA') +
(TSR_Score='NA') +
(Enterprise_Score='NA')) as total
from overall_scores having total >=2;

答案 1 :(得分:1)

通过更改字段名称和表名来尝试此查询。 它可以解决您的问题

SELECT * 
FROM  table1 
WHERE (length(trim(concat(field1,field2,field3))) - length(replace(trim(concat(field1,field2,field3)),'NA',''))) / 2 >= 2 

你可以在

上找到更多这样的例子

www.csnotes32.com