根据 B 中的多条记录从 table A 中选择记录
Selecting records from table A based in multiple records in B
我有以下销售结构。经理们在每个地区都被分配了一名销售人员,但他们也推销自己。下面的 table 包含经理和 salespersons/regions 之间的 link。
TABLE:MSR
Manager
Sales
Region
MA
SA
EAST
MA
SB
EAST
MA
SB
NORTH
MB
SA
WEST
MB
SB
WEST
这table列出了每个人得分的订单。
TABLE:ORD
OrderId
Sales
Region
1
MA
EAST
2
MA
WEST
3
SA
WEST
4
SA
EAST
5
SB
EAST
6
SB
WEST
7
MB
NORTH
8
SB
NORTH
我想要每个经理的订单输出。例如,经理 MA 看起来像这样:
OrderId
Sales
Region
1
MA
EAST
2
MA
WEST
4
SA
EAST
5
SB
EAST
8
SB
NORTH
对于 MB,它看起来像这样:
OrderId
Sales
Region
3
SA
WEST
6
SB
WEST
7
MB
NORTH
有SQL语句可以产生这个结果吗?
With cte_MA as (
Select Distinct sales from mar where manager = 'MA'
Union
Select Distinct manager from mar where manager = 'MA' ),
Cte_MB as (
Select Distinct sales from mar where manager = 'MB'
Union
Select Distinct manager from mar where manager = 'MB'),
Cte_MAorder as (
Select a orderid, a.sales, a region from ord a inner cte_MA b on a.sales = b.sales),
Cte_MBorder as (
Select a.orderid, a.sales, a.region from ord a inner cte_MB b on a.sales = b.sales)
select * from cte_MAorder;
Select * from cte_MBorder;
如果您 运行 select * 从 cte_maorder 查询您获得 MA 分配的销售员订单列表,您应该 运行 所有查询最多 select 查询
你 运行 select * 从 cte_mborder 查询你得到 MB 分配的销售员订单列表
下面的查询会得到你想要的结果,你只需要换出你正在寻找的任何经理的位置,这 returns 你正在寻找的,对于 MA .
SELECT Distinct A.OrderId, A.Sales, A.Region FROM ORD a
left join MSR b on (a.Sales = b.Sales OR B.Manager = A.Sales) and a.Region = B.Region
where B.Manager = 'MA' or A.Sales = 'MA'
order by orderid asc
如果出于任何原因您需要 return/use 经理,您可以使用如下内容:
SELECT ORD.*, IFNULL(MSR.Manager,ORD.Sales) as ActualManager
FROM ORD
LEFT JOIN MSR
ON (ORD.Sales = MSR.Sales and ORD.Region = MSR.Region)
WHERE IFNULL(MSR.Manager,ORD.Sales) = 'MA'
ORDER BY OrderId
我有以下销售结构。经理们在每个地区都被分配了一名销售人员,但他们也推销自己。下面的 table 包含经理和 salespersons/regions 之间的 link。
TABLE:MSR
Manager | Sales | Region |
---|---|---|
MA | SA | EAST |
MA | SB | EAST |
MA | SB | NORTH |
MB | SA | WEST |
MB | SB | WEST |
这table列出了每个人得分的订单。
TABLE:ORD
OrderId | Sales | Region |
---|---|---|
1 | MA | EAST |
2 | MA | WEST |
3 | SA | WEST |
4 | SA | EAST |
5 | SB | EAST |
6 | SB | WEST |
7 | MB | NORTH |
8 | SB | NORTH |
我想要每个经理的订单输出。例如,经理 MA 看起来像这样:
OrderId | Sales | Region |
---|---|---|
1 | MA | EAST |
2 | MA | WEST |
4 | SA | EAST |
5 | SB | EAST |
8 | SB | NORTH |
对于 MB,它看起来像这样:
OrderId | Sales | Region |
---|---|---|
3 | SA | WEST |
6 | SB | WEST |
7 | MB | NORTH |
有SQL语句可以产生这个结果吗?
With cte_MA as (
Select Distinct sales from mar where manager = 'MA'
Union
Select Distinct manager from mar where manager = 'MA' ),
Cte_MB as (
Select Distinct sales from mar where manager = 'MB'
Union
Select Distinct manager from mar where manager = 'MB'),
Cte_MAorder as (
Select a orderid, a.sales, a region from ord a inner cte_MA b on a.sales = b.sales),
Cte_MBorder as (
Select a.orderid, a.sales, a.region from ord a inner cte_MB b on a.sales = b.sales)
select * from cte_MAorder;
Select * from cte_MBorder;
如果您 运行 select * 从 cte_maorder 查询您获得 MA 分配的销售员订单列表,您应该 运行 所有查询最多 select 查询 你 运行 select * 从 cte_mborder 查询你得到 MB 分配的销售员订单列表
下面的查询会得到你想要的结果,你只需要换出你正在寻找的任何经理的位置,这 returns 你正在寻找的,对于 MA .
SELECT Distinct A.OrderId, A.Sales, A.Region FROM ORD a
left join MSR b on (a.Sales = b.Sales OR B.Manager = A.Sales) and a.Region = B.Region
where B.Manager = 'MA' or A.Sales = 'MA'
order by orderid asc
如果出于任何原因您需要 return/use 经理,您可以使用如下内容:
SELECT ORD.*, IFNULL(MSR.Manager,ORD.Sales) as ActualManager
FROM ORD
LEFT JOIN MSR
ON (ORD.Sales = MSR.Sales and ORD.Region = MSR.Region)
WHERE IFNULL(MSR.Manager,ORD.Sales) = 'MA'
ORDER BY OrderId