查询不返回结果

时间:2014-05-19 14:01:59

标签: sql sql-server sql-server-2008 where

我编写了以下查询,该查询返回了我最初需要的一些结果:

DECLARE @lastMonthDate VARCHAR(10)
DECLARE @currentMonthDate VARCHAR(10)
DECLARE @Year VARCHAR(10)

SET @lastMonthDate = CONVERT(VARCHAR(4), DATEPART(YEAR, DATEADD(MONTH, -1, GETDATE())))+'-'+CONVERT(VARCHAR(2), DATEPART(MONTH, DATEADD(MONTH, -1, GETDATE())))+'-21'
SET @currentMonthDate = CONVERT(VARCHAR(4), DATEPART(YEAR, GETDATE()))+'-'+CONVERT(VARCHAR(2), DATEPART(MONTH, GETDATE()))+'-20'
SET @Year = DATEPART(YEAR, GETDATE())

SELECT 
     DISTINCT 
     Comp_Name
     , comp_idnr 
     , oppo_date17_1sent
     ,datename(month, oppo_date17_1sent)
     , budg_tempDODate1
     , CONVERT(DECIMAL(10,2), budg_do1_total) AS budg_do1_total
     , budg_paymentMethodM_R
     , Commissions.dbo.Simplicity_Paid.RestructureFeeActualDate
     , CONVERT(DECIMAL(10,2),
                Commissions.dbo.Simplicity_Paid.RestructureFees) as RestructureFees
     , Commissions.dbo.Simplicity_Paid.RestructureFeeStatus
     , Channel.Chan_Description 

 FROM 
     Company
JOIN Commissions.dbo.Simplicity_Paid 
     on company.Comp_CompanyId = Commissions.dbo.Simplicity_Paid.CompanyID 
JOIN Opportunity 
     on Company.Comp_CompanyId = Opportunity.Oppo_PrimaryCompanyId
JOIN Budget 
     on  Opportunity.Oppo_OpportunityId = Budget.budg_OpportunityId
JOIN Users 
     on company.Comp_PrimaryUserId = User_UserId
Join Channel 
     on User_PrimaryChannelId = Channel.Chan_ChannelId

WHERE 
     CONVERT(DATE, RestructureFeeActualDate) BETWEEN CONVERT(DATE, @lastMonthDate) 
                                           AND CONVERT(DATE, @currentMonthDate) 
AND 
     User_FirstName = 'Joshua' 
AND 
     User_LastName = 'Niss' 
And 
     RestructureFeeStatus = 'Paid' 
AND 
     RestructureFees >= 900 
AND 
     Oppo_Deleted IS NULL 

随着项目的进行,需求现在已经改变,我需要在代码中添加一些SQL来获得我想要的结果。问题是......我没有得到我需要的东西。

我正在尝试添加以下内容:

AND comp_idnr NOT IN (SELECT ClientID FROM Commissions.dbo.AuditHistory)

但由于某些或其他原因,我的查询不再返回任何结果。 您可能正在考虑,因为ID在查询中。但我可以向您保证。我手动检查了数据,只找到了1条记录。

我一直在网上搜索类似于"为什么 NOT IN 声明不能正常工作",如何做其他等等。

My attempts include: Joins (LEFT & RIGHT), Exists and Not exists, IN vs NOT IN

我记得曾经有人告诉我,你构建WHERE子句的方式会输出不同的结果。所以这是我的问题,我猜,"我将如何构建我的where子句" ?或者你们也没有看到任何错误,我应该继续尝试不同的东西?

对不起,如果这是一个糟糕的帖子,我真的不知道还有什么可以尝试。我能为您提供哪些信息可以帮助您帮助我吗?

快速编辑:查询确实找到了IN子查询的1条记录。

0 个答案:

没有答案