You need the [ ] around the query name since otherwise, that name will be
seen with a subtraction, and there is a space in the name: that is not a
legal name, so you need [ ] around it.
The PIVOT clause determines the new columns. Here, we would create
columns
"[less than 0[" ,"[0-45[" ,"[45-60[", and ,"[60+"
(which are also illegal names, by the way).
Under each of these new columns, for each group, which are for every
CNTYNAME value, the COUNT of not null values in RefBeforeOpen is
computed.
The alias supplied by the query designer, CountOfRefBeforeOpen, is not
use
furthermore but it could be use if we were interested by reaching each
"cell" of the tableau of COUNTs, in our SQL Statement:
For a given record, in the initial table (here, [qsubReport-Sect F] used
as
if it was a table), to know under which of the new columns a record will
increase the COUNT, it is a matter 'decided' by the SWITCH statement: the
first time a condition evaluates to true, the 'name' following that said
condition will be the name of the new column for which this record will
increase the COUNT.
As example, if RefBeforeOpen = 32 for a record, then
Switch( RefBeforeOpen<0, "[less than 0[",
RefBeforeOpen<45, "[0-45[",
RefBeforeOpen<90, "[45-60[",
True, "[60+" )
would return "[0-45[" and the count under that column will be
increased.
Vanderghast, Access MVP
jmoore said:
Thank you. I got it to work after a couple of error messages. I
copied
the
code below that works, but I would like to understand why. I have
other
queries in the same database that work without the brackets around the
query
name, but this one did not. Access also added "AS
CountOfRefBeforeOpen"
in
the transform statement - I did not type it.
I am trying to learn SQL on my own. Would you know where I could find
tutorials/information that would be helpful? Thanks.
TRANSFORM Count([qsubReport-Sect F].RefBeforeOpen) AS
CountOfRefBeforeOpen
SELECT [qsubReport-Sect F].CNTYNAME
FROM [qsubReport-Sect F]
GROUP BY [qsubReport-Sect F].CNTYNAME
PIVOT Switch([qSubReport-Sect F].RefBeforeOpen<0,"[less than 0[",
RefBeforeOpen<45,"[0-45[",
RefBeforeOpen<90,"[45-60[",
True,"[60+");
:
TRANSFORM COUNT(RefBeforeOpen)
SELECT cntyname
FROM [qsubReport-Sect F]
GROUP BY cntyname
PIVOT SWITCH( RefBeforeOpen <=0, "up to 0 incl.",
RefBeforeOpen <45, "[0-45[",
RefBeforeOpen< 90, "[45-90[",
RefBeforeOpen< 120, "[90-120[",
True, "[120+" )
You can also use the predefined VBA function PARTITION if the
intervals
are
regular (ie. always by step of 45, rather than, like here, 90-120 is a
step
of 30 )
Vanderghast, Access MVP
I have a query based on a query that has a calculated field to
display
the
results, in days, between two date fields. The example below gives
me
the
count of records between 0 and 45 days. I also want to calculate 3
other
ranges. I can get the results I need with 4 queries, but it seems
there
should be a better way to do it to. Any advice?
SELECT DISTINCT [qSubReport-Sect F].CNTYNAME,
Count([qSubReport-SectF].RefBeforeOpen) AS UnderZero
FROM [qSubReport-Sect F]
WHERE (([qSubReport-Sect F].RefBeforeOpen) BETWEEN 0 AND 45
GROUP BY [qSubReport-Sect F].CNTYNAME;