2009-02-26 113 views
1

有沒有一種快速方法可以在MS Access中複製Count(Distinct [f])的效果?MS Access中的重複計數(Distinct [f])

例如:

數據表爲一個單一的轉診(有這些幾千的真實數據):

| 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 

我想要一個查詢,給出了:

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

任何幫助表示讚賞!

回答

4

這有些複雜,但可能適合。 Imp是你的桌子。

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]; 
+0

我希望避免嵌套的選擇,我必須承認,但如果這就是它需要在Access做的方式,這就是它需要做的事情...... 感謝您的答覆的方式! – mavnn 2009-02-27 09:06:17