我是SQL的新手,我在家庭作业上有这个问题:"列出ID低于100的客户代表的ID以及每个代表在邮政编码中服务的客户总数9或0(合并)"
到目前为止我有这段代码:
SELECT cust_rep, count(cust_nbr) as "CustomerCount"
FROM customer
WHERE cust_rep < 100
GROUP BY cust_rep
ORDER BY cust_rep;
但我不知道如何添加邮政编码限制。谢谢你的帮助!
答案 0 :(得分:1)
除了makeusre确保邮政编码的第一个字符是0或9
之外,这将执行您的查询正在执行的操作SELECT cust_rep, count(cust_nbr) as "CustomerCount"
FROM customer
WHERE cust_rep < 100
AND (
// adding quotes to avoid implicit conversion
LEFT(ZipCode, 1) = '0'
OR LEFT(ZipCode, 1) = '9'
)
GROUP BY cust_rep
ORDER BY cust_rep;
答案 1 :(得分:0)
试试这个。
SELECT CUST_REP, COUNT(*) as "CustomerCount"
FROM CUSTOMER
WHERE cust_rep < 100
AND (zipCode like '0%' OR zipCode like '9%')
GROUP BY cust_rep
ORDER BY cust_rep;