我需要在SQL中解决以下查询

时间:2011-10-24 19:13:01

标签: sql-server

提供将所有货件发送到单个城市的客户列表(cust_name,cust_id)。 (注意:对于这些客户,每个城市可能相同或不同。)

我有四节课: 客户   CUST_ID   CUST_NAME   年收入   cust_type

城市 城市名称 人口

装运   装运ID   CUST_ID   重量   卡车_#   目的地   ship_date

卡车   卡车_#   truck_driver

我尝试了几个小时不同的事情,我只是在走向死胡同。

这是我提出的一件事:

SELECT DISTINCT cust_name, C.cust_id
FROM customer C
WHERE NOT EXISTS 
     (SELECT cust_id
      FROM shipment S, city
      WHERE C.cust_id = S.cust_id AND destination != destination)

2 个答案:

答案 0 :(得分:4)

针对新问题进行了更新:

SELECT cust_name, C.cust_id
FROM customer C
INNER JOIN shipment s
    ON c.cust_id = s.cust_id
GROUP BY Cust_name, c.cust_id
HAVING COUNT(DISTINCT s.destination) = 1

答案 1 :(得分:0)

这就是我最终做到这一点的方式,感谢所有的帮助

SELECT DISTINCT cust_name, C.cust_id    
FROM shipment S, customer C     
WHERE S.cust_id = C.cust_id AND 1 =     
     (SELECT COUNT(DISTINCT destination)     
      FROM shipment, customer     
      WHERE shipment.cust_id = S.cust_id and customer.cust_id = C.cust_id)