Sql 此解决方案是否可以通过案例生成..何时生成?
编写一个查询,将字符串附加到所选字段,指示指定的销售人员是否与其所在城市的客户匹配Sql 此解决方案是否可以通过案例生成..何时生成?,sql,Sql,编写一个查询,将字符串附加到所选字段,指示指定的销售人员是否与其所在城市的客户匹配 salesman_id | name | city | commission -------------+------------+----------+------------ 5001 | James Hoog | New York | 0.15 5002 | Nail Knite | Paris | 0.13 5005 | Pi
salesman_id | name | city | commission
-------------+------------+----------+------------
5001 | James Hoog | New York | 0.15
5002 | Nail Knite | Paris | 0.13
5005 | Pit Alex | London | 0.11
5006 | Mc Lyon | Paris | 0.14
5007 | Paul Adam | Rome | 0.13
5003 | Lauson Hen | San Jose | 0.12
customer_id | cust_name | city | grade | salesman_id
-------------+----------------+------------+-------+-------------
3002 | Nick Rimando | New York | 100 | 5001
3007 | Brad Davis | New York | 200 | 5001
3005 | Graham Zusi | California | 200 | 5002
3008 | Julian Green | London | 300 | 5002
3004 | Fabian Johnson | Paris | 300 | 5006
3009 | Geoff Cameron | Berlin | 100 | 5003
3003 | Jozy Altidor | Moscow | 200 | 5007
3001 | Brad Guzan | London | | 5005
这就是提供的解决方案
SELECT a.salesman_id, name, a.city, 'MATCHED'
FROM salesman a, customer b
WHERE a.city = b.city
UNION
(SELECT salesman_id, name, city, 'NO MATCH'
FROM salesman
WHERE NOT city = ANY
(SELECT city
FROM customer))
ORDER BY 2 DESC
Select s.salesman_id, name, city,
case when s.city = c.city then 'Matched'
else 'No Match'
end as City_Match
from salesman s
join customer c
on s.salesman_id = c.salesman_id;
这是我想出来的一个解决方案,我想知道它是否会像提供的一样工作
SELECT a.salesman_id, name, a.city, 'MATCHED'
FROM salesman a, customer b
WHERE a.city = b.city
UNION
(SELECT salesman_id, name, city, 'NO MATCH'
FROM salesman
WHERE NOT city = ANY
(SELECT city
FROM customer))
ORDER BY 2 DESC
Select s.salesman_id, name, city,
case when s.city = c.city then 'Matched'
else 'No Match'
end as City_Match
from salesman s
join customer c
on s.salesman_id = c.salesman_id;
预期结果
salesman_id name city ?column?
5005 Pit Alex London MATCHED
5007 Paul Adam Rome NO MATCH
5002 Nail Knite Paris MATCHED
5006 Mc Lyon Paris MATCHED
5003 Lauson Hen San Jose NO MATCH
5001 James Hoog New York MATCHED
第一个查询是寻找(在第二部分)在一个没有顾客的城市里的任何推销员。您查询的是销售人员所在城市与客户所在城市匹配的情况 使用左连接和case语句的等效查询将是
Select distinct s.salesman_id, name, city,
case when c.city is not null then 'Match' else 'No Match' end as City_Match
from salesman s
left join customer c on s.city = c.city;
第一个查询是寻找(在第二部分)在一个没有顾客的城市里的任何推销员。您查询的是销售人员所在城市与客户所在城市匹配的情况 使用左连接和case语句的等效查询将是
Select distinct s.salesman_id, name, city,
case when c.city is not null then 'Match' else 'No Match' end as City_Match
from salesman s
left join customer c on s.city = c.city;
将
exists
与子查询一起使用:
select s.*,
(case when exists (select 1
from customer c
where c.salesman_id = s.salesman_id and
c.city = s.city
)
then 'MATCHED' else 'NO MATCH'
end) as flag
from salesman s;
您提供的示例查询是一个笑话。它必须是:
- 它使用无意义的表别名
- 它使用古老的逗号语法,而不是正确、明确、标准的连接语法
- 它依赖于
删除重复项,而这是不必要的开销union
- 它使用的操作比您想要的逻辑所需的操作多得多
存在
和子查询:
select s.*,
(case when exists (select 1
from customer c
where c.salesman_id = s.salesman_id and
c.city = s.city
)
then 'MATCHED' else 'NO MATCH'
end) as flag
from salesman s;
您提供的示例查询是一个笑话。它必须是:
- 它使用无意义的表别名
- 它使用古老的逗号语法,而不是正确、明确、标准的连接语法
- 它依赖于
删除重复项,而这是不必要的开销union
- 它使用的操作比您想要的逻辑所需的操作多得多