繁体   English   中英

SQL 平均行数

[英]SQL average number of rows

当 1 个订单中的订购产品总数大于 2 时,我需要显示姓氏,并且我需要计算订购产品的平均值。 output 必须是:


 | LASTNAME | AVG  |
--------------------
 | SMITH    | 2.5  |
 | HARRIS| 2.75 |
------------------

我正在尝试这个

   SELECT c.customer_id,firstname,lastname,count(o.order_id)*1.0/(select distinct count(*)FROM ORDERS where order_id=o.order_id)
   FROM customer c,PURCHASES p,ORDERS o
   WHERE c.customer_id=p.customer_id and p.order_id=o.order_id
        and  o.order_id in (SELECT order_id
        FROM ORDERS
        GROUP BY order_id
        HAVING COUNT(*) > 2)
  group by c.customer_id

这似乎是正确的,但我得到的答案不正确。

这是 CUSTOMER 和 ORDERS 之间的关系表


| LASTNAME | ORDER_ID | PRODUCT_ID  |
------------------------------------
| SMITH    | 0        | 91          |
| SMITH    | 0        | 122         |
| WILLIAMS | 1        | 179         |
| SMITH    | 2        | 50          |
| SMITH    | 2        | 43          |
| SMITH    | 2        | 200         |
| HARRIS   | 3        | 105         |
| HARRIS   | 3        | 173         |
| HARRIS   | 4        | 29          |
| HARRIS   | 4        | 158         |
| JACKSON  | 5        | 75          |
| WILLIAMS | 6        | 55          |
| HARRIS   | 7        | 86          |
| HARRIS   | 7        | 143         |
| HARRIS   | 7        | 152         |
| HARRIS   | 7        | 197         |
| HARRIS   | 7        | 198         |
| BROWN    | 8        | 149         |
| HARRIS   | 9        | 117         |
| HARRIS   | 9        | 177         |
| WILLIAMS | 10       | 116         |
| JACKSON  | 11       | 13          |
| JACKSON  | 11       | 188         |
| WILLIAMS | 12       | 17          |
| WILLIAMS | 12       | 89          |
------------------------------------

DBFIDDLE

SELECT LASTNAME, Products/Orders
FROM (
   SELECT 
      LASTNAME,
      COUNT(DISTINCT ORDER_ID) Orders,
      COUNT(PRODUCT_ID) Products
   FROM Table1
   GROUP BY LASTNAME
   )x
WHERE LASTNAME IN (
   SELECT LASTNAME
   FROM (
      SELECT 
        LASTNAME,
        COUNT(PRODUCT_ID) OVER (PARTITION BY LASTNAME,ORDER_ID) c
      FROM Table1
    )x
   WHERE c>=3
)

结果:

平均
哈里斯 2.7500
史密斯 2.5000

这适用于 MySQL >= 8

# gets all usernames which have have at least one order with more than two items
with users(name) as (
  select distinct name
  from orders
  group by name, orderid
  having count(productid) > 2
),
# gets the size of each order for the names
ordersize(name, productcount) as (
  select u.name, count(productid)
  from users u inner join orders o on u.name = o.name
  group by u.name, orderid
)
# get the average of productcount
select name, avg(productcount)
from ordersize
group by name

另请参阅此小提琴

MySQL > 8.0 的示例。 DDL 在链接的 Fiddle 中 - 我已经使用 Customer_ID 而不是 Lastname,因为后者在现实生活中不会真正起作用。

-- First CTE - Get the customers with two or more orders
WITH cte1 AS
(
   SELECT
     Customer_ID,
     COUNT(DISTINCT Order_ID) AS OrderCount
   FROM
     CustomerOrder
  GROUP BY 
      Customer_ID
  HAVING 
     COUNT(DISTINCT Order_ID) >= 2
), cte2 AS
-- Get product counts per order
(
  SELECT 
      co.Customer_ID, 
      co.Order_ID,
      COUNT(DISTINCT Product_ID) AS Products
  FROM 
      CustomerOrder co
  INNER JOIN 
      cte1
  ON  cte1.Customer_ID = co.Customer_ID
  GROUP BY
    co.Customer_ID,
    co.Order_ID
)
-- Finally, average products per order
SELECT Customer_ID, AVG(Products) AS AverageProductsInOrder
FROM cte2
GROUP BY 
    Customer_ID;

https://www.db-fiddle.com/f/dKkSN5H9DioyouAppAErn1/0

declare @tmp as table(LASTNAME varchar(20),ORDER_ID int, PRODUCT_ID int)

insert into @tmp values('SMITH',    0        , 91          )
,('SMITH',    0        , 122         )
,('WILLIAMS', 1        , 179         )
,('SMITH',    2        , 50          )
,('SMITH',    2        , 43          )
,('SMITH',    2        , 200         )
,('HARRIS',   3        , 105         )
,('HARRIS',   3        , 173         )
,('HARRIS',   4        , 29          )
,('HARRIS',   4        , 158         )
,('JACKSON',  5        , 75          )
,('WILLIAMS', 6        , 55          )
,('HARRIS',   7        , 86          )
,('HARRIS',   7        , 143         )
,('HARRIS',   7        , 152         )
,('HARRIS',   7        , 197         )
,('HARRIS',   7        , 198         )
,('BROWN',    8        , 149         )
,('HARRIS',   9        , 117         )
,('HARRIS',   9        , 177         )
,('WILLIAMS', 10       , 116         )
,('JACKSON',  11       , 13          )
,('JACKSON',  11       , 188         )
,('WILLIAMS', 12       , 17          )
,('WILLIAMS', 12       , 89          )

select LASTNAME, 
sum(numerator),
count(*),
sum(numerator)/cast(count(*) as decimal(18,3)) avg
FROM
(
select 
LASTNAME,ORDER_ID, COUNT(*) numerator
from @tmp tmp

where tmp.LASTNAME
IN
(
select LASTNAME from @tmp tmp2
group by LASTNAME
having count(*)>2
)
GROUP BY LASTNAME,ORDER_ID
)x
GROUP BY LASTNAME
ORDER BY LASTNAME

output:

LASTNAME    (No column name)    (No column name)    avg
HARRIS  11  4   2.7500000000000000000
JACKSON 3   2   1.5000000000000000000
SMITH   5   2   2.5000000000000000000
WILLIAMS    5   4   1.2500000000000000000

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM