One more thing: I copied what you did and replaced the asterisks with the
field name (I assume this is correct). It looks as if I'm almost there, but
the issue now is that each column shows the total for ALL records, not just
those that correspond to the specific name and chart (I'm grouping by two
fields, [WR_LNG_NAM] and [Chart]).
Here is the exact SQL I am using:
SELECT [WR_LNG_NAM], [Chart],
(SELECT COUNT(Peak) FROM [*qryAllSongsByWriter]
WHERE Peak = 1) AS [#1],
(SELECT COUNT(Peak) FROM [*qryAllSongsByWriter]
WHERE Peak BETWEEN 2 AND 10) AS [2-10],
(SELECT COUNT(Peak) FROM [*qryAllSongsByWriter]
WHERE Peak BETWEEN 11 AND 20) AS [11-20],
(SELECT COUNT(Peak) FROM [*qryAllSongsByWriter]
WHERE Peak BETWEEN 21 AND 30) AS [21-30],
(SELECT COUNT(Peak) FROM [*qryAllSongsByWriter]
WHERE Peak BETWEEN 31 AND 40) AS [31-40],
(SELECT COUNT(Peak) FROM [*qryAllSongsByWriter]
WHERE Peak > 40) AS [>40],
(SELECT COUNT(Peak) FROM [*qryAllSongsByWriter]
WHERE Peak > 0) AS [Total]
FROM [*qryAllSongsByWriter]
GROUP BY [WR_LNG_NAM], [Chart]
ORDER BY [WR_LNG_NAM], [Chart];
Thanks,
Eric
Eric Stephens said:
Tom,
Thanks for the info. Unfortunately, I'm rather new to SQL, so I have a
couple of questions. First, since I will be pulling the information from a
single source (another query), I don't understand why you have two tables
listed (T and T1). Also, do I need to enter anything specific where the
asterisk is (*) or do I enter it as just that?
Thanks,
Eric
:
Dear Eric:
While it is possible to do something like this in a crosstab query,
I'm going to suggest an alternative. If the categories you show are
static and not based on ranges set up in a table where you could
reconfigure them, then a set of subqueries will do this:
SELECT [Name],
(SELECT COUNT(*) FROM YourTable T1
WHERE T1.Name = T.Name
AND Position BETWEEN 1 AND 10) AS [1-10],
(SELECT COUNT(*) FROM YourTable T1
WHERE T1.Name = T.Name
AND Position BETWEEN 11 AND 20) AS [11-20],
(SELECT COUNT(*) FROM YourTable T1
WHERE T1.Name = T.Name
AND Position BETWEEN 21 AND 30) AS [21-30],
(SELECT COUNT(*) FROM YourTable T1
WHERE T1.Name = T.Name
AND Position BETWEEN 31 AND 40) AS [31-40],
(SELECT COUNT(*) FROM YourTable T1
WHERE T1.Name = T.Name
AND Position > 40) AS [<40]
FROM YourTable T
GROUP BY [Name]
This illustrates that crosstabs are something that can be accomplished
using other means. The crosstab facility in Access is a shortcut to
doing that dynamically without coding. But it is also restrictive.
Tom Ellison
Microsoft Access MVP
Ellison Enterprises - Your One Stop IT Experts
On Tue, 12 Oct 2004 14:25:32 -0700, "Eric Stephens"
Hello,
I am trying to develop a query that will count the number of records in
which the value of a particular field falls within a specified range. For
example, if I have the following information:
Name Position
Mike 1
Mike 7
Mike 14
Mike 18
Mike 25
Mike 33
Mike 35
Mike 52
I want to get the following output:
Name 1-10 11-20 21-30 31-40 >40
Total
Mike 2 2 1 2
1 8
I am completely baffled on how to go about this. I tried using a Count
function in the Total row and entering the ranges in the Criteria row (in
Design view) for each of the ranges, but I get no records returned when I do
this. Even if there are no matching records within one of the ranges, I want
it to show as 0 for that range. Any help would be greatly appreciated! BTW,
my SQL skills are minimal at best.
Thanks,
Eric