sql - 如何连接两个表并针对客户品牌获取完整数据?
问题描述
我有两张表,在一张表中我有完整的产品详细信息(品牌和产品名称),在另一张表中我有截至日期月份的销售详细信息。
在第三列中,我想根据客户品牌明智地了解所有产品的详细信息。
例如:
我正在销售 4 种不同型号的三星手机,有客户只从我这里购买了两种型号。所以我也有可能向他出售同一品牌的其他产品。
我附上了我有表 1 和表 2 的图像,我需要表 3 中的数据
我试过完全外连接
with cte as
(select Sales_Manager_ProductAnalysisTable.Customer, ProductMasterfromSales.Brand,ProductMasterfromSales.product
from ProductMasterfromSales
cross join Sales_Manager_ProductAnalysisTable
group by Sales_Manager_ProductAnalysisTable.Customer, ProductMasterfromSales.Brand,ProductMasterfromSales.product
)
select cte.Customer,cte.Brand, cte.product,
case when Sales_Manager_ProductAnalysisTable.product is null or Sales_Manager_ProductAnalysisTable.Brand is null then 0 else Sales_Manager_ProductAnalysisTable.jan end as jan,
case when Sales_Manager_ProductAnalysisTable.product is null or Sales_Manager_ProductAnalysisTable.Brand is null then 0 else Sales_Manager_ProductAnalysisTable.feb end as feb,
case when Sales_Manager_ProductAnalysisTable.product is null or Sales_Manager_ProductAnalysisTable.Brand is null then 0 else Sales_Manager_ProductAnalysisTable.feb end as Mar,
case when Sales_Manager_ProductAnalysisTable.product is null or Sales_Manager_ProductAnalysisTable.Brand is null then 0 else Sales_Manager_ProductAnalysisTable.feb end as Apr,
case when Sales_Manager_ProductAnalysisTable.product is null or Sales_Manager_ProductAnalysisTable.Brand is null then 0 else Sales_Manager_ProductAnalysisTable.feb end as May,
case when Sales_Manager_ProductAnalysisTable.product is null or Sales_Manager_ProductAnalysisTable.Brand is null then 0 else Sales_Manager_ProductAnalysisTable.feb end as Jun,
case when Sales_Manager_ProductAnalysisTable.product is null or Sales_Manager_ProductAnalysisTable.Brand is null then 0 else Sales_Manager_ProductAnalysisTable.feb end as Jul,
case when Sales_Manager_ProductAnalysisTable.product is null or Sales_Manager_ProductAnalysisTable.Brand is null then 0 else Sales_Manager_ProductAnalysisTable.feb end as Aug,
case when Sales_Manager_ProductAnalysisTable.product is null or Sales_Manager_ProductAnalysisTable.Brand is null then 0 else Sales_Manager_ProductAnalysisTable.feb end as Sep,
case when Sales_Manager_ProductAnalysisTable.product is null or Sales_Manager_ProductAnalysisTable.Brand is null then 0 else Sales_Manager_ProductAnalysisTable.feb end as Oct,
case when Sales_Manager_ProductAnalysisTable.product is null or Sales_Manager_ProductAnalysisTable.Brand is null then 0 else Sales_Manager_ProductAnalysisTable.feb end as Nov,
case when Sales_Manager_ProductAnalysisTable.product is null or Sales_Manager_ProductAnalysisTable.Brand is null then 0 else Sales_Manager_ProductAnalysisTable.feb end as Dev
from cte
left join Sales_Manager_ProductAnalysisTable on Sales_Manager_ProductAnalysisTable.Customer = cte.Customer and Sales_Manager_ProductAnalysisTable.Brand = cte.Brand and Sales_Manager_ProductAnalysisTable.product = cte.product;
+---------+---------------------+----------+------+------+------+------+------+------+------+------+------+------+------+------+
| Brand | Customer | Product | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec |
+---------+---------------------+----------+------+------+------+------+------+------+------+------+------+------+------+------+
| Samsung | viaCom Technologies | Note 10 | NULL | 1980 | NULL | NULL | NULL | 2640 | NULL | NULL | NULL | NULL | NULL | NULL |
+---------+---------------------+----------+------+------+------+------+------+------+------+------+------+------+------+------+
| Samsung | viaCom Technologies | Note 9 | NULL | 0 | 360 | NULL | NULL | NULL | -92 | -8 | NULL | NULL | NULL | NULL |
+---------+---------------------+----------+------+------+------+------+------+------+------+------+------+------+------+------+
| Samsung | viaCom Technologies | Iphone 8 | NULL | NULL | NULL | NULL | NULL | 100 | NULL | NULL | NULL | NULL | NULL | NULL |
+---------+---------------------+----------+------+------+------+------+------+------+------+------+------+------+------+------+
解决方案
使用 . 生成所有客户/产品组合cross join
。然后引入存在的数据。
实际上,您并不想要所有组合,因为您只是在寻找客户拥有的品牌。join
继续做你brand
想要的笛卡尔积。
尝试:
select cb.customer, cb.brand, pms.product,
coalesce(smpa.jan, 0) as jan,
coalesce(smpa.feb, 0) as feb,
. . .
from (select distinct smpa.customer, smpa.brand
from Sales_Manager_ProductAnalysisTable smpa
) cb join
ProductMasterfromSales pms
on pms.brand = smpa.brand left join
Sales_Manager_ProductAnalysisTable smpa
on smpa.customer = cb.customer and
smpa.brand = cb.brand and
smpa.product = pms.product
推荐阅读
- python - 使用 urlparse 解析不同的页面
- sql-server - 有没有办法以我写的方式使用不同的值列表循环查询?
- c++ - C++初学者。了解文件输入和输出,我可以写入文件但无法从文件打印
- c - 将元素插入链表时出现分段错误(核心转储)
- ros - ROS topic velodyne_msgs/VelodynePacket Message 中包含的数据是什么?
- swift - 如何在 SwiftUI 的 GeometryReader 中定义变量
- c# - 如何转换 ASP.NET Core 代码以访问 SQL Server 以使用本地 SQLite DB?
- java - 方法不会改变全局变量值
- node.js - 如何将值从另一个客户端 javascript 文档传递到 nodeJS?
- android - Android NDK - armeabi vs armeabi-v7a 文件夹