从多个SQL查询生成结果

时间:2017-02-11 00:30:08

标签: sql sql-server

我有一个拥有发布商的数据库,每个月他们都会提交一份时间表。要查找本月提交报告的发布商,我会运行此查询以向我显示本月提交的所有发布商报告...

SELECT              c2.FullName AS ContactFullName, 
                    c2.LastName AS ContactLastName, 
                    c2.ContactId AS PublisherGUID, 
                    f2.jajw_groupname AS FSGName, 
                    b2.jajw_FieldServiceGroup AS FieldServiceGroupGUID, 
                    b2.jajw_reportId AS ReportGUID, 
                    b2.jajw_id AS ReportID,
                    b2.jajw_ReportMonthId AS ReportMonthGUID,
                    b2.jajw_ReportYearId AS ReportYearGUID,  
                    r2.jajw_name AS ReportMonthName, 
                    r2.jajw_CalendarDate AS ReportMonthCalDate, 
                    y2.jajw_name AS ReportYearName 

FROM                ContactBase AS c2 
LEFT JOIN           jajw_reportBase AS b2 ON c2.ContactId = b2.jajw_PublisherId 
LEFT JOIN           jajw_reportyearBase AS y2 ON b2.jajw_ReportYearId = y2.jajw_reportyearId 
LEFT JOIN           jajw_reportmonthBase AS r2 ON b2.jajw_ReportMonthId = r2.jajw_reportmonthId 
LEFT JOIN           jajw_fieldservicegroupBase AS f2 ON b2.jajw_FieldServiceGroup = f2.jajw_fieldservicegroupId
WHERE               c2.StateCode = '0' 
AND                 b2.jajw_ReportYearId = 'A507DFBF-B9CE-E611-A953-002248013EC3'
AND                 c2.jajw_CongregationAssignment != 640840001 
AND                 c2.jajw_CongregationAssignment != 640840005 
AND                 c2.jajw_CongregationAssignment != 640840006
AND                 c2.jajw_CongregationAssignment != 640840007
ORDER BY ContactLastName

下一步是获取尚未提交报告的发布商的子集,以便以下查询检索所有发布者,然后告诉我上述查询中没有哪些发布者:

WITH 
ALLPUBLISHERSUNFILTERED AS
(
SELECT              c2.FullName AS ContactFullName, 
                    c2.LastName AS ContactLastName, 
                    c2.ContactId AS PublisherGUID 
FROM                ContactBase AS c2 
WHERE               c2.StateCode = '0' 
AND                 c2.jajw_CongregationAssignment != 640840001 
AND                 c2.jajw_CongregationAssignment != 640840005 
AND                 c2.jajw_CongregationAssignment != 640840006
AND                 c2.jajw_CongregationAssignment != 640840007
),

REPORTERS AS
(
SELECT              c2.FullName AS ContactFullName, 
                    c2.LastName AS ContactLastName, 
                    c2.ContactId AS PublisherGUID, 
                    f2.jajw_groupname AS FSGName, 
                    b2.jajw_FieldServiceGroup AS FieldServiceGroupGUID, 
                    b2.jajw_reportId AS ReportGUID, 
                    b2.jajw_id AS ReportID,
                    b2.jajw_ReportMonthId AS ReportMonthGUID,
                    b2.jajw_ReportYearId AS ReportYearGUID,  
                    r2.jajw_name AS ReportMonthName, 
                    r2.jajw_CalendarDate AS ReportMonthCalDate, 
                    y2.jajw_name AS ReportYearName 

FROM                ContactBase AS c2 
LEFT JOIN           jajw_reportBase AS b2 ON c2.ContactId = b2.jajw_PublisherId 
LEFT JOIN           jajw_reportyearBase AS y2 ON b2.jajw_ReportYearId = y2.jajw_reportyearId 
LEFT JOIN           jajw_reportmonthBase AS r2 ON b2.jajw_ReportMonthId = r2.jajw_reportmonthId 
LEFT JOIN           jajw_fieldservicegroupBase AS f2 ON b2.jajw_FieldServiceGroup = f2.jajw_fieldservicegroupId
WHERE               c2.StateCode = '0' 
AND                 b2.jajw_ReportYearId = 'A507DFBF-B9CE-E611-A953-002248013EC3'
AND                 c2.jajw_CongregationAssignment != 640840001 
AND                 c2.jajw_CongregationAssignment != 640840005 
AND                 c2.jajw_CongregationAssignment != 640840006
AND                 c2.jajw_CongregationAssignment != 640840007
)

SELECT          a.ContactFullName, 
                a.ContactLastName, 
                a.PublisherGUID
       FROM     ALLPUBLISHERSUNFILTERED a
       WHERE    NOT EXISTS   (SELECT    *
                             FROM       REPORTERS rp
                             WHERE      a.PublisherGUID = rp.PublisherGUID)

最后一步是我正在努力的部分。我需要将两个结果集合在一起,以便得到一个包含组合结果的列表。

1 个答案:

答案 0 :(得分:0)

只需在没有适用数据的字段中运行填充UNION的{​​{1}}查询。并考虑为已提交/未提交的指标字段添加,例如 PublisherType

NULL