我正在尝试建立一个“经销商的产品渗透率表”
我有三个表: Resellers、Products1和Products2。
表1:
ResellerID
001
002
003
表2--------------------------------------------Table 3
Customer# - Prod# - ResellerID | Customer# - Prod# - ResellerID
ABC - 1-2 - 001 | GHI - 9-8 - 001
DEF - 3-4 - 002 | JKL - 9-8 - 002
ABC - 3-4 - 001 | MNO - 7-6 - 003
我需要端表结果看起来像这样:
ResellerID(PK) - (Unique)CountofCustomer - (Unique)CountofProduct
001 - 2 - 3
002 - 2 - 2
003 - 1 - 1
我构建了基本表作为1个客户表的引用,我构建了最终的表,但我不知道如何获取Reseller的唯一计数并将它们插入到适当的行中(之后将应用数学来给我穿透)。
在这方面的任何帮助都将不胜感激。
谢谢
发布于 2018-10-12 03:53:16
我建议使用union all
和group by
select ResellerID, count(distinct Customer), count(distinct prod)
from ((select Customer, Prod, ResellerID from table2
) union all
(select Customer, Prod, ResellerID from table3
)
) t
group by ResellerID;
https://stackoverflow.com/questions/52767901
复制相似问题