wpshop said:
I tried the Count(*) and it didn't work. Below is the full query I am
building. Perhaps I am trying to do too much with one query.
INSERT INTO tblMARAccuracyExternal120Day529 ( [Date], [Count] )
SELECT [Enter 120 Day Month Begin Date] AS [Date],
Count(tblExternalErrorReport.ACCT_000) AS [Count]
FROM tblExternalErrorReport INNER JOIN tblCorrectionResearchTeam ON
tblExternalErrorReport.REP_001 = tblCorrectionResearchTeam.[USER ID]
WHERE (((tblExternalErrorReport.EVENT_DATTIM) Between [Enter MAR Month Begin
Date] And [Enter MAR Month End Date]) AND ((tblExternalErrorReport.PCDT_001)
Between [Enter 120 Day Month Begin Date] And [Enter 120 Day End Date]) AND
((tblExternalErrorReport.DEPT_001)="iq") AND
((tblCorrectionResearchTeam.[CURRENT TEAM])="CORR") AND
((tblExternalErrorReport.FUND_000) Between 1400 And 1899))
GROUP BY [Enter 120 Day Month Begin Date];
wpshop,
Here is your query, straightened out.
INSERT INTO tblMARAccuracyExternal120Day529
([Date]
,[Count]
)
SELECT [Enter 120 Day Month Begin Date] AS [Date]
,Count(tblExternalErrorReport.ACCT_000) AS [Count]
FROM tblExternalErrorReport
INNER JOIN
tblCorrectionResearchTeam
ON tblExternalErrorReport.REP_001 =
tblCorrectionResearchTeam.[USER ID]
WHERE (((tblExternalErrorReport.EVENT_DATTIM)
BETWEEN [Enter MAR Month Begin Date]
AND [Enter MAR Month End Date])
AND ((tblExternalErrorReport.PCDT_001)
BETWEEN [Enter 120 Day Month Begin Date]
AND [Enter 120 Day End Date])
AND ((tblExternalErrorReport.DEPT_001) = "iq")
AND ((tblCorrectionResearchTeam.[CURRENT TEAM]) = "CORR")
AND ((tblExternalErrorReport.FUND_000)
BETWEEN 1400
AND 1899))
GROUP BY [Enter 120 Day Month Begin Date];
Well, there are no table structures or sample data, so I'll invent
some to use as an example.
Tables:
Create a blank MS Access database. You can copy and paste these DDL
SQL queries each into an MS Access Query, executing each one in order
to create the tables and the relationships.
CREATE TABLE Items
(ItemID INTEGER
,ItemName TEXT(255)
,CONSTRAINT pk_Items
PRIMARY KEY (ItemID)
)
CREATE TABLE Prices
(ItemID INTEGER NOT NULL
,StartDate DATETIME NOT NULL
,EndDate DATETIME
,Price CURRENCY
,CONSTRAINT pk_Prices
PRIMARY KEY (ItemID
,StartDate
,EndDate)
,CONSTRAINT fk_Prices
FOREIGN KEY (ItemID)
REFERENCES Items (ItemID)
)
Items
ItemID ItemName
1, Nails
2, Rivets
3, Washers
4, Lock Washers
5, Bolts
6, Long Nails
7, Short Nails
8, Tiny Nails
9, Ten Penny Nail
10, Fasteners
Prices
ItemID StartDate EndDate, Price
1, 01/01/2002, 12/31/2002, $5.00
2, 01/01/2002, 12/31/2002, $6.00
3, 01/01/2002, 12/31/2002, $1.00
4, 01/01/2002, 12/31/2002, $1.50
6, 01/01/2005, 12/31/2005, $0.50
7, 01/01/2005, 06/30/2005, $0.30
7, 02/02/2005, 03/04/2005, $0.45
8, 03/01/2005, 04/01/2005, $0.10
9, 01/01/2005, 11/30/2005, $0.20
Query:
You want COUNT() to report 0 when there is no data.
SELECT I1.ItemID
,COUNT(P1.ItemID) AS ItemCount
FROM Items AS I1
LEFT JOIN
Prices AS P1
ON I1.ItemID = P1.ItemID
GROUP BY I1.ItemID
Results:
ItemID ItemCount
1, 1
2, 1
3, 1
4, 1
5, 0
6, 1
7, 2
8, 1
9, 1
10, 0
As can be seen, zeroes are showing up even though the COUNT() was
operating on no rows in Prices for ItemID values of 5 and 10 (the LEFT
JOIN kept rows with NULLs around for non-matches).
What I think you need to do is switch INNER JOIN to RIGHT JOIN (if
not, try LEFT JOIN) in order to preserve rows in
tblExternalErrorReport where no matches were found so that COUNT() has
something to report as 0.
Sincerely,
Chris O.