当我知道有多个命中时,为什么where子句只返回一个结果?

时间:2014-02-10 18:12:52

标签: java sql-server dateadd

当我知道数据库中的记录满足3个日期部分的条件时,只返回一条记录。这可能是和/或的链接吗?返回的一条记录属于它的适当范围,这使得我责备db没有一条记录符合标准,但在检查db之后我应该在结果集中有3条记录。

    Connection conn = null;
    Statement stmt = null;
    ResultSet result_set = null;
    try {
        Class.forName(JDBC_DRIVER);
        conn = DriverManager.getConnection(DB_URL);
        stmt = conn.createStatement();

        String terminated = "SELECT distinct b.new_TagID, c.AccountId, c.Name, a.OwnerId, b.new_SwWarrantyEndDate,"
                + " a.JobTitle, a.FirstName, a.LastName, a.EMailAddress1, a.new_Support_RenewalContact, a.new_Supported"
                + "RenevalContact, d.YomiFullName, d.InternalEMailAddress,"
                + " (SELECT InternalEMailAddress from dbo.SystemUserBase where SystemUserId=e.new_VPId) new_VPId,"
                + " (SELECT InternalEMailAddress from dbo.SystemUserBase where SystemUserId=e.new_RVPId) new_RVPId,"
                + "b.new_RenewalOpptyIDyr2,b.new_RenewalOpptyIDyr3,b.new_RenewalOpptyIDyr4,b.new_RenewalOpptyIDyr5, "
                + " Case when b.new_LevelofSupport = 100000000 then 'Platinum' "
                +        "when b.new_LevelofSupport = 100000001 then 'Standard' "
                +        "when b.new_LevelofSupport = 100000002 then 'NONE' "
                +        "when b.new_LevelofSupport = 100000003 then 'Standard with AR' end as LeveloFSupport "
                + "FROM dbo.contact as a, dbo.new_assetExtensionBase as b, dbo.AccountBase as c, dbo.SystemUserBase as d, dbo.SystemUserExtensionBase as e "
                + "WHERE b.new_account=a.AccountId AND a.AccountId=c.AccountId AND c.OwnerId=d.SystemUserId AND d.SystemUserId=e.SystemUserId AND "

                + "((b.new_SwWarrantyEndDate >= dateadd(day,27,(CONVERT (date, SYSUTCDATETIME()))) AND "
                + "b.new_SwWarrantyEndDate <= dateadd(day,33,(CONVERT (date, SYSUTCDATETIME())))) OR"

                + "(b.new_SwWarrantyEndDate >= dateadd(day,57,(CONVERT (date, SYSUTCDATETIME()))) AND "
                + "b.new_SwWarrantyEndDate <= dateadd(day,63,(CONVERT (date, SYSUTCDATETIME())))) OR "

                + "(b.new_SwWarrantyEndDate >= dateadd(day,87,(CONVERT (date, SYSUTCDATETIME()))) AND "
                + "b.new_SwWarrantyEndDate <= dateadd(day,93,(CONVERT (date, SYSUTCDATETIME())))) OR "

                + "(b.new_SwWarrantyEndDate >= dateadd(day,177,(CONVERT (date, SYSUTCDATETIME()))) AND "
                + "b.new_SwWarrantyEndDate <= dateadd(day,183,(CONVERT (date, SYSUTCDATETIME())))) OR "

                + "(b.new_SwWarrantyEndDate >= dateadd(day,-33,(CONVERT (date, SYSUTCDATETIME()))) AND "
                + "b.new_SwWarrantyEndDate <= dateadd(day,-27,(CONVERT (date, SYSUTCDATETIME())))) OR "

                + "(b.new_SwWarrantyEndDate >= dateadd(day,-63,(CONVERT (date, SYSUTCDATETIME()))) AND "
                + "b.new_SwWarrantyEndDate <= dateadd(day,-57,(CONVERT (date, SYSUTCDATETIME())))) OR "

                + "(b.new_SwWarrantyEndDate >= dateadd(day,-93,(CONVERT (date, SYSUTCDATETIME()))) AND "
                + "b.new_SwWarrantyEndDate <= dateadd(day,-87,(CONVERT (date, SYSUTCDATETIME())))) OR "

                + "(b.new_SwWarrantyEndDate >= dateadd(day,-3,(CONVERT (date, SYSUTCDATETIME()))) AND "
                + "b.new_SwWarrantyEndDate <= dateadd(day,3,(CONVERT (date, SYSUTCDATETIME()))))) "

                + "AND a.new_Support_RenewalContact=1 order by c.AccountId,a.new_Support_RenewalContact desc;";

        DataStore dataStore = new DataStore();
        result_set = stmt.executeQuery(terminated);

        while(result_set.next()){
            String newTagId,accountId,name,ownerId,newSoftwareWarrantyEndDate,jobTitle,firstName,lastName,
            emailAddress1,newSupportRenewalContact,newSupportedRenevalContact,yomiFullName,internalEmailAddress,newVPId,
            new_RVPId,newRenewalOpptyIdYr2,newRenewalOpptyIdYr3,newRenewalOpptyIdYr4,newRenewalOpptyIdYr5,levelOfSupport="";
            int remainingPeriod=0;

            newTagId=accountId=name=ownerId=newSoftwareWarrantyEndDate=jobTitle=firstName=lastName=emailAddress1=
                    newSupportRenewalContact=newSupportedRenevalContact=yomiFullName=internalEmailAddress=newVPId=new_RVPId=newRenewalOpptyIdYr2=
                    newRenewalOpptyIdYr3=newRenewalOpptyIdYr4=newRenewalOpptyIdYr5=levelOfSupport="";

            newTagId = result_set.getString("new_TagID");accountId = result_set.getString("AccountId");name = result_set.getString("Name");ownerId = result_set.getString("OwnerId");
            newSoftwareWarrantyEndDate = result_set.getString("new_SwWarrantyEndDate");jobTitle = result_set.getString("JobTitle");firstName = result_set.getString("FirstName");
            lastName = result_set.getString("LastName");emailAddress1 = result_set.getString("EMailAddress1");newSupportRenewalContact = result_set.getString("new_Support_RenewalContact");
            newSupportedRenevalContact = result_set.getString("new_SupportedRenevalContact");yomiFullName = result_set.getString("YomiFullName");internalEmailAddress = result_set.getString("InternalEMailAddress");
            newVPId = result_set.getString("new_VPId");new_RVPId = result_set.getString("new_RVPId");newRenewalOpptyIdYr2 = result_set.getString("new_RenewalOpptyIDyr2");
            newRenewalOpptyIdYr3 = result_set.getString("new_RenewalOpptyIDyr3");newRenewalOpptyIdYr4 = result_set.getString("new_RenewalOpptyIDyr4");
            newRenewalOpptyIdYr5 = result_set.getString("new_RenewalOpptyIDyr5");levelOfSupport = result_set.getString("LevelofSupport");

1 个答案:

答案 0 :(得分:0)

您是否尝试过直接在MSSQL Studio中运行查询?如果是这样 - 如果它返回相同的结果 - 尝试注释掉WHERE子句并查看会发生什么。然后一次添加一个条款,直到找到罪魁祸首。

如果是分组问题,缩进子句组也会帮助您找到错误。