我正在创建的报告显示了在指定日期范围内向其发送特定类型的信件('212')的情况列表。
目前我还有一些联接来展示我的其他专栏,但我遇到问题的一个方面是当我尝试使用“Documents”表时>“案例”表。在我添加连接之前,每个案例都是唯一的,但是只要我加入“Documents”表,就会有多个相同案例的实例,因为在“Documents”表中每个案例都创建了这封信的许多实例。我只需要提出那封信的最新创作日期。
选择
[casCaseID] as [Case ID],
[cliForename] + [clisurname] as [Client Name],
[rusFullname] as [Adviser Name],
[docDateCreated] as [Date EA Letter Sent],
[camloan],
CASE
WHEN isnull(camLoan,0) = 0 or isnull(campurchaseprice,0) = 0 THEN 0
ELSE [camLoan]/[campurchaseprice]
END as 'Loan To Value %'
FROM [MPS3].[dbo].[Cases]
inner join [MPS3].[dbo].[Clients] on casPrimaryClientID=cliClientID
inner join [MPS3].[dbo].[RefUsers] on casAdviserID=rusUserID
inner join [MPS3].[dbo].[RefSources] on casSourceID=rsoSourceID
inner join [MPS3].[dbo].[CasesMortgage] on casCaseID=camCaseID
inner join [MPS3].[dbo].[Documents] on casCaseID=docCaseID and docLetterID =212
where docDateCreated between @fromDate and @toDate
我尝试使用'MAX'在内连接上使用子查询,但无济于事。
inner join [MPS3].[dbo].[Documents]
on casCaseID=docCaseID
and docLetterID =212
and docdatecreated = (select max(docdatecreated)
from [MPS3].[dbo].[Documents]
where casCaseID=docCaseID
group by cascaseid)
我出错的任何想法?
谢谢!
答案 0 :(得分:3)
在与MAX()
的联接中,尝试添加联接中的相同字段检查。
inner join [MPS3].[dbo].[Documents]
on casCaseID=docCaseID
and docLetterID =212
and docdatecreated = (select max(docdatecreated)
from [MPS3].[dbo].[Documents]
where casCaseID = docCaseID
and docLetterID = 212)
答案 1 :(得分:2)
SELECT
[casCaseID] as [Case ID],
[cliForename] + [clisurname] as [Client Name],
[rusFullname] as [Adviser Name],
MAX([docDateCreated]) as [Date EA Letter Sent],
[camloan],
CASE
WHEN isnull(camLoan,0) = 0 or isnull(campurchaseprice,0) = 0 THEN 0
ELSE [camLoan]/[campurchaseprice]
END as 'Loan To Value %'
FROM [MPS3].[dbo].[Cases]
inner join [MPS3].[dbo].[Clients] on casPrimaryClientID=cliClientID
inner join [MPS3].[dbo].[RefUsers] on casAdviserID=rusUserID
inner join [MPS3].[dbo].[RefSources] on casSourceID=rsoSourceID
inner join [MPS3].[dbo].[CasesMortgage] on casCaseID=camCaseID
inner join [MPS3].[dbo].[Documents] on casCaseID=docCaseID and docLetterID =212
where docDateCreated between @fromDate and @toDate
GROUP BY
[casCaseID],
[cliForename] + [clisurname],
[rusFullname],
[camloan],
CASE
WHEN isnull(camLoan,0) = 0 or isnull(campurchaseprice,0) = 0 THEN 0
ELSE [camLoan]/[campurchaseprice]
END
答案 2 :(得分:0)
你可以这样做:
[casCaseID] as [Case ID],
[cliForename] + [clisurname] as [Client Name],
[rusFullname] as [Adviser Name],
(select max(docdatecreated)
from [MPS3].[dbo].[Documents]
where casCaseID=docCaseID
and docDateCreated between @fromDate and @toDate
group by cascaseid)
as [Date EA Letter Sent],
[camloan],
CASE
WHEN isnull(camLoan,0) = 0 or isnull(campurchaseprice,0) = 0 THEN 0
ELSE [camLoan]/[campurchaseprice]
END as 'Loan To Value %'
FROM [MPS3].[dbo].[Cases]
inner join [MPS3].[dbo].[Clients] on casPrimaryClientID=cliClientID
inner join [MPS3].[dbo].[RefUsers] on casAdviserID=rusUserID
inner join [MPS3].[dbo].[RefSources] on casSourceID=rsoSourceID
inner join [MPS3].[dbo].[CasesMortgage] on casCaseID=camCaseID
inner join [MPS3].[dbo].[Documents] on casCaseID=docCaseID and docLetterID =212