Here you go. I have some other ideas that might help you as well, I will put them in following posts - as I need to get back to some real work.
Thanks for this problem - it was a challenge.
Have Fun!
munkyCmunkyDU
<=================================================>
TABLES
-------------------------
TABLE company_local
id int NOT NULL, (PK)
repname varchar (50),
coname varchar (50),
cosize varchar (50)
TABLE actionsMB
actiontype varchar (50),
coid int NULL (FK)
DATA
--------------------------
company_local
1 joe big company big
2 harry med company med
3 al sm company sm
actionsMB
Decision Maker 2
Decision Maker 1
Golf 1
Golf 2
Golf 3
Dinner 1
Dinner 2
Dinner 3
Here is the query... (sorry - its a beast)
---------------------------------------------
select
id,
max(TBL.rep)AS rep,
max(TBL.company) as company,
max(TBL.size) as size,
max(TBL.allActions) as [All Actions],
max(TBL.specific) as [Decision Maker]
FROM
( SELECT
company_local.id,
MAX(company_local.repname) AS rep,
MAX(company_local.coname) AS company,
MAX(company_local.cosize) AS size,
COUNT(allActions.actiontype) AS allActions,
null as specific
FROM
company_local LEFT OUTER JOIN actionsMB allActions ON company_local.id = allActions.coid
GROUP BY company_local.id
UNION
SELECT
company_local.id,
MAX(company_local.repname) AS rep,
MAX(company_local.coname) AS company,
MAX(company_local.cosize) AS size,
null as allActions,
COUNT(specificActions.actiontype) AS specific
FROM
company_local LEFT OUTER JOIN actionsMB specificActions ON company_local.id = specificActions.coid
WHERE (specificActions.actiontype = 'Decision Maker')
GROUP BY company_local.id
) AS TBL
GROUP BY TBL.id
Here are your results...
--------------------------------------------------------------
ID REP COMPANY SIZE ALL DECISION MAKER
1 joe big company big 3 1
2 harry med company med 3 1
3 al sm company sm 2 NULL