如何使用GREATEST()& SQL中的LEAST()函数

时间:2018-04-03 01:16:28

标签: sql oracle

我有一个oracle代码,如:

select greatest(date1, date2, date3)
from myDate

有没有办法在SQL中复制这个逻辑?

欢呼声

3 个答案:

答案 0 :(得分:2)

复制NULL的确切逻辑很棘手,因为它处理select (case when date1 is null or date2 is null or date3 is null then null when date1 >= date2 and date1 >= date3 then date1 when date2 >= date3 then date2 else date3 end) from . . .; 值的方式。等效的逻辑是:

 services.AddAuthentication(auth =>
            {
                auth.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                auth.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;

            })
            .AddCookie()

            .AddOpenIdConnect(opts =>
            {
                Configuration.GetSection("Authentication").Bind(opts);

                opts.Events = new OpenIdConnectEvents
                {
                    OnAuthorizationCodeReceived = async ctx =>
                    {
                        HttpRequest request = ctx.HttpContext.Request;
                        //We need to also specify the redirect URL used
                        string currentUri = UriHelper.BuildAbsolute(request.Scheme, request.Host, request.PathBase, request.Path);
                        //Credentials for app itself
                        var credential = new ClientCredential(ctx.Options.ClientId, ctx.Options.ClientSecret);

                        //Construct token cache
                        ITokenCacheFactory cacheFactory = ctx.HttpContext.RequestServices.GetRequiredService<ITokenCacheFactory>();
                        TokenCache cache = cacheFactory.CreateForUser(ctx.Principal);

                        var authContext = new AuthenticationContext(ctx.Options.Authority, cache);

                        //Get token for Microsoft Graph API using the authorization code
                        string resource = "https://bupaau.onmicrosoft.com/4fa4b4a7-d34f-49af-8781-c8b39f0cf770";
                        AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(
                            ctx.ProtocolMessage.Code, new Uri(currentUri), credential, resource);

                        //Tell the OIDC middleware we got the tokens, it doesn't need to do anything
                        ctx.HandleCodeRedemption(result.AccessToken, result.IdToken);
                    }
                };
            });

答案 1 :(得分:1)

请注意,Azure SQL DB 现在支持 GREATEST/LEAST。未来的 SQL Server 版本也将如此。

GREATEST LEAST

答案 2 :(得分:0)

如果您想要在没有实际使用GREATEST功能的情况下替换GREATEST功能的方法,那么您可以改为使用CASE表达式:

SELECT
    CASE WHEN date1 > date2
         THEN
         (CASE WHEN date1 > date3 THEN date1 ELSE date3 END)
         ELSE
         (CASE WHEN date2 > date3 THEN date2 ELSE date3 END) END AS greatest
FROM dual