AVG PER VALUE SQL

时间:2017-11-17 09:19:27

标签: sql sql-server sql-server-2008

我有三张这样的表:

表客户端:

idcliente
namecliente

表发票:

idinvoice
idclient 

表invoiceline:

id
idinvoice
quantity

如何获得购买数量超过每个客户销售产品的平均数量的客户名称

1 个答案:

答案 0 :(得分:0)

架构是

table client
(idclient -> namecliente )
table invoice
(idinvoice -> idclient(Foreign key client) ) 
table invoiceline
(id -> idinvoice(Foreign key invoice), quantity)

我的查询是:

SELECT  client.nameclient
FROM client,invoice, invoiceline
WHERE client.idclient = invoice.idclient
AND invoive.idinvoice = invoiceline.idinvoice
AND (quantity > (select  AVG(quantity) FROM invoiceline))