Now, I need to check which date/period has no data in A_log and P_log for specific machine, and try to find out the reason that cause no data.
USE DTNSThe comparing dataset result as follows:
GO
DECLARE @Machine_Type VARCHAR(30),
@Machine_Name VARCHAR(30),
@Start_Date DATETIME,
@End_Date DATETIME
SET @Machine_Type = 'D' --'D', 'T'
SET @Machine_Name = 'GGG_D1'
SET @Start_Date = '2012-09-01'
SET @End_Date = '2012-10-01'
DECLARE @Dates VARCHAR(10),
@Counts INT,
@A_Qtr VARCHAR(500),
@C_Qtr VARCHAR(500),
@A_Start_Date DATETIME,
@C_Start_Date DATETIME
DECLARE @Check_Date TABLE
(
A_Data_Counts INT,
[Date] VARCHAR(10),
C_Data_Counts INT
)
SET @A_Qtr = 'SELECT COUNT(*), CONVERT(VARCHAR(10), start_time, 120) '
IF @Machine_Type = 'D'
BEGIN
SET @A_Qtr = @A_Qtr + ' FROM ' + 'DTNS.dbo.A_log al'
SET @A_Qtr = @A_Qtr + ' JOIN DTNS.dbo.A_def ad ON ad.A_id = al.A_id '
SET @A_Qtr = @A_Qtr + ' WHERE I_id = DTNS.dbo.GetIId(''' + @Machine_Name + ''')'
END
IF @Machine_Type = 'T'
BEGIN
SET @A_Qtr = @A_Qtr + ' FROM ' + 'TNLReporting.dbo.A_log al'
SET @A_Qtr = @A_Qtr + ' JOIN TNLReporting.dbo.A_def ad ON ad.A_id = al.A_id '
SET @A_Qtr = @A_Qtr + ' WHERE I_id = TNLReporting.dbo.GetIId(''' + @Machine_Name + ''')'
END
SET @A_Qtr = @A_Qtr + ' AND start_time >= ''' + CONVERT(VARCHAR(10), @Start_Date, 120) + ''' AND start_time < ''' + CONVERT(VARCHAR(10), @End_Date, 120) + ''''
SET @A_Qtr = @A_Qtr + ' GROUP BY CONVERT(VARCHAR(10), start_time, 120)'
SET @A_Qtr = @A_Qtr + ' ORDER BY CONVERT(VARCHAR(10), start_time, 120)'
IF @Machine_Type = 'D'
BEGIN
SET @C_Qtr = 'SELECT COUNT(*), CONVERT(VARCHAR(10), event_date, 120) '
SET @C_Qtr = @C_Qtr + ' FROM DTNS.dbo.P_log '
SET @C_Qtr = @C_Qtr + ' WHERE I_id = DTNS.dbo.GetIId(''' + @Machine_Name + ''')'
SET @C_Qtr = @C_Qtr + ' AND event_date >= ''' + CONVERT(VARCHAR(10), @Start_Date, 120) + ''' AND event_date < ''' + CONVERT(VARCHAR(10), @End_Date, 120) + ''''
SET @C_Qtr = @C_Qtr + ' GROUP BY CONVERT(VARCHAR(10), event_date, 120)'
SET @C_Qtr = @C_Qtr + ' ORDER BY CONVERT(VARCHAR(10), event_date, 120)'
END
IF @Machine_Type = 'T'
BEGIN
SET @C_Qtr = 'SELECT COUNT(*), CONVERT(VARCHAR(10), start_time, 120) '
SET @C_Qtr = @C_Qtr + ' FROM TNLReporting.dbo.C_T '
SET @C_Qtr = @C_Qtr + ' WHERE I_id = TNLReporting.dbo.GetIId(''' + @Machine_Name + ''')'
SET @C_Qtr = @C_Qtr + ' AND start_time >= ''' + CONVERT(VARCHAR(10), @Start_Date, 120) + ''' AND start_time < ''' + CONVERT(VARCHAR(10), @End_Date, 120) + ''''
SET @C_Qtr = @C_Qtr + ' GROUP BY CONVERT(VARCHAR(10), start_time, 120)'
SET @C_Qtr = @C_Qtr + ' ORDER BY CONVERT(VARCHAR(10), start_time, 120)'
END
SET @Dates = ''
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
EXEC ( 'DECLARE A_Date_Check CURSOR FOR ' + @A_Qtr)
OPEN A_Date_Check
FETCH NEXT
FROM A_Date_Check
INTO @Counts, @Dates
SET @A_Start_Date = CONVERT(VARCHAR(10), @Start_Date, 120)
WHILE @@FETCH_STATUS = 0
BEGIN
--Capture the records which don't exist before the first day of the checking dataset
WHILE @Dates <> @A_Start_Date
BEGIN
INSERT INTO @Check_Date(A_Data_Counts, [Date])
VALUES(0, CONVERT(VARCHAR(10), @A_Start_Date, 120))
SET @A_Start_Date = CONVERT(VARCHAR(10), DATEADD(Day, 1, @A_Start_Date), 120)
END
IF @Dates = @A_Start_Date
BEGIN
INSERT INTO @Check_Date(A_Data_Counts, [Date])
VALUES(@Counts, @Dates)
SET @A_Start_Date = DATEADD(Day, 1, @A_Start_Date)
END
FETCH NEXT
FROM A_Date_Check
INTO @Counts, @Dates
END
CLOSE A_Date_Check
DEALLOCATE A_Date_Check
--SELECT * FROM @Check_Date
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--Capture the records which don't exist after the last day of the checking dataset
WHILE (@A_Start_Date < @End_Date)
BEGIN
INSERT INTO @Check_Date(A_Data_Counts, [Date])
VALUES(0, CONVERT(VARCHAR(10), @A_Start_Date, 120))
SET @A_Start_Date = DATEADD(Day, 1, @A_Start_Date)
END
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
EXEC ( 'DECLARE C_Date_Check CURSOR FOR ' + @C_Qtr)
OPEN C_Date_Check
FETCH NEXT
FROM C_Date_Check
INTO @Counts, @Dates
SET @C_Start_Date = CONVERT(VARCHAR(10), @Start_Date, 120)
WHILE @@FETCH_STATUS = 0
BEGIN
WHILE @Dates <> @C_Start_Date
BEGIN
UPDATE @Check_Date
SET C_Data_Counts = 0,
[Date] = CONVERT(VARCHAR(10), @C_Start_Date, 120)
WHERE [Date] = @C_Start_Date
SET @C_Start_Date = CONVERT(VARCHAR(10), DATEADD(Day, 1, @C_Start_Date), 120)
END
IF @Dates = @C_Start_Date
BEGIN
UPDATE @Check_Date
SET C_Data_Counts = @Counts,
[Date] = @Dates
WHERE [Date] = @C_Start_Date
SET @C_Start_Date = DATEADD(Day, 1, @C_Start_Date)
END
FETCH NEXT
FROM C_Date_Check
INTO @Counts, @Dates
END
CLOSE C_Date_Check
DEALLOCATE C_Date_Check
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SELECT A_Data_Counts [A_log_Data_Counts], [Date], ISNULL(C_Data_Counts, 0) [P_log_Data_Counts]
FROM @Check_Date
WHERE A_Data_Counts = 0 OR A_Data_Counts IS NULL
OR C_Data_Counts = 0 OR C_Data_Counts IS NULL
No comments:
Post a Comment