简体   繁体   English

选择SQL连接语句上一对多关系的最上面匹配记录

[英]Selecting the top matching record of a one to many relationship on a SQL join statement

I have a question that will probably something simple but I am not a SQL expert by any stretch. 我有一个问题,可能会很简单,但无论如何我都不是SQL专家。 I have this query that has 3 join, the 3rd one is a one to many relationship and I am trying to get the top 1. Which in this case would be the last edit on a support call. 我的查询具有3个联接,第3个是一对多关系,并且我试图获得前1个。在这种情况下,这将是对支持电话的最后一次编辑。

The info comes for 4 tables, CASE, for the basic case details, CONTACT1 and CONTACT2 for the contacts information and NOTES for the notes of said case, This NOTES table creates 1 entry for each notes entered in that case. 该信息来自4个表,其中CASE用于基本案例详细信息,CONTACT1和CONTACT2用于联系信息,以及该案例注释的NOTES,该NOTES表为在这种情况下输入的每个注释创建1个条目。 I want to create a dashboard that will include that last entry time stamp. 我想创建一个仪表板,其中将包含最后一个输入时间戳。

I tried to add a Select top (1) within the join, tried all possible options for the join and use Max (C3.MODIFIEDDATE) with Group By but I always end up with all the records in NOTES for each ticket. 我试图在联接中添加一个选择顶部(1),尝试了联接的所有可能选项,并将Max(C3.MODIFIEDDATE)与Group By一起使用,但是我总是以每张票证中NOTES的所有记录为最终结果。

Here is my Query: 这是我的查询:

SELECT c.NUMBER as [Case Number], c.OWNER as [Assigned To], case c.status when 0 then '<unknown>' when 1 then 'Assigned' when 2 then 'Reassigned' when 3 then 'Escalated' when 4 then 'Resolved' 
else cast(c.status as varchar(50)) end as [Case Status], c.SUBJECT as [Subject], c.OFFERING as [Next Step], DATEADD(day,DATEDIFF(day,0,c.CREATED_DATE),0) as [Created on], c1.CONTACT 
as [Primary Contact], c1.COMPANY as [Company], c3.MODIFIEDDATE as [Last Edit], c3.MODIFIEDBY as [Last Agent]

FROM CASES c left join(CONTACT1 c1 inner join CONTACT2 c2 on c1.ACCOUNTNO = c2.ACCOUNTNO ) on c1.ACCOUNTNO = c.ACCOUNTNO 
   left join NOTES as c3 on c3.LOPRECID =c.recid -- That's the one that has a 1 to many resolve


WHERE (  c.IS_TEMPLATE = 0  AND   c.STATUS IN ( 1,2,3 ) and c3.rectype='CS' )
ORDER BY [Company] asc, c3.modifieddate desc

Any suggestion would be apreciated 任何建议将不胜感激

one way (there are others as well) replace 一种方式(也有其他方式)替换

left join NOTES as c3 on c3.LOPRECID =c.recid

with

OUTER APPLY (SELECT TOP 1 MODIFIEDDATE, MODIFIEDBY 
             FROM NOTES AS N 
             WHERE N.LOPRECID = c.recid AND N.rectype='CS'
             ORDER BY MODIFIEDDATE DESC
) AS c3

and remove the c3.rectype='CS' from your WHERE clause. 并从WHERE子句中删除c3.rectype='CS'

You will need to ensure this gives the desired results in the case where there are no rows in NOTES that satisfy the rectype='CS' requirement, since that adds a little sand in the gears for what you're asking. 在NOTES中没有满足rectype ='CS'要求的行的情况下,您将需要确保获得期望的结果,因为这会为您的要求在齿轮中添加一些沙子。

LEFT JOIN 
(
   SELECT LOPRECID, MODIFIEDDATE, MODIFIEDBY, 
     rn = ROW_NUMBER() OVER (PARTITION BY LOPRECID ORDER BY MODIFIEDDATE DESC)
   FROM dbo.Notes
) AS C3
ON C3.LOPRECID = c.recid AND C3.rn = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM