SQL对不同列中按相同值分组的计数进行除法运算

  • 本文关键字:运算 除法 SQL mysql sql
  • 更新时间 :
  • 英文 :


所以我有一个表,其中有两行包含名称和其他内容,这两列可以包含相同的名称:

RegionID约翰·康尼
买方 卖方
John
Lina Kajsa 2
Kajsa康尼3
JohnErik
Kajsa康尼约翰·康尼
John约翰·康尼
KajsaDavid
David

SELECT clinetname,
Sum(buyercount) / Sum(sellercount)
FROM   (SELECT buyer    AS ClinetName,
Count(1) AS BuyerCount,
0        AS SellerCount
FROM   Store
WHERE  regionid = 1
GROUP  BY buyer
UNION ALL
SELECT seller   AS ClinetName,
0        AS BuyerCount,
Count(1) AS SellerCount
FROM   Store
WHERE  regionid = 1
GROUP  BY seller) a
GROUP  BY clinetname
HAVING Sum(sellercount) > 0; 

最新更新