使用两个表的并集获取Count&ŧ039;s
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用两个表的并集获取Count&ŧ039;s相关的知识,希望对你有一定的参考价值。
I was having the hardest time getting this to work. It's a collection of various items. For whatever reason, I needed to include the CASE statement due to NULL's coming back. If this helps you out... go for it.
EXAMPLE 1 ----- SELECT Activity , COUNT (registrationID) AS totals FROM (SELECT CASE WHEN r1.Activity0403_Winner <> 'Spa (50 minutes)' THEN r1.Activity0403_Winner ELSE r1.Activity0403_Winner + ' - ' + r1.Activity0403_WinnerSpaChoice + ' ~ ' + r1.Activity0403_WinnerSpaTime END AS Activity , r1.registrationID FROM Registrations r1 WHERE r1.Activity0403_Winner IS NOT NULL AND r1.Activity0403_Winner <> 'No Activity' UNION ALL SELECT CASE WHEN r2.Activity0403_Guest <> 'Spa (50 minutes)' THEN r2.Activity0403_Guest ELSE r2.Activity0403_Guest + ' - ' + r2.Activity0403_GuestSpaChoice + ' ~ ' + r2.Activity0403_GuestSpaTime END AS Activity , r2.registrationID FROM Registrations r2 WHERE r2.Activity0403_Guest IS NOT NULL AND r2.Activity0403_Guest <> 'No Activity' ) AS X (Activity, registrationID) GROUP BY X.Activity ORDER BY X.Activity; EXAMPLE 2 ----- SELECT X.emp_id, COUNT(project_id) AS project_tot FROM (SELECT emp_id, project_id FROM DB1.Table1 UNION SELECT emp_id, project_id FROM DB2.Table1) AS X(emp_id, project_id) GROUP BY X.emp_id;
以上是关于使用两个表的并集获取Count&ŧ039;s的主要内容,如果未能解决你的问题,请参考以下文章