select proid 商品编号,sum(salemoney) 销售金额, count(distinct orderid) 客户数 from B group by proid; 这个查出来的客户数去除了重复项
客户数用错了,应该是 custid (同一客户可能有多张订单), 还要加上两表关联,补充一下select B.proid 商品编号,sum(B.salemoney) 销售金额, count(distinct A.custid) 客户数 from A INNER JOIN B ON A.orderid = B.orderid group by B.proid;
一.不去除重复客户数. select proid,count(custid) ,sum(salemoney) from tbl a,tbl b where a.orderid=b.orderid group by proid 二.去除重复客户数 select proid ,count(custid) ,sum(salemoney) from (select proid,custid,sum(salemoney) from tbl a,tbl b where a.orderid=b.orderid group by proid,custid) group by proid
这个查出来的客户数去除了重复项
from A
INNER JOIN B ON A.orderid = B.orderid
group by B.proid;
select proid,count(custid) ,sum(salemoney) from tbl a,tbl b where a.orderid=b.orderid
group by proid
二.去除重复客户数
select proid ,count(custid) ,sum(salemoney) from
(select proid,custid,sum(salemoney) from tbl a,tbl b where a.orderid=b.orderid
group by proid,custid) group by proid