views:

384

answers:

2

HI,

I have a large table from which i can query to get the following table

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

also i can get another table

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

but now i want to write a query which could get me the following table

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%

How can i write a query to get this?

Thanks

+2  A: 

Assuming a table like this:

type, code, ... other columns.

I assume your first 2 queries are something like

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

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

Then you want to do something like

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

Hope this helps. Chris

Chris Kimpton
+2  A: 

How about:

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

Where t is the name of the big table.

Remou
thanks that did the work. also can i round of the percent to single decimal point in th e query itself
tksy
i can round it off but how do i get the % symbol in to the results
tksy
Remou
If I wanted to draw graphs of the generated tables, would it be better for me to export them to excel and then try to plot them or do it in access itself. Either way i would want to do it with vba.
tksy
Hard to say. I would use Access, because I am familiar with it and rarely need anything very complex. You situation may be different.
Remou
Here in theis query if i wanted to order t.code by ascending shouldnt order by t.code asc work?
tksy
No, it is already ordered by type and then by code.
Remou
The reason i asked that was i am trying to plot graph were i would like %utyp for all code = 1, i thought it would be easier if code was in ascending order
tksy
When i use this query in vba i get a end of statement error in "code" at second line
tksy
Have you got all your quotes and line continuation characters in the right place? Don't forget to change internal string quotes to single quotes.
Remou