【问题标题】:How to take Null values query expression using servlet and jsp如何使用 servlet 和 jsp 获取 Null 值查询表达式
【发布时间】:2017-06-08 06:50:19
【问题描述】:

我需要从包含NoOfErrorsNoOfAudited 列的表中的SQL Server 数据库表中计算和检索数据。

我需要根据这些列计算准确度。

我有这样的查询

select 
   a.id, 
   (100-((a.NoOfErrors*100)/ NULLIF(a.NoOfAudited,0))) as Accuracy 
from Table1 a 
 join Table2 pd 
   on a.batchid=pd.id 
where 
  a.charge='"+Poster+"' and status=1 

仅当NoOfErrorsNoOfAudited 列包含某些值时,才会执行上述查询。

我拥有所有值,但如果NoOfErrorsNoOfAudited 列中的任何一个值包含Null 值,则它不会检索任何数据,而是显示No data found

ResultSet 数据不接受空值

如何让查询接受空值并通过将jsp表中的空替换为空来检索数据?

【问题讨论】:

    标签: java sql-server jsp


    【解决方案1】:

    这条线有问题,应该更正。

       (100-((a.NoOfErrors*100)/ NULLIF(a.NoOfAudited,0))) as Accuracy 
    

    问题 #1

    NULLIF(a.NoOfAudited,0)
    

    如果NoOfAuditedNULL,那么表达式将返回0;基本上你被零除。这应该替换为1,如下所示

    NULLIF(a.NoOfAudited,1)
    

    问题 #2

    不检查 NoOfErrors 列的 NULL 值。 在 SQL Server 中,大多数 NULL 算法的结果是 NULL。 例如,以下所有查询都会产生 NULL

    select 100-NULL*100/0
    select NULL*100
    select NULL/0
    select 100-NULL
    

    因此,只要NoOfErrorsNULL,您就会得到NULLAccuracy

    要更正这部分,您应该编写查询表达式,如

       (100-((COALESCE(a.NoOfErrors,0)*100)/ COALESCE(a.NoOfAudited,1))) as Accuracy 
    

    请注意,我已将 NULLIF 替换为符合 ANSI 的 COALESCE,它将在 SQL 服务器和 MySQL 上运行

    TLDR;

    您的查询应该是这样的

    select 
       a.id, 
      case 
       when COALESCE(a.NoOfErrors,a.NoOfAudited,'') ='' 
       then''
       else 
        CAST((100-((COALESCE(a.NoOfErrors,0)*100)/ COALESCE(a.NoOfAudited,1))) as VARCHAR(10))as Accuracy
     end
    from Table1 a 
     join Table2 pd 
       on a.batchid=pd.id 
    where 
      a.charge='"+Poster+"' and status=1 
    

    【讨论】:

    • 嗨,@DhruvJoshi,删除 Null 值以便您使用它,但是当 both columns having the Null values then Accuracy showing 100 没有显示正确的准确性但 i need when both columns having Null values accuracy display in empty
    猜你喜欢
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多