所以我有一个表,其中有两行包含名称和其他内容,这两列可以包含相同的名称:
买方 | 卖方 | RegionID||
---|---|---|---|
John | |||
Lina | Kajsa | 2 | |
Kajsa | 康尼 | 3 | |
John | Erik | ||
Kajsa | 康尼约翰·康尼 | ||
John约翰·康尼 | |||
Kajsa | David | ||
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;