tags:

views:

1076

answers:

1

Is there a quick way to duplicate the effect of the Count(Distinct [f]) in MS Access?

For example:

Data Table for a single referral (there are a few thousand of these in the real data):

| Referral ID | Assessment Date | Assessment Team | Service Provided | Service Team
| 1           | 02/01/2008      | AAA             | BBB              | AAA
| 1           | 02/01/2008      | AAA             | CCC              | AAA
| 1           | 02/01/2008      | AAA             | DDD              | BBB
| 1           | 03/01/2008      | BBB             | EEE              | CCC

I want a query that gives:

| Referral ID | Number of Assessments | Teams Assessing | Services Provided | No Teams Providing
| 1           | 2                     | 2               | 4                 | 3

Any help appreciated!

+4  A: 

This is somewhat complicated, but might suit. Imp is your table.

SELECT DISTINCT a.[Referral ID], b.CountOfADate, c.CountOfATeam, d.CountOfService, e.CountOfSTeam

FROM (((imp AS a 

INNER JOIN 
    (SELECT b1.[Referral ID], Count(b1.ADate) AS CountOfADate
     FROM (SELECT DISTINCT t.[Referral ID], t.[Assessment Date] As ADate FROM imp As t) AS b1
     GROUP BY b1.[Referral ID]) AS b 
ON a.[Referral ID] = b.[Referral ID]) 

INNER JOIN 
    (SELECT c1.[Referral ID], Count(c1.ATeam) AS CountOfATeam
    FROM (SELECT DISTINCT t.[Referral ID], t.[Assessment Team] As ATeam FROM imp As t) AS c1
    GROUP BY c1.[Referral ID]) AS c 
ON a.[Referral ID] = c.[Referral ID]) 

INNER JOIN 
    (SELECT d1.[Referral ID], Count(d1.Service) AS CountOfService
    FROM (SELECT DISTINCT t.[Referral ID], t.[Service Provided] As Service FROM imp As t) AS d1
    GROUP BY d1.[Referral ID]) AS d 
ON a.[Referral ID] = d.[Referral ID]) 

INNER JOIN 
    (SELECT e1.[Referral ID], Count(e1.STeam) AS CountOfSTeam
    FROM (SELECT DISTINCT t.[Referral ID], t.[Service Team] As STeam FROM imp As t) AS e1
    GROUP BY e1.[Referral ID]) AS e 
ON a.[Referral ID] = e.[Referral ID];
Remou
I was hoping to avoid nested selects, I must admit, but if that's the way it needs to be done in Access, that's the way it needs to be done...Thanks for the response!
mavnn
Is there some reason this can't be done with a crosstab?
David-W-Fenton
There is a limit on the number of column headings, is there not? Therefore, several crosstabs would be required, yesno?
Remou
The issue with crosstabs isn't the number of columns, it's the fact that the count function in Access cannot be told to count unique values (it just counts all of them). The results table above would show '4' in all columns, for example, even though only 2 distinct teams were involved.
mavnn