在第一个查询中,我使用LEFT JOIN
返回月份,即使其中没有数据。但似乎它不起作用。
所以在第二个查询中,我决定使用RIGHT JOIN tblCalendar
来返回没有数据的月份。但为什么每个月会有不同的结果?
;with cte_TopClasses AS
(
select
b.YearNum,
b.MonthNum,
REPLACE(ClassCode,'+','') + ' - '+ QLL.Description as Description,
SUM( Premium) as NetWrittenPremium
from
tblCalendar b
left join
ProductionReportMetrics prm on b.MonthNum = Month(prm.EffectiveDate)
and b.YearNum = YEAR(EffectiveDate)
and prm.EffectiveDate >= DateAdd(yy, -1, DATEADD(d, 1, EOMONTH(GETDATE())))
and prm.EffectiveDate <= EOMONTH(GETDATE())
and CompanyLine = 'Ironshore Insurance Company'
and LineOfCoverage NOT IN ('SSP Commercial General Liability', 'SSP Property')
left join
NetRate_Quote_Insur_Quote Q on prm.NetRate_QuoteID = Q.QuoteID
left join
NetRate_Quote_Insur_Quote_Locat QL ON Q.QuoteID = QL.QuoteID
LEFT JOIN
(SELECT *
FROM NetRate_Quote_Insur_Quote_Locat_Liabi nqI
JOIN
(SELECT LocationID as LocID, MAX(ClassCode) as ClCode
FROM NetRate_Quote_Insur_Quote_Locat_Liabi
GROUP BY LocationID) nqA ON nqA.LocID = nqI.LocationID AND nqA.ClCode = nqI.ClassCode ) QLL ON QLL.LocationID = QL.LocationID
where
(b.YearNum = YEAR(GETDATE()) - 1 and b.MonthNum >= MONTH(GETDATE()) + 1)
or (b.YearNum = YEAR(GETDATE()) and b.MonthNum <= MONTH(GETDATE()))
group by
b.YearNum, b.MonthNum, ClassCode, QLL.Description
)
/*And this is that crazy final select statement from Richards (Stackoverflow) */
SELECT *
FROM (
SELECT RANK() OVER (ORDER BY [Description] DESC) AS Rank, *
FROM cte_TopClasses
WHERE Description IN (
SELECT [Description]
FROM (
SELECT RANK() OVER (ORDER BY SUM(NetWrittenPremium) DESC) AS [Rank], [Description], SUM(NetWrittenPremium) AS total
FROM cte_TopClasses
WHERE [Description] IS NOT NULL
GROUP BY [Description]
) BB
WHERE [Rank] <= 10
)
) AA
ORDER BY YearNum, MonthNum
在下一个查询中,我在最后tblCalendar
语句中使用SELECT
的第一个RIGHT JOIN tblCalendar
语句中所做的全部内容基本上不是SELECT
在我将其转储到SSRS并按月分解后,由于某种原因它给出了不同的结果
;with cte_TopClasses
AS (
select
YEAR(prm.EffectiveDate) as EffectiveYear, --adding Effective because of SSRS repeating column and it gives me a shit
MONTH(prm.EffectiveDate) as EffectiveMonthNum, --adding Effective because of SSRS repeating column and it gives me a shit
REPLACE(ClassCode,'+','') + ' - '+ QLL.Description as Description,
SUM( Premium) as NetWrittenPremium
--FROM tblCalendar b
FROM ProductionReportMetrics prm
--LEFT JOIN ProductionReportMetrics prm ON b.MonthNum=Month(prm.EffectiveDate) AND b.YearNum = YEAR(EffectiveDate)
-- AND prm.EffectiveDate >=DateAdd(yy, -1, DATEADD(d, 1, EOMONTH(GETDATE()))) AND prm.EffectiveDate <= EOMONTH(GETDATE()) AND CompanyLine = 'Ironshore Insurance Company'
-- AND LineOfCoverage NOT IN ('SSP Commercial General Liability','SSP Property')
LEFT JOIN NetRate_Quote_Insur_Quote Q ON prm.NetRate_QuoteID = Q.QuoteID
AND LineOfCoverage NOT IN ('SSP Commercial General Liability','SSP Property')
LEFT JOIN NetRate_Quote_Insur_Quote_Locat QL ON Q.QuoteID = QL.QuoteID
LEFT JOIN (SELECT * FROM NetRate_Quote_Insur_Quote_Locat_Liabi nqI
JOIN ( SELECT LocationID as LocID, MAX(ClassCode) as ClCode
FROM NetRate_Quote_Insur_Quote_Locat_Liabi GROUP BY LocationID ) nqA
ON nqA.LocID = nqI.LocationID AND nqA.ClCode = nqI.ClassCode ) QLL
ON QLL.LocationID = QL.LocationID
--WHERE ( b.YearNum = YEAR(GETDATE())-1 and b.MonthNum >= MONTH(GETDATE())+1 ) OR
-- ( b.YearNum = YEAR(GETDATE()) and b.MonthNum <= MONTH(GETDATE()) )
GROUP BY YEAR(prm.EffectiveDate) ,
MONTH(prm.EffectiveDate),
ClassCode,
QLL.Description
)
/*And this is that crazy final select statement from Richards (Stackoverflow) */
SELECT Rank, b.YearNum,b.MonthNum, Description,NetWrittenPremium
FROM (
SELECT RANK() OVER (ORDER BY [Description] DESC) AS Rank, *
FROM cte_TopClasses
WHERE Description IN (
SELECT [Description]
FROM (
SELECT RANK() OVER (ORDER BY SUM(NetWrittenPremium) DESC) AS [Rank], [Description], SUM(NetWrittenPremium) AS total
FROM cte_TopClasses
WHERE [Description] IS NOT NULL
GROUP BY [Description]
) BB
WHERE [Rank] <= 10
)
) AA RIGHT JOIN tblCalendar b ON b.YearNum = AA.EffectiveYear AND b.MonthNum=AA.EffectiveMonthNum
WHERE ( b.YearNum = YEAR(GETDATE())-1 and b.MonthNum >= MONTH(GETDATE())+1 ) OR
( b.YearNum = YEAR(GETDATE()) and b.MonthNum <= MONTH(GETDATE()) )
ORDER BY YearNum, MonthNum
一切都是一样的,直到部分以红色圈出。为什么会这样?
尝试#1
;with cte_TopClasses
AS (
select
YEAR(prm.EffectiveDate) as EffectiveYear,
MONTH(prm.EffectiveDate) as EffectiveMonthNum,
REPLACE(ClassCode,'+','') + ' - '+ QLL.Description as Description,
SUM( Premium) as NetWrittenPremium
FROM ProductionReportMetrics prm
LEFT JOIN NetRate_Quote_Insur_Quote Q ON prm.NetRate_QuoteID = Q.QuoteID
AND LineOfCoverage NOT IN ('SSP Commercial General Liability','SSP Property')
LEFT JOIN NetRate_Quote_Insur_Quote_Locat QL ON Q.QuoteID = QL.QuoteID
LEFT JOIN (SELECT * FROM NetRate_Quote_Insur_Quote_Locat_Liabi nqI
JOIN ( SELECT LocationID as LocID, MAX(ClassCode) as ClCode
FROM NetRate_Quote_Insur_Quote_Locat_Liabi GROUP BY LocationID ) nqA
ON nqA.LocID = nqI.LocationID AND nqA.ClCode = nqI.ClassCode ) QLL
ON QLL.LocationID = QL.LocationID
GROUP BY YEAR(prm.EffectiveDate) ,
MONTH(prm.EffectiveDate),
ClassCode,
QLL.Description
)
SELECT TOP 10 Description, SUM(NetWrittenPremium) AS NetWrittenPremium, EffectiveYear,EffectiveMonthNum
FROM cte_TopClasses RIGHT JOIN tblCalendar b ON b.YearNum = EffectiveYear AND b.MonthNum=EffectiveMonthNum
WHERE ( b.YearNum = YEAR(GETDATE())-1 and b.MonthNum >= MONTH(GETDATE())+1 ) OR
( b.YearNum = YEAR(GETDATE()) and b.MonthNum <= MONTH(GETDATE()) )
GROUP BY [Description],EffectiveYear,EffectiveMonthNum
ORDER BY SUM(NetWrittenPremium) DESC
答案 0 :(得分:0)
sql server中sum的前10个描述。
1)得到描述和总结:
SELECT [Description], SUM(NetWrittenPremium) AS total
FROM ct_TopClasses
GROUP BY [Description]
2)进入前10名
SELECT TOP 10 [Description], SUM(NetWrittenPremium) AS total
FROM ct_TopClasses
GROUP BY [Description]
ORDER BY SUM(NetWrittenPremium) DESC
这有点简单,是吗?