两个不同表中的列的双重求和

时间:2016-07-21 18:01:51

标签: sql sqlite

我有两张桌子。

<style>html { font-size: 14px; font-family: Arial, Helvetica, sans-serif; } .k-input { font-weight:bold !important; font-size:12pt !important; color: green !important; } ::-webkit-input-placeholder { color: green; } ::-webkit-input-placeholder { color: green; font-weight: 800; } :-moz-placeholder { /* Firefox 18- */ color: green; font-weight: 800; } ::-moz-placeholder { /* Firefox 19+ */ color: green; font-weight: 800; } :-ms-input-placeholder { color: green; font-weight: 800; } .container:hover{ background-color:#F0F2F3; } </style> <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.2.607/styles/kendo.common-material.min.css" /> <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.2.607/styles/kendo.material.min.css" /> <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.2.607/styles/kendo.default.mobile.min.css" /> <script src="//kendo.cdn.telerik.com/2016.2.607/js/jquery.min.js"></script> <script src="//kendo.cdn.telerik.com/2016.2.607/js/kendo.all.min.js"></script> <div class="container" style="overflow: hidden; border: 1px solid gray; width: 465px; margin-left: -180px; padding: 30px 70px 30px 170px;"> <div id="example" role="application" style="float:left;width:49%; margin-right:2%;margin-left: -32%;"> <div id="select" class="demo-section k-content"> <select id="size" placeholder="Skills, Designations...." style="width: 230px;border-width: 2px !important; border-color: #D8D3D3 !important;" > <option /> <option />to <option />code <option />way <option />always <option />easily </select> </div> </div> <div id="example1" role="application" style="float:left;width:49%;margin-right:0;margin-left: -5px;"> <div id="select" class="demo-section k-content"> <select id="size1" placeholder="Location" style="width: 140px;border-width: 2px !important; border-color: #D8D3D3 !important;" > <option /> <option />Delhi <option />Bangalore <option />Jammu and kashmir <option />ahmedabad <option />Arunachal Pradesh </select> </div> </div> <div id="example2" role="application" style=" float: right; margin-left: 95px; width: 49%; margin-right: 14px; margin-top: -47px;"> <div id="select" class="demo-section k-content"> <select id="size2" placeholder="Exp(Years)" style="width: 114px;border-width: 2px !important; border-color: #D8D3D3 !important; " > <option /> <option />0 Year <option />1 <option />2 <option />3 <option />4 </select> </div> </div> <div id="example3" role="application" style="float:right;width:49%;margin-right:-102px;margin-top: -47px;"> <div id="select" class="demo-section k-content"> <select id="size3" placeholder="Salary" style="width: 100px;border-width: 2px !important; border-color: #D8D3D3 !important; "> <option /> <option />&lt;1 Lac <option />2 <option />3 <option />4 </select> </div> </div> </div> <script> $(document).ready(function() { // create ComboBox from input HTML element // create ComboBox from select HTML element $("#size").kendoComboBox(); $("#size1").kendoComboBox(); $("#size2").kendoComboBox(); $("#size3").kendoComboBox(); var select = $("#size").data("kendoComboBox"); var select = $("#size1").data("kendoComboBox"); var select = $("#size2").data("kendoComboBox"); var select = $("#size3").data("kendoComboBox"); }); </script>

'Order details' OrderID | Quantity | UnitPrice | ProductID 1002 | 19 | 17 | 824 1002 | 5 | 15 | 285 1003 | 7 | 6 | 543 1004 | 12 | 11 | 205

我必须得到购买了最多产品的客户的ID。正如您所看到的,一个客户不仅可以拥有一个OrderID

2 个答案:

答案 0 :(得分:0)

我认为这样的事情应该可行:

SELECT TOP 1 customerid
FROM (
SELECT customerid, SUM(od.quantity) AS sum
FROM orders AS O
INNER JOIN order_details AS OD
ON O.orderid = OD.orderid
GROUP BY customerid
ORDER BY sum DESC)

检查表格名称

答案 1 :(得分:0)

您可以先根据SUM Quantity的降序汇总第一条记录的限制:

SELECT
    ord.CustomerID,
    SUM(det.Quantity) AS Quantity
FROM
    Orders ord
JOIN
    Order_Details det
    ON  (det.OrderID = ord.OrderID)
ORDER BY
    Quantity DESC
LIMIT 1;