I have a table with the fields: resourceID,work_date,stringValue
I’m trying to build an Access query that will show a count of how many different resourceID numbers with a given stringValue occur in each week over a given date range. Using partition() seems to be the simplest approach, however, when I use the following query:
select partition([work_date],#6/6/2011#,#9/4/2011#,7),stringValue,
count(resourceID) from
(select distinct resourceID,work_date,stringValue from myTable) as subQuery
group by partition([work_date],#6/6/2011#,#9/4/2011#,7), stringValue
then I have two problems:
-My dates end up formatted as integers, e.g.:
:40699
40700:40706
40784:40790
whereas I want them to appear as, e.g., 6/6/2011:6/12/2011 (I also don’t want the :40699 value)
–resourceID gets counted more than once per week if it appears on more than one weekday; I just want it to be counted once for each stringValue if it appears at all that week. I thought the distinct qualifier would accomplish this but it didn’t.
EDIT: I’ve solved the excess resourceID count by putting the partition in a subquery as follows:
select datePartition,stringValue,count(ID)
from (select partition() as datePartition, stringValue, ID
from (select distinct stringvalue,ID,work_date))
group by datePartition,stringvalue
and then pulling count(ID) from that subquery. Still can’t figure out the date formatting, though.
To get rid of the range which ends with 40699, try revising the subquery piece. Add a WHERE condition to limit rows to work_date values >= #6/6/2011#
I’m not clear about your description regarding the duplicate rows from the SELECT DISTINCT. One possibility could be that at least some of your work_date values contain different time values from the same date. So two rows with the same resourceID and stringValue, but work_date values of #6/6/2011 01:00 AM# and #6/6/2011 02:00 AM#, would legitimately qualify as distinct.
If that’s not the explanation, clarify your question by showing us a smallish set of data from myTable, and the resultset of that data which illustrates how you want the data evaluated.
AFAICT, the issue about Partition() presenting your date ranges as whole numbers is unavoidable. According to the help topic, Partition() expects whole numbers for its parameters. Apparently it’s willing to accept your Date/Time values by casting them to whole numbers. But it’s not willing/able to transform them back to date strings. You would have to transform them back yourself. A user-defined function could do it when called from a query.
An example from the Immediate Window which uses that function …