• <i id='NCYSz'><tr id='NCYSz'><dt id='NCYSz'><q id='NCYSz'><span id='NCYSz'><b id='NCYSz'><form id='NCYSz'><ins id='NCYSz'></ins><ul id='NCYSz'></ul><sub id='NCYSz'></sub></form><legend id='NCYSz'></legend><bdo id='NCYSz'><pre id='NCYSz'><center id='NCYSz'></center></pre></bdo></b><th id='NCYSz'></th></span></q></dt></tr></i><div id='NCYSz'><tfoot id='NCYSz'></tfoot><dl id='NCYSz'><fieldset id='NCYSz'></fieldset></dl></div>

    <small id='NCYSz'></small><noframes id='NCYSz'>

      <tfoot id='NCYSz'></tfoot>

          <bdo id='NCYSz'></bdo><ul id='NCYSz'></ul>

        <legend id='NCYSz'><style id='NCYSz'><dir id='NCYSz'><q id='NCYSz'></q></dir></style></legend>

        SQL Server 中是否有一个 Max 函数,它采用两个值,如 .NET 中的 Math.Max?

        时间:2023-07-16
          <tbody id='ZUY3Q'></tbody>
        • <bdo id='ZUY3Q'></bdo><ul id='ZUY3Q'></ul>
          <legend id='ZUY3Q'><style id='ZUY3Q'><dir id='ZUY3Q'><q id='ZUY3Q'></q></dir></style></legend>

          • <small id='ZUY3Q'></small><noframes id='ZUY3Q'>

              <tfoot id='ZUY3Q'></tfoot>
              1. <i id='ZUY3Q'><tr id='ZUY3Q'><dt id='ZUY3Q'><q id='ZUY3Q'><span id='ZUY3Q'><b id='ZUY3Q'><form id='ZUY3Q'><ins id='ZUY3Q'></ins><ul id='ZUY3Q'></ul><sub id='ZUY3Q'></sub></form><legend id='ZUY3Q'></legend><bdo id='ZUY3Q'><pre id='ZUY3Q'><center id='ZUY3Q'></center></pre></bdo></b><th id='ZUY3Q'></th></span></q></dt></tr></i><div id='ZUY3Q'><tfoot id='ZUY3Q'></tfoot><dl id='ZUY3Q'><fieldset id='ZUY3Q'></fieldset></dl></div>

                1. 本文介绍了SQL Server 中是否有一个 Max 函数,它采用两个值,如 .NET 中的 Math.Max?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想写一个这样的查询:

                  I want to write a query like this:

                  SELECT o.OrderId, MAX(o.NegotiatedPrice, o.SuggestedPrice)
                  FROM Order o
                  

                  但这不是 MAX 函数的工作原理,对吧?它是一个聚合函数,因此它需要一个参数,然后返回所有行的 MAX.

                  But this isn't how the MAX function works, right? It is an aggregate function so it expects a single parameter and then returns the MAX of all rows.

                  有谁知道如何按照我的方式来做?

                  Does anyone know how to do it my way?

                  推荐答案

                  如果你想有一个类似于你的例子的语法,你需要制作一个 User-Defined Function,但你能做到吗?就像其他人所说的那样,你想用 CASE 语句相当容易地内联.

                  You'd need to make a User-Defined Function if you wanted to have syntax similar to your example, but could you do what you want to do, inline, fairly easily with a CASE statement, as the others have said.

                  UDF 可能是这样的:

                  create function dbo.InlineMax(@val1 int, @val2 int)
                  returns int
                  as
                  begin
                    if @val1 > @val2
                      return @val1
                    return isnull(@val2,@val1)
                  end
                  

                  ...你会这样称呼它...

                  ... and you would call it like so ...

                  SELECT o.OrderId, dbo.InlineMax(o.NegotiatedPrice, o.SuggestedPrice) 
                  FROM Order o
                  

                  这篇关于SQL Server 中是否有一个 Max 函数,它采用两个值,如 .NET 中的 Math.Max?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 SQL Server Express 版中创建作业 下一篇:通过连接不同服务器上两个数据库中的两个表来查询数据

                  相关文章

                    <i id='nL4o3'><tr id='nL4o3'><dt id='nL4o3'><q id='nL4o3'><span id='nL4o3'><b id='nL4o3'><form id='nL4o3'><ins id='nL4o3'></ins><ul id='nL4o3'></ul><sub id='nL4o3'></sub></form><legend id='nL4o3'></legend><bdo id='nL4o3'><pre id='nL4o3'><center id='nL4o3'></center></pre></bdo></b><th id='nL4o3'></th></span></q></dt></tr></i><div id='nL4o3'><tfoot id='nL4o3'></tfoot><dl id='nL4o3'><fieldset id='nL4o3'></fieldset></dl></div>

                    <small id='nL4o3'></small><noframes id='nL4o3'>

                  1. <legend id='nL4o3'><style id='nL4o3'><dir id='nL4o3'><q id='nL4o3'></q></dir></style></legend>

                    1. <tfoot id='nL4o3'></tfoot>

                      • <bdo id='nL4o3'></bdo><ul id='nL4o3'></ul>