PC Review


Reply
Thread Tools Rate Thread

Distinct Count in Crosstab

 
 
Annette
Guest
Posts: n/a
 
      23rd Mar 2011
I have an inspection table that has the following fields:
InspectionID, Type, Group, Question#, Status
I want a crosstab query that will list Type, Group and count the # of
questions for each status.

For example
Inspection ID Type Group Question Status
12 Comp CDC 1 In
12 Comp CDC 2 Out
12 Comp CDC 3 In
13 Comp CDC 1 In
13 Comp CDC 2 In
13 Comp CDC 3 In
12 Comp PTL 4 In
13 Comp PTL 4 Out


I would like the crosstab to return the following
Type Group Total Inspections In Out
Comp CDC 2 5 1
Comp PTL 2 1 1

Instead I am getting
Type Group Total Inspections In Out
Comp CDC 6 5 1
Comp PTL 2 1 1

In other words, the Total Inspection count needs to be the the count
of the distinct Inspection ID's and not the number of entries.
 
Reply With Quote
 
 
 
 
Bob Quintal
Guest
Posts: n/a
 
      23rd Mar 2011
Annette <(E-Mail Removed)> wrote in
news:ebfce22c-80fb-4178-b411-(E-Mail Removed)
m:

> I have an inspection table that has the following fields:
> InspectionID, Type, Group, Question#, Status
> I want a crosstab query that will list Type, Group and count the #
> of questions for each status.
>
> For example
> Inspection ID Type Group Question Status
> 12 Comp CDC 1 In
> 12 Comp CDC 2 Out
> 12 Comp CDC 3 In
> 13 Comp CDC 1 In
> 13 Comp CDC 2 In
> 13 Comp CDC 3 In
> 12 Comp PTL 4 In
> 13 Comp PTL 4 Out
>
>
> I would like the crosstab to return the following
> Type Group Total Inspections In Out
> Comp CDC 2 5 1
> Comp PTL 2 1 1
>
> Instead I am getting
> Type Group Total Inspections In Out
> Comp CDC 6 5 1
> Comp PTL 2 1 1
>
> In other words, the Total Inspection count needs to be the the
> count of the distinct Inspection ID's and not the number of
> entries.
>


You need to break down the process into a few steps.
I'll show the SQL, but you can build your queries in the designer.

The first step is a simple query
Select Distinct [Inspection ID] from [TheTable];

the second step is to count the values in a Totals query
Select [Inspection ID], count([Inspection ID]) as [Total
Inspections] from [The First Query] Group By [Inspection ID];

The third step is to build a Crosstab query using the Table and the
second query.joined on the Inspection ID

TRANSFORM count([TheTable].[status]) as [whatever]
SELECT [TheTable].Type, [TheTable].Group,
[Second Query].[Total Inspections]
FROM [TheTable] Inner Join [Second Query]
ON [TheTable].[Inspection ID] =[Second Query].[Inspection ID]
GROUP BY [TheTable].Type, [TheTable].Group, [Second Query].[Total
Inspections]
PIVOT [TheTable].[Status];

--
Bob Q.
PA is y I've altered my address.
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Count Distinct in a crosstab Rita Brasher Microsoft Access Queries 2 30th Jul 2009 08:49 PM
Count distinct value Boon Microsoft Access Queries 1 25th Sep 2008 10:43 PM
COUNT DISTINCT ? Jay Microsoft Access Queries 5 23rd Feb 2008 08:03 PM
Distinct Count in Crosstab Query kittyfool via AccessMonster.com Microsoft Access Queries 11 23rd Nov 2006 01:42 PM
DISTINCT in a crosstab query? =?Utf-8?B?RGVubmlz?= Microsoft Access Reports 5 22nd Dec 2004 08:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:00 PM.