SQL(Oracle) - 使用'having'选择包含max()的行

时间:2015-09-24 20:11:56

标签: sql oracle max

我有一张大学数据库的表格:

takes(ID, course_id, sec_id, semester, year, grade)
section(course_id, sec_id, semester, year)

我需要确定哪些部分在2009年秋季学期注册的学生人数最多。

我尝试过以下查询的变体:

with sec_enrollment as(
    select course_id, sec_id, count(ID) as enrollment
    from section natural join takes
    where semester = 'Fall' and year = 2009
    group by course_id, sec_id)
select course_id, sec_id, enrollment
from sec_enrollment
group by course_id, sec_id, enrollment
having enrollment = max(enrollment)

以上查询返回2009年秋季的所有部分,而不仅仅是具有最高注册数的部分。似乎我的“拥有”条款被忽略了。

如果我使用查询:

with sec_enrollment as(
    select course_id, sec_id, count(ID) as enrollment
    from section natural join takes
    where semester = 'Fall' and year = 2009
    group by course_id, sec_id)
select max(enrollment)
from sec_enrollment

我可以获得最大注册量的期望值。我只是无法弄清楚如何获得所需的值以及包含该最大值的部分的course_id和sec_id。我认为'group by'子句搞砸了,但是我不能在没有绘制错误的情况下以任何其他方式配置它(ORA-00979:不是GROUP BY表达式)。任何帮助将不胜感激

3 个答案:

答案 0 :(得分:0)

这是另一种使用with sec_enrollment as ( select course_id, sec_id, count(ID) as enrollment from section natural join takes where semester = 'Fall' and year = 2009 group by course_id, sec_id) , max_enrollment as ( select max(enrollment) as maxenrollment from sec_enrollment) select course_id, sec_id from sec_enrollment s join max_enrollment m on s.enrollment = m.maxenrollment 的方法。

    <style type="text/css">
#map { min-height: 320px; height: 100%; margin: -15px;}
body {
padding: 0;
margin: 0;
}
html, body, {
height: 100%;
}
    </style>
</head>
<body>

         <div data-role="page" id="glossary">
            <div data-role="header">
                 <h3>Example</h3>
            </div>
            <div data-role="content">
                <ul data-role='listview' data-inset='true' id='resultsList'>
                    <!-- keep empty for dynamically added items -->
                </ul>
            </div>

        </div>
        <!-- display -->
        <div data-role="page" id="display">
            <div data-role="header">
                 <h3>Name Goes Here</h3>
 <a data-role="button" data-rel="back" data-icon="back">Back</a>
            </div>
            <div data-role="content">
  <div id="definition"></div>
        <div id="weathermap"></div>
            </div>
        </div>

<script>
var json ={"rows":[{"id":"129","id_user":"1","id_dispositivo":"1","Latitude":"10.1497326","Longitude":"-67.9283981"},{"id":"135","id_user":"1","id_dispositivo":"2","Latitude":"8.6119127","Longitude":"-70.2084732"}]};
            var currentItem = 0;
            $('#glossary').on('pageinit', function() {

                $.each(json.rows, function(i, term) {
$('#resultsList').append('<li><a href="#display"' + term.id_dispositivo + '"><img src="../menu0/images/dispositivos/small/'+term.id_dispositivo+'.jpg" height=100px width=100px/></a></li>')
                });
                $('#resultsList').listview('refresh');

                $('#resultsList li').click(function() {
                    currentItem = $(this).index();
                });
            });

            $('#display').on('pagebeforeshow', function() {
                $(this).find('[data-role=header] .ui-title').text(json.rows[currentItem].id_dispositivo);
var lat = json.rows[currentItem].Latitude;
var lon = json.rows[currentItem].Longitude;   
initMap(lat,lon);

            });



function initMap(lat,lon){
 document.getElementById('weathermap').innerHTML = "<div id='map' style='min-height: 320px; height: 100%; margin: -15px;'></div>";
var map = L.map('map').setView([lat, lon], 13);

L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
            attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
            }).addTo(map);

L.marker([lat,lon]).addTo(map)
.bindPopup('A pretty CSS3 popup. <br> Easily customizable.')
.openPopup();
   }
</script>

答案 1 :(得分:0)

按注册计数按降序排列结果,然后将结果限制为1条记录。

with sec_enrollment as(
    select course_id, sec_id, count(ID) as enrollment
    from section natural join takes
    where semester = 'Fall' and year = 2009
    group by course_id, sec_id)
select course_id, sec_id, enrollment
    from sec_enrollment
    where ROWNUM = 1
    order by enrollment DESC  

答案 2 :(得分:0)

您可以将查询编写为:

 select course_id, sec_id, enrollment
 from (
      select s.course_id, s.sec_id, count(t.ID) as enrollment
      from section s join takes t
      where s.semester = 'Fall' and s.year = 2009
      group by s.course_id, s.sec_id
      order by count(t.ID)
 ) 
 where rownuw <= 1

当您必须找到具有最大值的项目时,您不需要找到最大值,然后找到具有该值的项目。当您通过排序操作在同一操作中找到最大值和项目时,有两种操作。