2012-10-16 56 views
0

有一個問題與查詢的特定部分:DATEDIFF子查詢返回在MySQL超過1行使用<code>DATEDIFF</code>

select 1 as NumApps, 
LenderInfo.Name as LenderName, 
CASE WHEN ApplicationInfo.AEType IS NULL OR ApplicationInfo.AEType = 0 THEN 'Unknown' ELSECONCAT (AEContact.FirstName, ' ', AEContact.LastName) END As AEName, 
CASE WHEN b.createdby > 0 THEN CONCAT (contactinfo.firstname, ' ', contactinfo.lastname) END AS LogActionBy, 
CASE WHEN ApplicationInfo.RecertificationById IS NULL THEN CASE WHEN ApplicationInfo.IsCorrespondent IS NULL OR ApplicationInfo.IsCorrespondent = 0 THEN 'Wholesale' ELSE 'Correspondent' END ELSE CASE WHEN ApplicationInfo.IsCorrespondent IS NULL OR ApplicationInfo.IsCorrespondent = 0 THEN 'Wholesale - Recert' ELSE 'Correspondent- Recert' END END As AppType, 
Applicationinfo.SubmissionDate, 
ApplicationInfo.ApplicationID, 
b.status AS LogStatus, 
b.CreatedOn as LogDate, 
companyinfo.Name, 
companyinfo.NMLSEntityID, 
applicationinfo.CreatedDate,   
ApplicationInfo.Status as ApplicationStatus, 
ApplicationInfo.StatusChangeDate, 
DATEDIFF ((Select CreatedOn From ApplicationStatusChangeLog a where a.ApplicationId = b.ApplicationId 
And a.status = 'Approved'),(Select CreatedOn From ApplicationStatusChangeLog a Where a.ApplicationId = b.ApplicationId And a.status = 'Pending Approval')) AS DaysToApprove 
from applicationinfo 
INNER JOIN CompanyInfo ON(ApplicationInfo.CompanyId = CompanyInfo.CompanyId) 
left join CompanyInfo As LenderInfo ON (ApplicationInfo.LenderId = LenderInfo.CompanyId) 
LEFT JOIN UserInfo ON (UserInfo.UserId = ApplicationInfo.LastModifiedById) 
LEFT JOIN ContactInfo ON UserInfo.ContactId = ContactInfo.ContactId 
LEFT JOIN ContactInfo AS Comergence ON(ApplicationInfo.ComergenceRepId = Comergence.ContactId) 
LEFT JOIN UserInfo AS AEUser ON(AEUser.UserId = ApplicationInfo.AEType) 
left join paymentinfo on applicationinfo.applicationid = paymentinfo.applicationid 
Inner join applicationstatuschangelog b on applicationinfo.applicationid = b.applicationid 
LEFT JOIN ContactInfo AS AEContact ON(AEContact.ContactId = AEUser.ContactId) 
LEFT JOIN UserInfo AS StatusUser ON(StatusUser.UserId = ApplicationInfo.StatusChangeById) 
LEFT JOIN ContactInfo AS StatusContact ON(StatusContact.ContactId = StatusUser.ContactId) 
LEFT JOIN ApprovalStatus ON(ApplicationInfo.ApplicationId = ApprovalStatus.ApplicationId AND ApplicationInfo.CompanyId = ApprovalStatus.CompanyHQId 
AND ApplicationInfo.CompanyId = ApprovalStatus.CompanyId) 
Where ApplicationInfo.Status NOT In ('Approved Monitor Only') 
AND LenderInfo.ActiveLenderContract = 1 
AND COALESCE(ApplicationInfo.IsDeleted,0) <> 1 
AND b.Status NOT IN ('Incomplete', 'Not Submitted') 
Group BY b.ApplicationID` 

我見過一些人使用IN代替=子查詢中提,但我似乎無法得到它的工作。那裏有任何想法?提前致謝。

+0

請出示整個查詢。我的猜測是,這種選擇內選擇不是最好的方法。 –

+0

編輯包含整個查詢 - 再次感謝您的幫助。 – user1750555

+0

「遇到問題」並不是什麼問題描述。請至少在這裏提出一個問題,人們可以回答一些問題。 (例如,示例數據,期望的輸出以及所獲得的輸出對於使SQL牆可以理解將有很大幫助。) –

回答

0

我能得到由該查詢確定狀態之後加入「限價1」的工作:

(Select CreatedOn From ApplicationStatusChangeLog a where a.ApplicationId = b.ApplicationId And a.status = 'Approved' LIMIT 1),(Select CreatedOn From ApplicationStatusChangeLog a Where a.ApplicationId = b.ApplicationId And a.status = 'Pending Approval' LIMIT 1)

相關問題