Hi,
I've written the following SQL Statement to locate missing data in one of my tables.
The statement creates a temp table called '#Dates' which is then populated with each date from the start of the year to 'GETDATE() - 1'
This all works perfectly, however i then want to execute a query that will show me all of the dates that a particular '[Subscriber Code]' is missing however when i execute the code below, i get every date returned, not just the ones that are in 'tempdb.#Dates' and not in 'RxWorks.Result'.
Can anyone spot where i'm going wrong?
Thanks in advance,
Leigh Moore
Business Systems Manager
Vets4Pets Veterinary Group
I've written the following SQL Statement to locate missing data in one of my tables.
The statement creates a temp table called '#Dates' which is then populated with each date from the start of the year to 'GETDATE() - 1'
Code:
CREATE TABLE #Dates
([Date] smalldatetime)
GO
INSERT #Dates ([Date])
VALUES ('2004-01-01')
GO
BEGIN TRAN
DECLARE @DateValue smalldatetime
SET @DateValue = (SELECT MAX([Date]) FROM #Dates) + 1
WHILE @DateValue < GETDATE()
BEGIN
INSERT #Dates ([Date])
VALUES (@DateValue)
SET @DateValue = (SELECT MAX([Date]) FROM #Dates) + 1
END
COMMIT TRAN
This all works perfectly, however i then want to execute a query that will show me all of the dates that a particular '[Subscriber Code]' is missing however when i execute the code below, i get every date returned, not just the ones that are in 'tempdb.#Dates' and not in 'RxWorks.Result'.
Code:
SELECT R.[Subscriber Code], D.[Date]
FROM RxWorks.RxWorks.Result AS R
RIGHT OUTER JOIN tempdb.#Dates AS D
ON D.[Date] <> R.[Date]
INNER JOIN RxWorks..[List - Practices] AS P
ON R.[Date] >= P.[OpenDate]
WHERE R.[Period Code] = 1 AND
R.[Sub Group Code 1] = 99 AND
R.[Result Code] IN (1, 10) AND
R.[Date] BETWEEN '2004-01-01' AND GETDATE()
GROUP BY R.[Subscriber Code], D.[Date]
ORDER BY R.[Subscriber Code], D.[Date]
Can anyone spot where i'm going wrong?
Thanks in advance,
Leigh Moore
Business Systems Manager
Vets4Pets Veterinary Group