我的查询是我将数据分解为12个月,但列'Submitted - New Business'
来自另一个表。有没有机会它也可以按月分解?
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
正如你在照片上看到的那样,每个月都有相同的价值,有没有机会在不使用JOIN
的情况下以某种方式将其分解?
答案 0 :(得分:1)
尝试加入分组数据集
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