此解决方案能否与案件..何时?

问题描述 投票:0回答:1

写一个将字符串附加到所选字段的查询,以表明指定的推销员是否与所在城市的客户匹配?

 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


 This is the solution provided.

 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;

 Expected outcome.

 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
sql
1个回答
0
投票

第一个查询正在(在第二部分中)寻找在没有客户的城市中的任何推销员。您要查询的是业务员所在城市与客户所在城市匹配的情况。

使用左联接和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;
© www.soinside.com 2019 - 2024. All rights reserved.