Distinct的使用


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58
Distinct不仅可用在SELECT中,也可用在COUNT内
如:查询出所有在一线、二线部门都出过单的客户
方法:按客户ID分组,对成单的部门进行DISTINCT,次数大于2的即是
SELECT a.CustomerID
FROM bdOrder a(NOLOCK)
JOIN frmuser f ON a.SalesStaff = f.Account
JOIN mdDepartment md ON f.DepartmentId = md.ID
WHERE md.DepartType IN('first','second')
AND a.OrderDate >='2011-01-01'
GROUP BY a.CustomerID
HAVING COUNT(DISTINCT md.DepartType) >=2