0

我有这两个查询,我想要一组结果,组合 TP 的前 5 个结果和 LP 的前 5 个结果。我真的不知道如何更清楚地解释这一点,我有两组结果,LP 前 5 名和 TP 前 5 名,我想要一组结果 Incident_TP, IncidentID_TP, IncidentHappenedDate_TP , IncidentNumber_TP , LossValue_TP , RecoveredValue_TP , TotalLoss_TP , Incident_LP, IncidentID_LP, IncidentHappenedDate_LP , IncidentNumber_LP , LossValue_LP , RecoveredValue_LP , TotalLoss_LP

  DECLARE @IncidentFromDate_TP DATE = '2011-1-12'
  DECLARE @IncidentToDate_TP DATE =  '2012-1-12'
  DECLARE @IncidentFromDate_LP DATE = '2010-1-12'
  DECLARE @IncidentToDate_LP DATE =  '2011-1-12'

SELECT TOP 5
    Incident_TP = Incident_TP.IncidentID
    , IncidentHappenedDate_TP = Incident_TP.IncidentHappenedDate
    , IncidentNumber_TP = Incident_TP.IncidentNumber
    , LossValue_TP = Incident_TP.TotalLoss
    , RecoveredValue_TP = Incident_TP.TotalRecovered
    , TotalLoss_TP = Incident_TP.CostOfIncident
FROM 
    Incident AS Incident_TP
    INNER JOIN Site AS Site_TP ON Incident_TP.SiteID = Site_TP.SiteID
    INNER JOIN Region AS Region_TP ON Site_TP.RegionID = Region_TP.RegionID

WHERE
    Incident_TP.TotalLoss > 0.00
    AND Incident_TP.IncidentHappenedDate BETWEEN @IncidentFromDate_TP AND  @IncidentToDate_TP

ORDER BY
    TotalLoss_TP DESC
    , IncidentHappenedDate_TP DESC



SELECT TOP 5
     Incident_LP = Incident_LP.IncidentID
    , IncidentHappenedDate_LP = Incident_LP.IncidentHappenedDate
    , IncidentNumber_LP = Incident_LP.IncidentNumber
    , LossValue_LP = Incident_LP.TotalLoss
    , RecoveredValue_LP = Incident_LP.TotalRecovered
    , TotalLoss_LP = Incident_LP.CostOfIncident

FROM 
    Incident AS Incident_LP
    INNER JOIN Site ON Incident_LP.SiteID = Site.SiteID
    INNER JOIN Region ON Site.RegionID = Region.RegionID

WHERE
    Incident_LP.TotalLoss > 0.00
    AND Incident_LP.IncidentHappenedDate BETWEEN @IncidentFromDate_LP AND @IncidentToDate_TP

ORDER BY
    TotalLoss_LP DESC
    , IncidentHappenedDate_LP DESC

非常感谢

4

1 回答 1

0

正如蒂姆建议的那样,您可以将这些集合合并在一起,例如:

  DECLARE @IncidentFromDate_TP DATE = '2011-1-12';
  DECLARE @IncidentToDate_TP DATE = '2012-1-12';
  DECLARE @IncidentFromDate_LP DATE = '2010-1-12';
  DECLARE @IncidentToDate_LP DATE = '2011-1-12';

  WITH  RawData
          AS ( SELECT   Incident_TP = Incident_TP.IncidentID ,
                        IncidentHappenedDate_TP = Incident_TP.IncidentHappenedDate ,
                        IncidentNumber_TP = Incident_TP.IncidentNumber ,
                        LossValue_TP = Incident_TP.TotalLoss ,
                        RecoveredValue_TP = Incident_TP.TotalRecovered ,
                        TotalLoss_TP = Incident_TP.CostOfIncident
               FROM     Incident AS Incident_TP
                        INNER JOIN Site AS Site_TP ON Incident_TP.SiteID = Site_TP.SiteID
                        INNER JOIN Region AS Region_TP ON Site_TP.RegionID = Region_TP.RegionID
               WHERE    Incident_TP.TotalLoss > 0.00
             ),
        TopFiveSetA
          AS ( SELECT TOP 5
                        *
               FROM     RawData
               WHERE    IncidentHappenedDate_TP BETWEEN @IncidentFromDate_TP
                                                AND     @IncidentToDate_TP
               ORDER BY TotalLoss_TP DESC ,
                        IncidentHappenedDate_TP DESC
             ),
        TopFiveSetB
          AS ( SELECT TOP 5
                        *
               FROM     RawData
               WHERE    IncidentHappenedDate_TP BETWEEN @IncidentFromDate_LP
                                                AND     @IncidentToDate_LP
               ORDER BY TotalLoss_TP DESC ,
                        IncidentHappenedDate_TP DESC
             ),
        Merged
          AS ( SELECT   *
               FROM     TopFiveSetA
               UNION ALL
               SELECT   *
               FROM     TopFiveSetB
             )
    SELECT  *
    FROM    Merged
于 2012-06-07T13:06:36.953 回答