如何比较给定数据集的最新两个值

时间:2018-06-15 21:57:24

标签: sql-server select sql-server-2008-r2

我需要一个查询,它会为每个发布MAX(PublishDate)书籍的作者提供最新的作者,发布者和书籍,并比较每位作者最新出版的2本书的出版商,看看它是否已经更改。它不必是单个语句,但不能创建任何物理表。

declare @books TABLE(Author nvarchar(30), PublishDate datetime2(3), Publisher nvarchar(30), Title nvarchar(30))

INSERT INTO @books
VALUES  ('Author A', '15 June 2017 15:47', 'Publisher A', 'Book 1'),
        ('Author A', '15 May 2016 14:47', 'Publisher B', 'Book 2'),
        ('Author B', '15 May 2016 14:47', 'Publisher C', 'Book 3'),
        ('Author B', '15 April 2015 13:47', 'Publisher D', 'Book 4'),
        ('Author C', '15 June 2017 15:47', 'Publisher E', 'Book 5'),
        ('Author C', '15 May 2014 14:47', 'Publisher E', 'Book 6'),
        ('Author D', '15 June 2017 15:47', 'Publisher F', 'Book 7'),
        ('Author D', '15 May 2013 14:47', 'Publisher F', 'Book 8'),
        ('Author E', '15 June 2017 15:47', 'Publisher G', 'Book 9'),
        ('Author E', '15 May 2012 14:47', 'Publisher H', 'Book 10'),
        ('Author E', '15 April 2011 13:47', 'Publisher I', 'Book 11')


Output:  
Author         Publisher          PublisherChanged          Book
Author A       Publisher A        1                         Book 1
Author C       Publisher E        0                         Book 5
Author D       Publisher F        0                         Book 7
Author E       Publisher G        1                         Book 9

1 个答案:

答案 0 :(得分:1)

2008 R2

您必须自己创建一个关系,我在这里使用row_number()基本上将表连接到自身。如果它是一个大表,你可能想要使用临时表而不是cte。我将出版商与排名第1的排名与排名第2的出版商进行比较:

declare @books table (Author nvarchar(30), PublishDate datetime2(3), Publisher nvarchar(30), Title nvarchar(30))

insert into @books
values  ('Author A', '15 June 2017 15:47', 'Publisher A', 'Book 1'),
        ('Author A', '15 May 2016 14:47', 'Publisher B', 'Book 2'),
        ('Author B', '15 May 2016 14:47', 'Publisher C', 'Book 3'),
        ('Author B', '15 April 2015 13:47', 'Publisher D', 'Book 4'),
        ('Author C', '15 June 2017 15:47', 'Publisher E', 'Book 5'),
        ('Author C', '15 May 2014 14:47', 'Publisher E', 'Book 6'),
        ('Author D', '15 June 2017 15:47', 'Publisher F', 'Book 7'),
        ('Author D', '15 May 2013 14:47', 'Publisher F', 'Book 8'),
        ('Author E', '15 June 2017 15:47', 'Publisher G', 'Book 9'),
        ('Author E', '15 May 2012 14:47', 'Publisher H', 'Book 10'),
        ('Author E', '15 April 2011 13:47', 'Publisher I', 'Book 11');

declare @max_date date = (
        select max(PublishDate)
        from @books
        );

with EvaluateChanges (
    Author
    ,PublishDate
    ,Publisher
    ,Title
    ,DateRanking
    )
as (
    select Author
        ,PublishDate
        ,Publisher
        ,Title
        ,row_number() over (partition by Author order by PublishDate desc)
    from @books
    )
select e1.Author
    ,e1.Publisher
    ,case when isnull(e2.Publisher, e1.Publisher) <> e1.Publisher then 1 else 0 end as PublisherChanged
    ,e1.Title
from EvaluateChanges as e1
left join EvaluateChanges as e2
    on e1.Author = e2.Author
        and e2.DateRanking = 2
where cast(e1.PublishDate as date) = @max_date
    and e1.DateRanking = 1;

2012年起

这是2012年的解决方案。我使用2012年引入的lead()函数帮助分析作者的上一行。 isnull的原因是,lead()nullnull当在分区中没有要比较的前一行时。Publisher当它是cast时,我告诉评估假设没有变化,通过为当前行上下文赋予与datetime相同的值。我为您的最长日期使用了日期变量,而declare @books table (Author nvarchar(30), PublishDate datetime2(3), Publisher nvarchar(30), Title nvarchar(30)) insert into @books values ('Author A', '15 June 2017 15:47', 'Publisher A', 'Book 1'), ('Author A', '15 May 2016 14:47', 'Publisher B', 'Book 2'), ('Author B', '15 May 2016 14:47', 'Publisher C', 'Book 3'), ('Author B', '15 April 2015 13:47', 'Publisher D', 'Book 4'), ('Author C', '15 June 2017 15:47', 'Publisher E', 'Book 5'), ('Author C', '15 May 2014 14:47', 'Publisher E', 'Book 6'), ('Author D', '15 June 2017 15:47', 'Publisher F', 'Book 7'), ('Author D', '15 May 2013 14:47', 'Publisher F', 'Book 8'), ('Author E', '15 June 2017 15:47', 'Publisher G', 'Book 9'), ('Author E', '15 May 2012 14:47', 'Publisher H', 'Book 10'), ('Author E', '15 April 2011 13:47', 'Publisher I', 'Book 11'); declare @max_date date = ( select max(PublishDate) from @books ); with EvaluateChanges ( Author ,PublishDate ,Publisher ,Title ,PublisherChanged ) as ( select Author ,PublishDate ,Publisher ,Title ,case when isnull(lead(Publisher) over ( partition by author order by PublishDate desc ), Publisher) <> Publisher then 1 else 0 end from @books ) select Author ,Publisher ,PublisherChanged ,Title from EvaluateChanges where cast(PublishDate as date) = @max_date; 使用了日期变量,因为您的字段为lead()

lag()

更多关于import UIKit import MessageUI class ViewController: UIViewController, MFMailComposeViewControllerDelegate { override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) let mailComposer = MFMailComposeViewController() mailComposer.mailComposeDelegate = self let recipients = ["fresher40@gmail.com"] mailComposer.setToRecipients(recipients) mailComposer.setSubject("Title") mailComposer.setMessageBody("Hello", isHTML: false) if MFMailComposeViewController.canSendMail() { self.present(mailComposer, animated: true, completion: nil) } } func mailComposeController( _ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error? ) { controller.dismiss(animated: true, completion: nil) } } 及其堂兄import UIKit import MessageUI class MailSender: NSObject, MFMailComposeViewControllerDelegate { private var mailComposer = MFMailComposeViewController() init(email: String) { super.init() mailComposer.mailComposeDelegate = self let recipients = [email] mailComposer.setToRecipients(recipients) mailComposer.setSubject("Title") mailComposer.setMessageBody("Hello", isHTML: false) } func presentOverViewController(viewController: UIViewController) { if MFMailComposeViewController.canSendMail() { viewController.present(mailComposer, animated: true, completion: nil) } } func mailComposeController( _ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error? ) { controller.dismiss(animated: true, completion: nil) } } 的阅读:

https://docs.microsoft.com/en-us/sql/t-sql/functions/lead-transact-sql?view=sql-server-2017 https://docs.microsoft.com/en-us/sql/t-sql/functions/lag-transact-sql?view=sql-server-2017