Tengo dos tablas. Una es una tabla de los informes que se nos han enviado. Otra es una tabla temporal con los registros de informes que eventualmente se nos deben enviar. Me gustaría mostrar solo los registros en la tabla temporal que no coinciden con los de la tabla de informes (por lo que se muestran los informes que aún deben enviarse).Consulta que muestra registros que NO coinciden entre las tablas
Ejemplo de datos es:
Reports table:
CREATE TABLE [dbo].[Reports]
(
[ReportID] [int] IDENTITY(1,1) NOT NULL,
[ReportDate] [date] NULL,
[AssessmentID] [int] NOT NULL,
[ReportType] [varchar](50) NULL
);
AssessmentID ReportType ReportID
1 1st Quarterly 27
2 1st Quarterly 30
2 2nd Quarterly 31
2 3rd Quarterly 32
QuarterlyReportsDue table:
CREATE TABLE #QuarterlyReportsDue
(
AssessmentID INT,
InstallationDate DATE,
QuarterlyReportType VARCHAR(50)
);
AssessmentID InstallationDate QuarterlyReportType
1 2009-08-14 1st Quarterly
1 2009-08-14 2nd Quarterly
1 2009-08-14 3rd Quarterly
1 2009-08-14 4th Quarterly
2 2008-05-16 4th Quarterly
2 2008-05-16 3rd Quarterly
2 2008-05-16 2nd Quarterly
2 2008-05-16 1st Quarterly
He tratado IZQUIERDA combinaciones externas, pero estoy funcionando en problemas. Por favor, vea mi continuación de SQL:
SELECT #QuarterlyReportsDue.InstallationDate, #QuarterlyReportsDue.QuarterlyReportType, Reports.ReportType
FROM #QuarterlyReportsDue
LEFT OUTER JOIN Reports ON #QuarterlyReportsDue.AssessmentID = Reports.AssessmentID
WHERE Reports.ReportType IN ('1st Quarterly', '2nd Quarterly', '3rd Quarterly', '4th Quarterly')
AND Reports.ReportType <> #QuarterlyReportsDue.QuarterlyReportType
ORDER BY #QuarterlyReportsDue.AssessmentID
Y mis resultados:
AssessmentID QuarterlyReportType ReportType ReportID
1 2nd Quarterly 1st Quarterly 27
1 3rd Quarterly 1st Quarterly 27
1 4th Quarterly 1st Quarterly 27
2 4th Quarterly 1st Quarterly 30
2 4th Quarterly 2nd Quarterly 31
2 4th Quarterly 3rd Quarterly 32
2 1st Quarterly 2nd Quarterly 31
2 1st Quarterly 3rd Quarterly 32
2 3rd Quarterly 1st Quarterly 30
2 3rd Quarterly 2nd Quarterly 31
2 2nd Quarterly 1st Quarterly 30
2 2nd Quarterly 3rd Quarterly 32
Para la evaluación 1 funciona muy bien, la evaluación 2 tiene muchos duplicados. ¿Cómo puedo evitar esto para mostrar solo los resultados ideales?
AssessmentID QuarterlyReportType ReportType
1 2nd Quarterly 1st Quarterly
1 3rd Quarterly 1st Quarterly
1 4th Quarterly 1st Quarterly
2 4th Quarterly
¡Funciona muy bien también! Gracias por la explicación. –