2016-07-26 11 views
0

I meine Abfrage im brechen Daten um 12 Monate, aber Spalte 'Submitted - New Business' kommt aus einer anderen Tabelle. Gibt es eine Chance, dass es auch nach Monaten aufgeteilt werden kann?Aufschlüsselung nach Monat Sub-Select-Anweisung

SELECT  'Eldred, Rod' AS Underwriter,     
       SUM(CASE WHEN Underwriter = 'Eldred, Rod' THEN UWReportFee ELSE 0 END) as 'UW ReportFee', 
       (SUM(CASE WHEN Underwriter = 'Eldred, Rod' THEN Fees ELSE 0 END)) - SUM(CASE WHEN Underwriter = 'Eldred, Rod' THEN UWReportFee ELSE 0 END) as 'Taxes & Surcharges', 
       COUNT(CASE WHEN Underwriter = 'Eldred, Rod' AND PolicyType = 'New Business' THEN QuoteControlNum END) as  'Submitted - New Business', 
     /*SubQuery*/ 
       (
       SELECT COUNT(CASE WHEN Underwriter = 'Eldred, Rod' AND Type = 'New Business' THEN ControlNo END) 
       FROM  tblCalendar b 
       LEFT JOIN ClearanceReportMetrics a ON b.MonthNum=Month(a.EffectiveDate)  
       AND b.YearNum = YEAR(a.EffectiveDate) AND CompanyLine = 'Arch Insurance Company' AND YEAR(EffectiveDate)=2016 
       ) as 'Submitted - New Business' , 
     /* End of SubQuery*/ 

       b.MonthNum, 
       b.YearNum 
    FROM  tblCalendar b 
    LEFT JOIN ProductionReportMetrics a ON b.MonthNum=Month(a.EffectiveDate) 
       AND b.YearNum = YEAR(a.EffectiveDate) AND CompanyLine = 'Arch Insurance Company' AND YEAR(EffectiveDate)=2016 
       --AND a.Underwriter ='Eldred, Rod' 
    WHERE  b.YearNum = 2016 
    GROUP BY --a.Underwriter, 
       b.MonthName, 
       b.MonthNum, 
       b.YearNum  

enter image description here

, wie Sie auf einem Bild sehen können, jeden Monat den gleichen Wert hat, ist eine Chance, es zu brechen irgendwie ohne JOIN usin nach unten?

+0

eine korrelierte Unterabfrage verwenden b.month von Ihrem Außen zu Ihrem Monat (a.effectivedate) beitreten? aber Sie müssten die innere Abfrage B-Tabelle anders als ich glaube Alias. – xQbert

+0

Danke. Aber ist dir das ein gutes Beispiel? – Oleg

+0

Sie müssen 2 Grpuped-Abfragen beitreten oder nach dem Gruppieren einen Kreuz anwenden. Was ist los mit Join? – Serg

Antwort

1

Versuchen Verbindungs ​​gruppierten Daten

setzt
declare @uw varchar(50) ='Eldred, Rod'; 
    declare @year = 2016; 
    declare @cn varchar(50) ='Arch Insurance Company'; 

    SELECT  @uw,     
       t1.'UW ReportFee', 
       t1.'Taxes & Surcharges', 
       t1.'Submitted - New Business', 
       t2.cnt as 'Submitted - New Business count' , 
       b.MonthNum, 
       b.YearNum 
    FROM  tblCalendar b 
    LEFT JOIN ( 
       SELECT 
       Month(EffectiveDate) as Month, 
       SUM(UWReportFee) as 'UW ReportFee', 
       (SUM(Fees) - SUM(UWReportFee) as 'Taxes & Surcharges', 
       COUNT(CASE WHEN PolicyType = 'New Business' THEN QuoteControlNum END) as 'Submitted - New Business', 
       FROM ProductionReportMetrics 
       GROUP BY Month(EffectiveDate), 
       WHERE YEAR(a.EffectiveDate) = @year AND CompanyLine = @cn AND Underwriter = @uw 
       ) t1 ON t1.Month = b.MonthNum 
    LEFT JOIN ( 
       SELECT 
       Month(EffectiveDate) as Month, 
       COUNT(ControlNo) as cnt 
       FROM ClearanceReportMetrics 
       GROUP BY Month(EffectiveDate), 
       WHERE YEAR(a.EffectiveDate) = @year AND CompanyLine = @cn AND Underwriter = @uw AND Type = 'New Business' 
       ) t2 ON t2.Month = b.MonthNum 
    WHERE  b.YearNum = 2016 
+0

ERSTAUNLICH !!! Danke Serg, es funktioniert super !! – Oleg