Counting days

  • Thread starter Thread starter Phil McFarlane
  • Start date Start date
P

Phil McFarlane

Have the following fields in a table: Date,Name,JobNo,Hours.
I need a SQL query to count the number of days each Name appears over a
period of days.
Any Name can appear more than once on any Date.
If a Name appeared three times on the same Date it should only count as one
day.

Any suggestions or help would be appreciated

Thanks
Phil_mac
 
SELECT name, COUNT(*)
FROM (SELECT DISTINCT name, [date]
FROM tableName
WHERE [date] BETWEEN firstDate AND lastDate) AS a
GROUP BY name


where firstDate and lastDate are parameters defining the range of date of
your interest.


Hoping it may help,
Vanderghast, Access MVP
 
Thanks for the quick reply. Unfortunately I left out an important fact. I am
using Access 97.

Cheers



Michel Walsh said:
SELECT name, COUNT(*)
FROM (SELECT DISTINCT name, [date]
FROM tableName
WHERE [date] BETWEEN firstDate AND lastDate) AS a
GROUP BY name


where firstDate and lastDate are parameters defining the range of date of
your interest.


Hoping it may help,
Vanderghast, Access MVP


Phil McFarlane said:
Have the following fields in a table: Date,Name,JobNo,Hours.
I need a SQL query to count the number of days each Name appears over a
period of days.
Any Name can appear more than once on any Date.
If a Name appeared three times on the same Date it should only count as
one
day.

Any suggestions or help would be appreciated

Thanks
Phil_mac
 
You should be able to use

SELECT name, COUNT(*)
FROM [SELECT DISTINCT name, [date]
FROM tableName
WHERE [date] BETWEEN firstDate AND lastDate]. AS a
GROUP BY name

That's an opening square bracket before, and a closing square bracket
followed by a period after.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Phil McFarlane said:
Thanks for the quick reply. Unfortunately I left out an important fact. I
am
using Access 97.

Cheers



Michel Walsh said:
SELECT name, COUNT(*)
FROM (SELECT DISTINCT name, [date]
FROM tableName
WHERE [date] BETWEEN firstDate AND lastDate) AS a
GROUP BY name


where firstDate and lastDate are parameters defining the range of date of
your interest.


Hoping it may help,
Vanderghast, Access MVP


Phil McFarlane said:
Have the following fields in a table: Date,Name,JobNo,Hours.
I need a SQL query to count the number of days each Name appears over a
period of days.
Any Name can appear more than once on any Date.
If a Name appeared three times on the same Date it should only count as
one
day.

Any suggestions or help would be appreciated

Thanks
Phil_mac
 
Thanks for the quick reply. Unfortunately I left out an important fact. I am
using Access 97.

Cheers



Michel Walsh said:
SELECT name, COUNT(*)
FROM (SELECT DISTINCT name, [date]
FROM tableName
WHERE [date] BETWEEN firstDate AND lastDate) AS a
GROUP BY name

This query should work fine for any version of Access. Are you having a
problem with it?

John W. Vinson [MVP]
 
Initially this did not work, giving a bracketing error. However when name
and [date] where changed to tableName.name and tableName.date everything
worked OK.

Thanks to all for the input.

cheers


John W. Vinson said:
Thanks for the quick reply. Unfortunately I left out an important fact. I am
using Access 97.

Cheers



Michel Walsh said:
SELECT name, COUNT(*)
FROM (SELECT DISTINCT name, [date]
FROM tableName
WHERE [date] BETWEEN firstDate AND lastDate) AS a
GROUP BY name

This query should work fine for any version of Access. Are you having a
problem with it?

John W. Vinson [MVP]
 

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

Back
Top