Counting populated fields in crosstab

A

Adam Carpenter

Hello,

How can I count the number of columns in a row of a crosstab query that
contain data?

Thanks in anticipation.

Adam
 
M

Michel Walsh

Hi,


The number of rows is equal to the number of groups, which is equal
to the number of distinct values of the fields making the groups. The number
of fields is equal to the number of different distinct values the PIVOT
expression can supply.

SELECT COUNT(*) FROM (SELECT DISTINCT f1, f2, f3 FROM myTable)


assuming the groups are based on f1, f2, f3.


Hoping it may help,
Vanderghast, Access MVP
 
R

Rufus T. Firefly

Use the form:

Transform aggregate(column) as somename
Select ......,count(somename) as [number of columns with data]

Utilities for Sql Server @
www.rac4sql.net
 
M

Michel Walsh

Hi Steve,


The count will occur for each group, and thus, the number could be
different, per record, unless you make no group at all (the result is then
just one record) that is... but that is not allowed in a Jet
crosstab...unless you "fake" it... and since we are at it, let us fake the
aggregate too:


TRANSFORM SUM(1) As toto
SELECT COUNT(toto) As ReadThisField
FROM myTable
GROUP BY 1
PIVOT whateverExpressionToBeReallyUsed





Vanderghast, Access MVP
 
R

Rufus T. Firefly

Michel Walsh" said:
The count will occur for each group, and thus, the number could be
different, per record, unless you make no group at all (the result is then
just one record) that is... but that is not allowed in a Jet
crosstab...unless you "fake" it... and since we are at it, let us fake the
aggregate too:


TRANSFORM SUM(1) As toto
SELECT COUNT(toto) As ReadThisField
FROM myTable
GROUP BY 1
PIVOT whateverExpressionToBeReallyUsed

Hi Michel,

Kewl!
But the OP wanted the count of the number of columns with data in each row:)
 
M

Michel Walsh

Hi Steve,


... indeed... I didn't read the OP question like that the first time,
but now that you mentionned it...


Vanderghast, Access MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top