SQL Over Overition by Over Partition By

时间:2015-06-09 19:10:40

标签: sql sybase-ase

我有以下代码,当我引用一个'leadno'时,它可以正常工作,因为它返回单个产品的最高总和

select t.*
from (Select t2.leadno, t1.quoteno, t1.cn_ref, sum(t1.qty/100) as 't_qty',
             ROW_NUMBER() Over (Partition By t1.cn_ref order by sum(qty/100) desc) as RN 
      From dba.quotelne t1 INNER JOIN
           dba.quotehdr t2
           ON t1.quoteno = t2.quoteno
      Where leadno = 31665 and t1.statusflag = 'A' and t2.statusflag = 'A' 
      Group By t2.leadno, t1.quoteno, t1.cn_ref
     ) t
where rn = 1

但是当我添加更多那个leadno时它返回并不返回所有quoteno它只返回最高sum(qty/100)的值,那么有没有办法运行一个ROW_NUMBER() Over (Partition By ROW_NUMBER() Over (Partition By)

中的sum(qty/100)

我希望这是有道理的,但如果不是,我会尝试进一步解释,每个“领导”可能会有一个或多个“引号”。每个'报价'都会列出许多产品。这些可能是重复的(因此我使用leadno的原因我需要显示的是cn_ref所有RN = 1,唯一leadno和最高数量L.mapbox.accessToken = 'pk.wwwwwwwwwwwwxxxxxxxxxxxxxxxxxxssssssss'; // In the propeties object for each marker define key's // like `rentals`, `fuel`, `tackleshop` that are set to true for false // depending on whether or they exist at a location. var geoJson = [ { "type": "Feature", "geometry": { "coordinates": [ -84.519253,39.116177 ], "type": "Point" }, "properties": { "title": "Findlay Market (North Lot)", "cashaccepted": true, "creditcardsaccepted": true, "indoorgarage": false, "monthlyrates": true, "open24": true, "outdoorlots": true, "icon": { "iconUrl": "http://www.wakeupcoffee.biz/images/icon_car03b.png", "iconSize": [35, 35], // size of the icon "iconAnchor": [0, 0], // point of the icon which will correspond to marker's location "popupAnchor": [17,-5], // point from which the popup should open relative to the iconAnchor "className": "dot" } } }, { "type": "Feature", "geometry": { "coordinates": [ -84.518411,39.114629 ], "type": "Point" }, "properties": { "title": "Findlay Market (East Lot)", "cashaccepted": true, "creditcardsaccepted": true, "indoorgarage": false, "monthlyrates": true, "open24": true, "outdoorlots": true, "icon": { "iconUrl": "http://www.wakeupcoffee.biz/images/icon_car03b.png", "iconSize": [35, 35], // size of the icon "iconAnchor": [0, 0], // point of the icon which will correspond to marker's location "popupAnchor": [17,-5], // point from which the popup should open relative to the iconAnchor "className": "dot" } } }, { "type": "Feature", "geometry": { "coordinates": [ -84.520621,39.114837 ], "type": "Point" }, "properties": { "title": "Findlay Market (West Lot)", "cashaccepted": true, "creditcardsaccepted": true, "indoorgarage": false, "monthlyrates": true, "open24": true, "outdoorlots": true, "icon": { "iconUrl": "http://www.wakeupcoffee.biz/images/icon_car03b.png", "iconSize": [35, 35], // size of the icon "iconAnchor": [0, 0], // point of the icon which will correspond to marker's location "popupAnchor": [17,-5], // point from which the popup should open relative to the iconAnchor "className": "dot" } } }, { "type": "Feature", "geometry": { "coordinates": [ -84.510765,39.110845 ], "type": "Point" }, "properties": { "title": "ABM Parking", "cashaccepted": true, "creditcardsaccepted": true, "indoorgarage": false, "monthlyrates": false, "open24": true, "outdoorlots": true, "icon": { "iconUrl": "http://www.wakeupcoffee.biz/images/icon_car03b.png", "iconSize": [35, 35], // size of the icon "iconAnchor": [0, 0], // point of the icon which will correspond to marker's location "popupAnchor": [17,-5], // point from which the popup should open relative to the iconAnchor "className": "dot" } } }, { "type": "Feature", "geometry": { "coordinates": [ -84.518343,39.109173 ], "type": "Point" }, "properties": { "title": "Washington Park (Elm Entrance)", "cashaccepted": true, "creditcardsaccepted": true, "indoorgarage": true, "monthlyrates": false, "open24": true, "outdoorlots": false, "icon": { "iconUrl": "http://www.wakeupcoffee.biz/images/icon_car03b.png", "iconSize": [35, 35], // size of the icon "iconAnchor": [0, 0], // point of the icon which will correspond to marker's location "popupAnchor": [17,-5], // point from which the popup should open relative to the iconAnchor "className": "dot" } } }, { "type": "Feature", "geometry": { "coordinates": [ -84.516762,39.109543 ], "type": "Point" }, "properties": { "title": "Washington Park (Race Entrance)", "cashaccepted": true, "creditcardsaccepted": true, "indoorgarage": true, "monthlyrates": false, "open24": true, "outdoorlots": false, "icon": { "iconUrl": "http://www.wakeupcoffee.biz/images/icon_car03b.png", "iconSize": [35, 35], // size of the icon "iconAnchor": [0, 0], // point of the icon which will correspond to marker's location "popupAnchor": [17,-5], // point from which the popup should open relative to the iconAnchor "className": "dot" } } }, { "type": "Feature", "geometry": { "coordinates": [ -84.510899,39.109465 ], "type": "Point" }, "properties": { "title": "OTR Parking / Neon's", "cashaccepted": false, "creditcardsaccepted": true, "indoorgarage": false, "monthlyrates": false, "open24": false, "outdoorlots": true, "icon": { "iconUrl": "http://www.wakeupcoffee.biz/images/icon_car03b.png", "iconSize": [35, 35], // size of the icon "iconAnchor": [0, 0], // point of the icon which will correspond to marker's location "popupAnchor": [17,-5], // point from which the popup should open relative to the iconAnchor "className": "dot" } } }, { "type": "Feature", "geometry": { "coordinates": [ -84.511513,39.108121 ], "type": "Point" }, "properties": { "title": "Central Parking - Maint St.", "cashaccepted": true, "creditcardsaccepted": true, "indoorgarage": false, "monthlyrates": false, "open24": true, "outdoorlots": true, "icon": { "iconUrl": "http://www.wakeupcoffee.biz/images/icon_car03b.png", "iconSize": [35, 35], // size of the icon "iconAnchor": [0, 0], // point of the icon which will correspond to marker's location "popupAnchor": [17,-5], // point from which the popup should open relative to the iconAnchor "className": "dot" } } }, { "type": "Feature", "geometry": { "coordinates": [ -84.518547,39.108793 ], "type": "Point" }, "properties": { "title": "Central Parking - Memorial Hall", "cashaccepted": true, "creditcardsaccepted": true, "indoorgarage": false, "monthlyrates": false, "open24": true, "outdoorlots": true, "icon": { "iconUrl": "http://www.wakeupcoffee.biz/images/icon_car03b.png", "iconSize": [35, 35], // size of the icon "iconAnchor": [0, 0], // point of the icon which will correspond to marker's location "popupAnchor": [17,-5], // point from which the popup should open relative to the iconAnchor "className": "dot" } } }, { "type": "Feature", "geometry": { "coordinates": [ -84.516638,39.107744 ], "type": "Point" }, "properties": { "title": "Over The Rhine Public Parking", "cashaccepted": true, "creditcardsaccepted": true, "indoorgarage": false, "monthlyrates": false, "open24": true, "outdoorlots": true, "icon": { "iconUrl": "http://www.wakeupcoffee.biz/images/icon_car03b.png", "iconSize": [35, 35], // size of the icon "iconAnchor": [0, 0], // point of the icon which will correspond to marker's location "popupAnchor": [17,-5], // point from which the popup should open relative to the iconAnchor "className": "dot" } } }, { "type": "Feature", "geometry": { "coordinates": [ -84.515022,39.110240 ], "type": "Point" }, "information": { "Information": "<p><b>Mercer Garage</b></p> <hr> <div style='float:left; margin-top:5px;'>1324 Vine St.<br>Cincinnati, OH 45202</div> <div style='float:right; width:100px; margin-top:5px; background-color:#3d54a5; color:white; text-align: center; padding:6px 0px 6px 0px; border-radius: 4px;'> Get Directions </div> <br><br><br><hr> <div style='margin-top:5px;'><b>Phone:</b>(513)381-5800</div> ", "Rates": "<div>$1/hour, up to an $8 maximum</div> <div>Monday – Friday after 4pm: $5</div> <div>All day Saturday – Sunday: $5<div><br><div>Monthly parking passes are available for $85/month</div>", "Images": "<iframe src='https://www.google.com/maps/embed?pb=!1m0!3m2!1sen!2sus!4v1433855519514!6m8!1m7!1sI_zlWDFRpn2L2r1DmPeRBw!2m2!1d39.110841!2d-84.510429!3f276.4279292294492!4f-11.08512964773044!5f0.4000000000000002' width='350' height='250' frameborder='0' style='border:0'></iframe>", }, "properties": { "title": "Mercer Garage", "cashaccepted": true, "creditcardsaccepted": true, "indoorgarage": true, "monthlyrates": false, "open24": true, "outdoorlots": false, "icon": { "iconUrl": "http://static.nationwide.com/static/icon-opt-link-car-orange.gif?r=47", "iconSize": [35, 35], // size of the icon "iconAnchor": [0, 0], // point of the icon which will correspond to marker's location "popupAnchor": [17,-5], // point from which the popup should open relative to the iconAnchor "className": "dot" } } } ]; var map = L.mapbox.map('map', 'motrpub.3bbda9ac', { zoomControl: false }) .setView([39.112, -84.514], 16); new L.Control.Zoom({ position: 'topright' }).addTo(map); var markers = L.mapbox.featureLayer() .setGeoJSON(geoJson) .addTo(map); // START CUSTOM ICON // markers.on('layeradd', function(e) { var marker = e.layer, feature = marker.feature; marker.setIcon(L.icon(feature.properties.icon)); }); // Add features to the map. markers.setGeoJSON(geoJson); // END CUSTOM ICON // var info = document.getElementById('info'); // Iterate through each feature layer item, // and create a custom HTML group for each. In this example // we create a tabs menu and put each property in a respective tab. markers.eachLayer(function(m) { // Shorten m.feature.properties to p for convenience. var p = m.feature.information; var tabs = document.createElement('div'); tabs.className = 'tabs-ui'; for (var key in p) { var tab = document.createElement('div'); tab.className = 'tab'; var input = document.createElement('input'); input.type = 'radio'; input.id = idify(key); input.name = 'tab-group'; // For your own needs, you might want this to be unique. if (key === 'Information') input.setAttribute('checked', true); tab.appendChild(input); tab.innerHTML += '<label for=' + idify(key) + '>' + key + '</label>' + '<div class="content">' + p[key] + '</div>'; tabs.appendChild(tab); } m.bindPopup(tabs, {minWidth: 400}); }); function idify(str) { return str.replace(/\s+/g, '-').toLowerCase(); }; $('.menu-ui a').on('click', function() { // For each filter link, get the 'data-filter' attribute value. var filter = $(this).data('filter'); $(this).addClass('active').siblings().removeClass('active'); markers.setFilter(function(f) { // If the data-filter attribute is set to "all", return // all (true). Otherwise, filter on markers that have // a value set to true based on the filter name. return (filter === 'all') ? true : f.properties[filter] === true; }); return false; }); 对{ {1}}

2 个答案:

答案 0 :(得分:1)

我不是百分百肯定,但我认为您需要将分数放在分区中以实现您的需求:

select t.*
    from (Select t2.leadno, t1.quoteno, t1.cn_ref, sum(t1.qty/100) as 't_qty',
         ROW_NUMBER() Over (Partition By t2.leadno, t1.cn_ref order by sum(qty/100) desc) as RN 
      From dba.quotelne t1 INNER JOIN
           dba.quotehdr t2
       ON t1.quoteno = t2.quoteno
      Where leadno = 31665 and t1.statusflag = 'A' and t2.statusflag = 'A' 
      Group By t2.leadno, t1.quoteno, t1.cn_ref
     ) t
where rn = 1

除此之外,你不应该按顺序排除数量,它不会改变顺序,因为你将每个值除以100,但引擎必须在创建之前将每个记录分开总结

答案 1 :(得分:0)

为什么需要使用ROW_NUMBER()OVER(PARTITION BY)?

如果我了解您的需求,您需要以下值:leadno,cn_ref,max(sum(qty / 100))

所以,试试这个查询:

Select t2.leadno, t1.cn_ref, max(sum(t1.qty/100)) as 't_qty',
  From dba.quotelne t1 INNER JOIN
       dba.quotehdr t2
       ON t1.quoteno = t2.quoteno
  Where leadno = 31665 and t1.statusflag = 'A' and t2.statusflag = 'A' 
  Group By t2.leadno