如何编写sql查询以选择一列中具有最大值的行

时间:2017-06-28 02:53:10

标签: sql oracle greatest-n-per-group

我的表看起来像这样。

Id   |  Name  |  Ref  | Date        | From
10   |  Ant   |  100  | 2017-02-02  | David
10   |  Ant   |  300  | 2016-01-01  | David
2    |  Cat   |  90   | 2017-09-09  | David
2    |  Cat   |  500  | 2016-02-03  | David
3    |  Bird  |  150  | 2017-06-28  | David

这是我想要的结果。

Id   |  Name  |  Ref  | Date       | From
3    |  Bird  |  150  | 2017-06-28 | David
2    |  Cat   |  500  | 2016-02-03 | David
10   |  Ant   |  300  | 2016-01-01 | David

我的目标是按订单日期desc。

排序的最高每个ID

请告诉我如何使用pl / sql编写SQL查询。

5 个答案:

答案 0 :(得分:1)

这种要求(您需要最多或最少一列,按另一列分组,但您需要来自最大或最小行的所有数据)几乎是分析函数是什么对于。我使用了row_number - 如果可以联系,您需要澄清分配(请参阅我的问题下的评论),并且根据细节,另一个分析函数可能更合适 - 也许rank()。< / p>

with
     my_table ( id, name, ref, dt, frm ) as (
       select 10, 'Ant' , 100, date '2017-02-02', 'David' from dual union all
       select 10, 'Ant' , 300, date '2016-01-01', 'David' from dual union all
       select  2, 'Cat' ,  90, date '2017-09-09', 'David' from dual union all
       select  2, 'Cat' , 500, date '2016-02-03', 'David' from dual union all
       select  3, 'Bird', 150, date '2017-06-28', 'David' from dual
     )
-- End of simulated table (for testing purposes only, not part of the solution).
-- SQL query begins BELOW THIS LINE.
select   id, name, ref, dt, frm
from     (
           select id, name, ref, dt, frm,
                  row_number() over (partition by id order by ref desc, dt desc) as rn
           from   my_table
         )
where    rn = 1
order by dt desc
;

ID  NAME  REF  DT          FRM 
--  ----  ---  ----------  -----
 3  Bird  150  2017-06-28  David
 2  Cat   500  2016-02-03  David
10  Ant   300  2016-01-01  David

答案 1 :(得分:0)

您可以使用此

SELECT
Id
,Name
,Ref
,[Date]
FROM(
SELECT 
* 
, ROW_NUMBER() OVER(PARTITION BY ID ORDER BY Ref DESC) AS Row#
FROM yourtable
) A WHERE Row# = 1
ORDER BY A.[Date] DESC

答案 2 :(得分:0)

另一种自我加入的解决方案(Idea来自这里:How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?):

with
    my_table ( id, name, ref, dt, frm ) as (
      select 10, 'Ant' , 100, date '2017-02-02', 'David' from dual union all
      select 10, 'Ant' , 300, date '2016-01-01', 'David' from dual union all
      select 10, 'Ant' , 300, date '2015-01-01', 'David' from dual union all
      select  2, 'Cat' ,  90, date '2017-09-09', 'David' from dual union all
      select  2, 'Cat' , 500, date '2016-02-03', 'David' from dual union all
      select  3, 'Bird', 150, date '2017-06-28', 'David' from dual
    )
-- End of simulated table (for testing purposes only, not part of the solution).
-- SQL query begins BELOW THIS LINE.

select m1.* 
from my_table m1 
left join my_table m2 
on m1.id = m2.id and (
 -- this is basically a comparator: order by ref desc, dt desc
 m1.ref < m2.ref or (
   m1.ref = m2.ref and 
   m1.dt < m2.dt
 )
) where m2.id is null order by m1.dt desc
;

        ID NAME        REF DT        FRM 
---------- ---- ---------- --------- -----
         3 Bird        150 28-JUN-17 David
         2 Cat         500 03-FEB-16 David
        10 Ant         300 01-JAN-16 David

答案 3 :(得分:0)

使用&#34;优于&#34; SQL主体:

<div class="row">
    <form action="AddDataServlet" method="post">

            <div class="row">
              <div class="col-md-6">
                <div class="form-group">
                  <label class="control-label">Phone:</label>

                <input type="text" class="form-control" id="phone" name="phone" pattern="^[0-9\-\+\s\(\)]*$" placeholder="(123)456-2345"  value="<%=session.getAttribute("Phone")%>">

                </div>
              </div>

             <div class="col-md-2">
             <label class="control-label">OPT-IN DATE:</label>
             <td><input type="datetime" id="OPT-IN phoneDate" name="OPT-IN phoneDate" class="form-control" value="<%=session.getAttribute("PhoneDate")%>"/></td>
             <br>

             </div>


             <div class="col-md-2">
             <br>
            <input type="Submit" class="form-control" value="Opt-In" style ='margin:6px'/>
              </div>
              <script>
            if $(document).ready(function(){
                $('.form-control').attr('disabled',true);

                $('#OPT-IN phoneDate').keyup(function(){
                    if($(this).val().length !=0){
                        $('.form-control').attr('disabled', false);
                    }
                    else
                    {
                        $('.form-control').attr('disabled', true);        
                    }
                   })                                      
            });
            else if $(document).ready(function(){
                $('.form-control').attr('disabled',false);

                $('#OPT-IN phoneDate').keyup(function(){
                    if($(this).val().length !=0){
                        $('.form-control').attr('disabled', true);
                    }
                    else
                    {
                        $('.form-control').attr('disabled', false);        
                    }
                   })                                      
            });
             </script>

             <div class="col-md-2">
             <br>
             <input type="button" class="sendButton form-control" value="Opt-Out" style ='margin:6px'></input>

             </div>

答案 4 :(得分:-1)

SELECT Id, Name, Max(Ref) as Ref, Min(`Date`) as `Date`
From Forge
Group By Id, Name
Order by Min(`Date`) desc;