这个SQL查询应该是什么?

时间:2015-12-08 09:34:08

标签: sql oracle11g

假设:

  

Customers {CustomerID, CustomerName, Address, City, Tel, Fax}

     

Orders {OrderID, OrderDate, CustomerID, OrderValue}

     

"给出满足以下条件的客户列表:来自给定客户的 每个订单 的价值超过10,000美元。"

enter image description here

enter image description here

此查询对我来说看起来不正确:

select cc.CustomerID, cc.CustomerName, tt.OrderValue
from Customers cc,
(select CustomerID, OrderValue
from Orders
where OrderValue > 10000) tt
where cc.CustomerID = tt.CustomerID;

Or, is it?

答案应该是: Pixoboo

8 个答案:

答案 0 :(得分:2)

好吧,如果每个订单超过10000,那么最小的订单必须这样做。所以,也许这是更简单的方法:

SELECT *
FROM Customers  
WHERE CustomerID IN 
   (SELECT CustomerID 
   FROM Orders 
   GROUP BY CustomerID  
   HAVING min(OrderValue)>10000)

奖励:它避免了NOT INNULLs的毛病问题。

答案 1 :(得分:1)

试试这个:

  select cc.CustomerID, cc.CustomerName, tt.OrderValue as OrderValue
   from Customers cc, Orders tt
   where cc.CustomerID = tt.CustomerID and tt.OrderValue >10000  
   Group by cc.CustomerID, cc.CustomerName, tt.OrderValue

答案 2 :(得分:1)

当前的SQL查询将获取那些已经下订单少于10,000且超过10,000的记录。

鉴于您已经检索了总是下订单超过10,000的客户列表,我总是会使用否定条件来处理这个问题,因为这将过滤掉在任何给定时间点放置较低值的所有记录价值秩序。

查询是:

  Select DISTINCT  CC.CUSTOMERNAME
    from CUSTOMER CC
    WHERE CC.CUSTOMERID NOT IN 
(Select DISTINCT CUSTOMERID from ORDERS where ORDERVALUE<10000)

如果这回答了您的问题,请告诉我

答案 3 :(得分:1)

如果这个问题没有足够的答案,这里有一个没有使用子查询的答案: -

Select C.CustomerId,count(C.CustomerId),count(OrderValue)
from
Customers C
inner join
Orders o
on o.CustomerId=C.CustomerId
group by C.CustomerId
having min(OrderValue)>10000
and count(*)=count(OrderValue)

最后一行是可选的,如果您想要排除任何具有null OrderValue的客户,则可以包含该行。

这是一种只使用连接的方法: -

select distinct C.CustomerId
from
Customers C
inner join
Orders o
on o.CustomerId=C.CustomerId
and o.OrderValue>10000
left outer join 
(
  select distinct CustomerId
  from Orders
  where OrderValue<=10000
 ) t1 
 on C.CustomerId=t1.CustomerId
 where t1.CustomerId is null

答案 4 :(得分:0)

我会这样做

std::tuple<Matrix, Matrix> function(int p, int q) {
   // ...
   return std::make_tuple(a, b);
}

int main() {
   Matrix a, b;
   std::tie(a, b) = function(p, q);
   // ...
}

答案 5 :(得分:0)

您必须检查给定的<script> $("#BandeauPostRelatifsHover").mouseover(function(){ $("#BandeauPostRelatifsContenu").show("slow"); }); $("#fermetureBandeauPostRelatifs").click(function(){ $("#BandeauPostRelatifsContenu").hide(); $("#BandeauPostRelatifsHover").css("visibility","hidden"); }); </script>,它没有任何低于阈值的订单。

第一次尝试时,我会选择NOT EXISTS(或NOT IN)和subselect。之后你可以将它重新加工成花哨的连接。

CustomerID

答案 6 :(得分:0)

如果要查找超过10k的个别订单,请尝试使用left outer join

select cc.CustomerID, cc.CustomerName, ods.OrderValue
from Customers cc left outer join Orders ods
on cc.CustomerID = ods.CustomerID
where OrderValue > 10000

如果要查看超过10k的总和订单,请尝试使用left outer joinhaving条件:

select cc.CustomerID, cc.CustomerName, ods.OrderValue
from Customers cc left outer join Orders ods
on cc.CustomerID = ods.CustomerID
group by cc.CustomerID, cc.CustomerName, ods.OrderValue
having sum(OrderValue) > 10000

答案 7 :(得分:0)

我认为这是获得这些结果的更为标准的方式。我使用子查询来获得这些结果..

SELECT table_name, table_type, engine FROM information_schema.tables

有关详细信息,请参阅此网站.. click here