AOB
Registered User.
- Local time
- Today, 13:54
- Joined
- Sep 26, 2012
- Messages
- 633
Hi guys,
I'm trying to perform a rather complicated query but struggling with it. Hoping somebody can fill in the gaps for me...
Some essential background first. I have a Balances table which records balances by date. I also have an Issues table where problems are logged. There is a one-to-many relationship between Balances and Issues (i.e. each Balance can have multiple Issues). I also have a Comments table where updates for each Issue are recorded. There is again a one-to-many relationship between Issues and Comments (i.e. each Issue can have multiple Comments)
There are two key date fields in the Issues table :
I need to incorporate a trend graph which will show the counts of
The subqueries for 'New', 'Cleared' and 'Outstanding' work perfectly; the resultant dataset gives me one record for each date in the Balance table and correctly counts the number of issues falling into each of those buckets.
The problem I have is with the 'Updated' bucket. If a flagged issue happens to be updated twice on the same day (which is perfectly acceptable), it counts this twice as well. I don't want this as I just want to know how many issues were updated on any given day - not how many updates there were.
I tried using COUNT(DISTINCT) in the 'Updated' subquery but it gives me a syntax error - on further research, I don't think it's possible to use the DISTINCT keyword in a COUNT subquery (at least not easily)
I also tried grouping by IssueID within that 'Updated' subquery but it still gives me the duplicate count within the same IssueID (and returns nulls rather than zeroes for those days where no updates occured)
I think I need to add a subquery within the subquery
confused
to only return the latest comment as of the date in question - something along the lines of :
But haven't the faintest idea how to do this, nor if it is even feasible in Access to begin with.
Would really appreciate if anyone could assist me as have been struggling with this for days :banghead:
Thanks in advance!
AOB
I'm trying to perform a rather complicated query but struggling with it. Hoping somebody can fill in the gaps for me...
Some essential background first. I have a Balances table which records balances by date. I also have an Issues table where problems are logged. There is a one-to-many relationship between Balances and Issues (i.e. each Balance can have multiple Issues). I also have a Comments table where updates for each Issue are recorded. There is again a one-to-many relationship between Issues and Comments (i.e. each Issue can have multiple Comments)
There are two key date fields in the Issues table :
- FlagDate (the date an Issue was flagged by a user for investigation)
- ResolveDate (the date said investigation was brought to a conclusion)
- UpdatedWhen (the date any given comment was added)
I need to incorporate a trend graph which will show the counts of
- New (i.e. new issues flagged as of each day)
- Cleared (i.e. issues resolved each day)
- Updated (i.e. issues not yet resolved but updated each day)
- Unchanged (i.e. issues not yet resolved and not updated each day)
- Outstanding (i.e. all unresolved issues as of each day)
Code:
SELECT [tblBalances].[BalanceDate] AS AsOfDate,
(SELECT COUNT([tblIssues].[IssueID])
FROM [tblIssues]
WHERE [tblIssues].[Flag] = True
AND [tblIssues].[FlagDate] = [tblBalances].[BalanceDate]) AS New,
(SELECT COUNT([tblIssues].[IssueID])
FROM [tblIssues]
WHERE [tblIssues].[Flag] = True
AND [tblIssues].[ResolveDate] = [tblBalances].[BalanceDate]) AS Cleared,
(SELECT COUNT([tblIssues].[IssueID])
FROM [tblIssues]
INNER JOIN [tblComments] ON [tblIssues].[IssueID] = [tblComments].[IssueID]
WHERE [tblIssues].[Flag] = True
AND ([tblIssues].[FlagDate] < [tblBalances].[BalanceDate])
AND (DateDiff("d",DateValue(Nz([tblComments].[UpdatedWhen],DateSerial(1900,1,1))), [tblBalances].[BalanceDate]) = 0)
AND (Nz([tblIssues].[ResolveDate],DateSerial(9999,12,31)) > [tblBalances].[BalanceDate])) AS Updated,
(SELECT COUNT([tblIssues].[IssueID])
FROM [tblIssues]
WHERE [tblIssues].[Flag] = True
AND [tblIssues].[FlagDate] <= [tblBalances].[BalanceDate]
AND Nz([tblIssues].[ResolveDate],DateSerial(9999,12,31)) > [tblBalances].[BalanceDate]) AS Outstanding,
(Outstanding - New - Updated) AS Unchanged
FROM [tblBalances]
GROUP BY [tblBalances].[BalanceDate]
ORDER BY [tblBalances].[BalanceDate];
The subqueries for 'New', 'Cleared' and 'Outstanding' work perfectly; the resultant dataset gives me one record for each date in the Balance table and correctly counts the number of issues falling into each of those buckets.
The problem I have is with the 'Updated' bucket. If a flagged issue happens to be updated twice on the same day (which is perfectly acceptable), it counts this twice as well. I don't want this as I just want to know how many issues were updated on any given day - not how many updates there were.
I tried using COUNT(DISTINCT) in the 'Updated' subquery but it gives me a syntax error - on further research, I don't think it's possible to use the DISTINCT keyword in a COUNT subquery (at least not easily)
I also tried grouping by IssueID within that 'Updated' subquery but it still gives me the duplicate count within the same IssueID (and returns nulls rather than zeroes for those days where no updates occured)
I think I need to add a subquery within the subquery


Code:
(SELECT TOP 1 [tblComments].[UpdatedWhen]
FROM [tblComments]
WHERE [tblComments].[IssueID] = [tblIssues].[IssueID]
AND DateValue([tblComments].[UpdatedWhen]) <= [tblBalances].[BalanceDate]
ORDER BY [tblComments].[UpdatedWhen] DESC) AS UpdatedWhen
But haven't the faintest idea how to do this, nor if it is even feasible in Access to begin with.
Would really appreciate if anyone could assist me as have been struggling with this for days :banghead:
Thanks in advance!
AOB