Joining two count results

A

Adam Pedder

I've got a problem with a query that I'm sure should be quite simple.

Basically I have a table that looks like this:

Date | Agent
01/01/2001 | 12345
01/02/2001 | 12345
01/01/2001 | 12346

An I want to return this:

Count 1 | Agent | Count 2
1 | 12345 | 2
1 | 12346 | 1

Where Count 1 is the number of claims since 01/02/2001 and Count 2 is the
number of claims since 01/01/2001.

Count 1 is easy with Select Count(*), agent from Claims where date >
'01/02/2001' group by agent.but I cannot get Count 2 in there.

I'm at the point of using a temporary table to store the results, but there
must be a simple way of Joining two select statements on the same table.

Can anyone help?

Adam
 
D

Dale Fye

Adam,

How about:

SELECT Agent
, SUM(iif(DateField > #1/2/2001#, 1, 0)) as Count1
, SUM(iif(DateField > #1/1/2001#, 1, 0)) as Count2
FROM yourTable
GROUP BY Agent

BTW, Date is a reserved word in Access, so I would advise
against using it as a field name. I have changed it to
DateField in my example.

HTH
Dale
 
G

Gerald Stanley

Try something along the lines of
SELECT Q1.agent, Q1.claims, Q2.claims
FROM
(SELECT agent, Count(*) as Claims FROM YourTable WHERE
claimDate > #01/01/2001# GROUP BY agent) As Q1,
(SELECT agent, Count(*) as Claims FROM YourTable WHERE
claimDate > #01/02/2001# GROUP BY agent) As Q2
WHERE Q1.agent = Q2.agent

You should be aware that this will only produce a row for
agents who return claim counts in both queries.

Hope This Helps
Gerald Stanley MCSD
 
A

Adam Pedder

Thanks a lot, I needed that kick in the right direction.

I was actually using an ADP (forgot to mention that), but I converted the
iifs to CASEs and away I went.

Thanks again.

Adam
 
G

Guest

Glad I could help.
-----Original Message-----
Thanks a lot, I needed that kick in the right direction.

I was actually using an ADP (forgot to mention that), but I converted the
iifs to CASEs and away I went.

Thanks again.

Adam




.
 

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

Similar Threads

Multiple Queries 5
SELECT Statement Question 5
Counts number of entries per day in query 4
Access Count dates within a Month 4
Count Records By Emp 2
Need a query to count 'Yes' values per month 8
MTD 3
Count with condition Queries 6

Top