Group By / Having相关子查询

时间:2013-12-17 05:59:21

标签: sql group-by having

我正在尝试解决此问题,我真的失去了

  

订购数量中的至少一个订单大于   所有其他订单的平均数量。

我的表是:

Customer

Cust_ID | CustName | Region | Phone

Orders

Ordernum | Cust_ID | Item_ID | Quantity

Vendor

Vendor_ID | Item_ID | Costs | Region

stock

Item_ID | Description | Price | On_hand

数据库架构是:

Customer (Cust_ID, CustName, Region, Phone)

Orders (Ordernum, Cust_ID, Item_ID, Quantity) Foreign key Cust_ID

references Customer Not Null, On Delete Restrict

Foreign key Item_ID references Stock Not Null, On Delete Restrict

Stock (Item_ID, Description, Price, On_hand)

Vendor (Vendor_ID, Item_ID, Cost, Region)

Foreign key Item_ID references Stock Not null, On Delete Restrict

这是我到目前为止所尝试的内容。我做错了什么?

select custname, phone, count(distinct(item_id)), sum(quantity)
from customer c, orders o
Where c.cust_id= o.cust_id and count (o.ordernum) >= 2
group by cust_id
having sum (quantity) >
  (select avg(quantity)
  from orders o2
  where o.item_id != o2.item_id)
order by custname;

我重写了我的代码,这就是我提出的。我真的输了:

select c1.custname, phone, count(distinct(o.item_id)), sum(quantity) as quantity
from customer c1, orders o
Where c1.cust_id= o.cust_id
group by c1.cust_id, custname, phone, quantity
Having 2>= 
(select count(o1.item_id) 
From orders o1 
where c1.cust_ID = o1.cust_ID)
AND sum(quantity) > 
(select AVG(quantity) 
from orders o2
Where c1.cust_ID != o2.cust_ID AND o.Item_ID = o2.Item_ID)
order by custname;

3 个答案:

答案 0 :(得分:0)

尝试此查询

select a.cust_id, count(distinct Item_ID) as itemOrderd, 
sum(b.quantity) as sum 
from
customer a
inner join 
orders b
on a.cust_id=b.cust_id
group by a.cust_id
having count(distinct ordernum) > 1 
and max(b.quantity) > avg(b.quantity)

Fiddle

| CUST_ID | ITEMORDERD | SUM |
|---------|------------|-----|
|       1 |          2 |  61 |

答案 1 :(得分:0)

希望能够满足您的需求

SELECT x.custname, 
       x.phone, 
       Sum(x.itemcnt), 
       Sum(x.quantity) 
FROM   (SELECT c.custname, 
               c.phone, 
               1                                            AS itemCnt, 
               Sum(o.quantity), 
               Isnull((SELECT Avg(y.quantity) 
                       FROM   orders y 
                       WHERE  y.cust_id = o.cust_id), 0.00) AvgQty 
        FROM   customer c 
               LEFT OUTER JOIN orders o 
                            ON c.cust_id = o.cust_id 
        GROUP  BY c.custname, 
                  c.phone, 
                  o.item_id) x 
GROUP  BY x.custname, 
          x.phone 
HAVING Sum(x.itemcnt) > 1 
       AND Sum(x.quantity) > x.avgqty 

答案 2 :(得分:0)

我认为会:

SELECT MAX(c.CustName) as CustName,
       MAX(c.Phone) as Phone, 
       COUNT(DISTINCT o.Item_ID) as CountOfDistinctItems,
       SUM(o.Quantity) as SumOfQuantity
FROM Customer as c
JOIN Orders as o on c.Cust_ID=o.Cust_ID 
JOIN (SELECT Ordernum, SUM(Quantity) as Order_q_sum 
             FROM Orders 
             GROUP BY Ordernum) as oq 
    ON o.Ordernum=oq.Ordernum
GROUP BY c.Cust_ID
HAVING COUNT(DISTINCT o.Ordernum)>=2
       AND
       MAX(oq.Order_q_sum)>
       (
        SELECT AVG(SumQuantity) FROM
             (SELECT SUM(Quantity) SumQuantity FROM Orders GROUP BY OrderNum) as t1
       )