根据值拉出最后更新的记录

时间:2017-02-23 20:22:05

标签: sql

如果可以的话,请协助我相对简单的查询。

我想要一个SQL查询来记录每个ID的记录,Status = 16是最新的更新日期。

从此列表中:

ID      Date                  Status
000203E 1988-01-01 00:00:00.000 16
000203E 1970-01-01 00:00:00.000 15
000236S 1970-01-01 00:00:00.000 15
000236S 1982-12-15 00:00:00.000 16
000678W 1996-06-05 00:00:00.000 16
000678W 1970-01-01 00:00:00.000 15
000755U 1984-04-16 00:00:00.000 14
000755U 1970-01-01 00:00:00.000 16

期望的输出

000203E 1988-01-01 00:00:00.000 16
000678W 1996-06-05 00:00:00.000 16

提前致谢!

3 个答案:

答案 0 :(得分:2)

获取id的一种方法是使用聚合:

select id
from t
group by id
having max(date) = max(case when status = 16 then date end);

如果您想要完整的行,可以使用inexistsjoin。或者只是将结果归咎于:

select id, max(date) as date, 16 as status
from t
group by id
having max(date) = max(case when status = 16 then date end);

获取完整行的另一种方法是:

select t.*
from t
where t.status = 16 and
      t.date = (select max(t2.date) from t t2 where t2.id = t.id);

答案 1 :(得分:0)

SELECT      table.*
FROM        table
INNER JOIN
(
    SELECT id, max(date) max_date
    FROM table
    WHERE status_id = 16
    GROUP BY id
)           latest_records
ON 
    table.id = latest_records.id
    AND table.date = latest_records.max_date

答案 2 :(得分:0)

[assembly: ExportRenderer(typeof(FBLoginPage), typeof(FBLoginPageRenderer))]
namespace vidmoji.iOS
{
    public class FBLoginPageRenderer : PageRenderer
    {
        bool IsShown;


        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);

            if (!IsShown)
            {
                IsShown = true;

                var auth = new OAuth2Authenticator(
                    clientId: App.Instance.FBAuthSettings.ClientId,
                    clientSecret: App.Instance.FBAuthSettings.SecurityKey,
                    accessTokenUrl: new Uri(App.Instance.FBAuthSettings.AccessTokenUrl),
                    scope: App.Instance.FBAuthSettings.Scope,
                    authorizeUrl: new Uri(App.Instance.FBAuthSettings.AuthorizeUrl),
                    redirectUrl: new Uri(App.Instance.FBAuthSettings.RedirectUrl));



                auth.Completed += (sender, eventArgs) =>
                {
                    DismissViewController(true, null);
                    if (eventArgs.IsAuthenticated)
                    {
                        App.Instance.loginWithFacebook(eventArgs.Account.Properties["access_token"]);
                    }
                    else {
                        DismissModalViewController(true);
                    }
                };

                PresentViewController(auth.GetUI(), true, null);
            }
        }
    }
}