简体   繁体   English

如何返回最近的记录

[英]How to return most recent record

I have two tables, Sales and SalesNotes , as below 我有两个表SalesSalesNotes ,如下所示

Sales
SO No......Cust.........Date  
1..........Me..........22-04-13  
2..........You.........23-04-13  



SalesNotes  

SO No.......Note.......Notedate  
1...........Blah.......24-04-13  
2...........Bleh.......23-04-13  
2...........Bluh.......27-04-13  

How can I return a result set showing Cust, date and the most recent dated note for the corresponding SO no? 如何返回显示相应SO号的客户,日期和最新日期注释的结果集?

I have tried using MAX() but cannot use an aggregate in the where clause and do not understand how I could implement HAVING to do what I need. 我尝试使用MAX()但无法在where子句中使用聚合,并且不了解如何实现HAVING以执行所需的操作。

What I am trying to achieve is: 我想要实现的是:

SO No.......Cust........Note  
1...........Me..........Blah    
2...........You.........Bluh  

One way of doing this is with the row_number window function: 一种方法是使用row_number窗口函数:

SELECT    s.[So no], [cust], [Note]
FROM      [Sales] s
LEFT JOIN (SELECT [So no], [Note],
                  ROW_NUMBER() OVER (PARTITION BY [So no]
                                     ORDER BY [Notedate] DESC) rn
           FROM   [SalesNotes]) n ON s.[So no] = n.[So no] AND rn = 1

You can use outer apply for this: 您可以为此使用outer apply

select s.*, sn.note
from sales s outer apply
     (select top 1 sn.*
      from salesnotes sn
      where sn.so_no = s.so_no
      order by sn.notedate desc
     ) sn;

You can use window function FIRST_VALUE to get the most recent Note value per [SO No.] : 您可以使用窗口函数FIRST_VALUE获取每个[SO No.]最新Note值:

SELECT s.[SO No.], Cust, Note
FROM Sales AS s
INNER JOIN (SELECT DISTINCT [SO No.], 
                   FIRST_VALUE(Note) OVER (PARTITION BY [SO No.] 
                                           ORDER BY NoteDate DESC) AS Note
            FROM SalesNotes) AS sn
ON s.[SO No.] = sn.[SO No.]

This way you avoid using a correlated subquery, which, I think, is less performant. 这样,您可以避免使用相关的子查询,我认为这种查询的性能较差。

FIRST_VALUE is available from SQL Server 2012+. SQL Server 2012+提供了FIRST_VALUE

I understand your output as last sale and last note for that sale. 我了解您的输出是该笔交易的最后一笔交易和最后一笔票据。 If you want to do this for all sales, just remove first Top 1 .You can do it with Apply : 如果要对所有销售都执行此操作,只需删除第一个Top 1 。您可以使用Apply

Select Top 1 s.SoNo, s.Cust, oa.Note
From Sales s
Outer Apply (Select Top 1 Note From Notes n Where n.SoNo = s.SoNo Order By Date Desc) oa
Order By s.Date desc

Something like this: 像这样:

select s.so_no, s.cust, 
(select top (1) n.note from salesnotes n where s.so_no = n.so_no order by notedate desc) as note
from sales s

Since you were talking about 'max' in where clause , this is just for you to understand 'max' . 由于您在where子句中讨论的是“ max”,因此这只是为了您了解“ max”。 And just so you know this is not a good solution because there are number of problems like what if notes are same for two different customer, what if customers have the same name and so on and so forth. 就是这样,您知道这不是一个好的解决方案,因为存在许多问题,例如两个不同客户的注释是否相同,客户具有相同名称的情况等等。 You should follow the outer apply concept as others have suggested for the right solution . 您应该遵循外部应用概念,就像其他人建议的正确解决方案一样。

Select s.SoNo, s.cust, sn.notes,           max(sn.noteDate) from sales s inner join salesnotes sn on s.sono=sn.sono group by s.sono,s.cust,sn.notes 

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

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