kittyfool said:
I need your help on my crosstab query again.
How can i exclude a count if a particular customer has a value of zero (0)
in
that particular period?
I don't understand.
Can you relate it back to our example data?
Q1:
SELECT
[RM Consolidated] As RMC,
Period,
[Cust Name] As CustName
FROM
DPM
GROUP BY
[RM Consolidated],
Period,
[Cust Name];
suppose results:
RMC Period CustName
A 2006q1 Homer
A 2006q1 Marge
A 2006q1 Bart
A 2006q2 Lisa
A 2006q2 Maggie
B 2006q1 Homer
B 2006q2 Homer
TRANSFORM Count(*) As CntOfUniqueCusts
SELECT
RMC
FROM
Q1
GROUP BY RMC
PIVOT Period;
RMC 2006q1 2006q2
A 3 2
B 1 1
Homer does not have a "value" (record) in Period
of "2006q2" for RMC of "A", but does have a
"value" (record) for RMC of "B" for Period
of "2006q2."
How can i exclude a count if a particular customer has
a value of zero (0) in that particular period?
Do you want result to be?
RMC 2006q1 2006q2
A 3
B 1 1
Please reply back if that is the case,
but I doubt that is what you mean?
====================
Or is it that your "Q1" is also providing
a "value" for each group?
Q1:
SELECT
[RM Consolidated] As RMC,
Period,
[Cust Name] As CustName,
Sum([Sales] - [Returns]) As NetSales
FROM
DPM
GROUP BY
[RM Consolidated],
Period,
[Cust Name];
and supposing results:
RMC Period CustName NetSales
A 2006q1 Homer 0
A 2006q1 Marge 5
A 2006q1 Bart 1
A 2006q2 Lisa 8
A 2006q2 Maggie 1
B 2006q1 Homer 4
B 2006q2 Homer 3
For "A/2006q1" Homer had at least one
sale but had the same returns, so his
"value" of NetSales was zero(0), so
you do not want to include him in the
count for "A/2006q1" ?
If that is the case one method would be to
introduce a HAVING clause in Q1:
Q1:
SELECT
[RM Consolidated] As RMC,
Period,
[Cust Name] As CustName,
Sum([Sales] - [Returns]) As NetSales
FROM
DPM
GROUP BY
[RM Consolidated],
Period,
[Cust Name]
HAVING
([Cust Name] = "Homer"
AND
Sum([Sales] - [Returns]) = 0) = False;
RMC Period CustName NetSales
A 2006q1 Marge 5
A 2006q1 Bart 1
A 2006q2 Lisa 8
A 2006q2 Maggie 1
B 2006q1 Homer 4
B 2006q2 Homer 3
Homer is gone from group "A/2006q1"
so our xtab would produce
RMC 2006q1 2006q2
A 2 2
B 1 1
if you were not concerned with *just* Homer,
but did not want to include *any* [Cust Name]
whose "value" was 0, then alter the Having clause
SELECT
[RM Consolidated] As RMC,
Period,
[Cust Name] As CustName
FROM
DPM
GROUP BY
[RM Consolidated],
Period,
[Cust Name]
HAVING
Sum([Sales] - [Returns]) > 0;
which would return
RMC Period CustName
A 2006q1 Bart
A 2006q1 Marge
A 2006q2 Lisa
A 2006q2 Maggie
B 2006q1 Homer
B 2006q2 Homer
again, Homer is gone from group "A/2006q1"
but, if any others had "value" <= 0, they would
be gone also...
and xtab
TRANSFORM Count(*) As CntOfUniqueCusts
SELECT
RMC
FROM
Q1
GROUP BY RMC
PIVOT Period;
would still produce (since only Homer "value" of 0)
RMC 2006q1 2006q2
A 2 2
B 1 1
====================
quick review of designing Q1 in grid....
when you change a select query to a "group by"
(by clicking on Sigma icon in toolbar), you get
an extra row ("Total:") in grid
Field: CustName: [Cust Name]
Table: DPM
Total: Group By
Sort:
Show:
Criteria:
or:
Besides "Group By," this row can be any of
the aggregate functions (like Sum), plus it can
also be either
Where
Expression
an Expression can use any previous alias
(like "CustName" above) if you do not
apply a Criteria to it.
Once you apply a Criteria to an expression,
that expression gets added to the HAVING
clause, and the alias is "out-of-scope" in both
the WHERE and HAVING clauses.
{WHERE filters before the grouping,
HAVING filters after the groupings
have been done}
If we had tried the following
(using both aliases "CustName" and "Ignore")
Field: Ignore: [CustName] = "Homer"
Table: DPM
Total: Expression
Sort:
Show: <checked>
Criteria:
or:
Access will be happy...
[Ignore] will show up as field in the
query results with value of -1 (true)
or 0 (false).
But, if we wanted to filter out the groups by
writing out in SQL
HAVING [Ignore] = True
or
([CustName] = "Homer") = True
or, in grid,
Field: Ignore: [CustName] = "Homer"
Table: DPM
Total: Expression
Sort:
Show: <checked>
Criteria: True
or:
Access will ask you what the alias "CustName" is....
So.....in grid, for a field expression that
will have a Criteria, remember to not use
any aliases.....
Field: Ignore: [Cust Name] = "Homer"
Table: DPM
Total: Expression
Sort:
Show:
Criteria: True
or:
or, writing out SQL...
HAVING ([Cust Name] = "Homer") = True
Plus, remember that if you set
Total: Expression
and give that column a Criteria,
it will end up in the HAVING clause.
Sometimes it is more efficient (not in our
specific case here) to filter before the
grouping occurs, so you would want
Total: Where