2008-12-10 6 views
1

HI,requête d'accès: comment interroger pour obtenir des valeurs calculées?

J'ai une grande table à partir de laquelle je peux interroger pour obtenir le tableau suivant

type  no of times type occurs 
101   450 
102   562 
103   245 

aussi je peux obtenir une autre table

code  no of times code occurs 
0    1222 
1    750 
2    355 

mais maintenant je veux écrire un requête qui pourrait me obtenir le tableau suivant

type no of timescode1occurs %of timescode1 occurs out of %of times code1 occurs out of 
           no of times type occurs  no of times code occcurs 

101   50    11%         6% 
102   75    13%         10% 

Comment puis-je écrire une requête pour obtenir ceci?

Merci

Répondre

2

Que diriez-vous:

SELECT t.Type, t.Code, COUNT(t.Code) AS CountOfCode, 
    [CountOfCode]/DCount("Code","t","Code=" & [Code])*100 AS PercentCode, 
    [CountOfCode]/DCount("Type","t","Type=" & [Type])*100 AS PercentType 
     FROM t 
     GROUP BY t.Type, t.Code 

Où t est le nom de la grande table.

+0

merci qui a fait le travail. Je peux aussi arrondir le pour cent à la virgule décimale seule dans la requête elle-même – tksy

+0

je peux l'arrondir mais comment puis-je obtenir le symbole% dans les résultats – tksy

+0

One way: ... Format ([CountOfCode]/DCount (" Tapez "," t "," Type = "et [Type])," Pour cent ") AS [% Type] – Fionnuala

2

En supposant une table comme ceci:

type, code, ... other columns. 

je suppose que vos 2 premières requêtes sont quelque chose comme

select type, count(*) from mytable group by type 

select code, count(*) from mytable group by code 

Ensuite, vous voulez faire quelque chose comme

SELECT DISTINCTROW mytable.Type, mytable.Code, 
Count(*)/q1.[Count of type] AS [Percent Of Type], 
Count(*)/q2.[Count of code] AS [Percent Of Code] 
FROM mytable, 
    (select type, count(*) as [Count of type] from mytable group by type) q1, 
    (select code, count(*) as [Count of code] from mytable group by code) q2 
where mytable.Type =q1.Type 
and mytable.Code=q2.Code 
GROUP BY mytable.Type, mytable.Code, q1.[Count of type], q2.[Count of code]; 

J'espère que cela aide. Chris

Questions connexes