T
TipTop
Hi,
I have a (fairly) complicated query that I'm piecing together. There's
just one part of it that I can't seem to get my head around - maybe it's
not possible.
Here's an example I've made up to simplify my question (rather than
confuse things with the parts that already work). Essentially, I want to
get a list of all the SalesReps who have made sales in a certain
SalesRegion. But... accompanying each row I want to have a count of the
number of NewClient sales they have made - not just for the one region
but for all regions.
SELECT SalesRep, NewSales 'need to sum this somehow
FROM
(SELECT SalesRep,SalesRegion, SUM(IIF(IsNewClient,1,0)) as NewSales
FROM MySales
WHERE Sales.Date = #01/01/2005#
GROUP BY SalesRep, SalesRegion)
WHERE
SalesRegion = 1
The subquery will return something like:
name region totalnewsales
DAVE, 1 8
DAVE, 2 4
DAVE, 3 6
CHRIS 2 3
SUE 1 9
SUE 2 5
etc.
And I want the final result to be thus
DAVE, 18
SUE 14
i.e. only the people who have sales in region 1 but with the count of
new sales for all regions.
How do I get the the totals? Sure I could just query the database twice
for the required date but that seems a little clumsy to have it scan the
full table twice. Is there a way that I can use the subquery in one
query twice?
Thanks for any help,
Tiptop
I have a (fairly) complicated query that I'm piecing together. There's
just one part of it that I can't seem to get my head around - maybe it's
not possible.
Here's an example I've made up to simplify my question (rather than
confuse things with the parts that already work). Essentially, I want to
get a list of all the SalesReps who have made sales in a certain
SalesRegion. But... accompanying each row I want to have a count of the
number of NewClient sales they have made - not just for the one region
but for all regions.
SELECT SalesRep, NewSales 'need to sum this somehow
FROM
(SELECT SalesRep,SalesRegion, SUM(IIF(IsNewClient,1,0)) as NewSales
FROM MySales
WHERE Sales.Date = #01/01/2005#
GROUP BY SalesRep, SalesRegion)
WHERE
SalesRegion = 1
The subquery will return something like:
name region totalnewsales
DAVE, 1 8
DAVE, 2 4
DAVE, 3 6
CHRIS 2 3
SUE 1 9
SUE 2 5
etc.
And I want the final result to be thus
DAVE, 18
SUE 14
i.e. only the people who have sales in region 1 but with the count of
new sales for all regions.
How do I get the the totals? Sure I could just query the database twice
for the required date but that seems a little clumsy to have it scan the
full table twice. Is there a way that I can use the subquery in one
query twice?
Thanks for any help,
Tiptop