我们公司希望将报告的财政年度计算从3/1 / 17-18更改为1/1 / 17-18,这与日历年一致。
我很可能会将此提交给最初设计查询的顾问,以便将数据从另一个数据库中的表转移到表中并在报告数据库中查看,但我希望能够更好地理解查询(见附件)。
我是否需要在所有日期相关报表上或仅在会计年度报表上将月份更改为1?
非常感谢任何建议和意见
非常感谢你。
USE [ABC_Cube]
GO
/****** Object: View [dbo].[ABC_Fact] Script Date: 04/06/2017
11:28:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER view [dbo].[ABC_Fact] as
-- Start Binder Data
Select
'Binder Record' as RowType
,CASE when b.NewOrRenew ='N' THEN 'New Business' ELSE 'Renewal' end as
RowSubType
,CASE when b.Underwriteruserid IS NULL THEN 'N/A' ELSE
b.Underwriteruserid END AS userID
,CASE when u.username IS NULL THEN 'N/A' ELSE u.username END AS username
,CASE when B.ACCOUNTID IS NULL THEN 'N/A' ELSE B.ACCOUNTID END AS
ACCOUNTID
,CASE when a.account IS NULL THEN 'N/A' ELSE a.account END as Agency
,CASE when ad.CITY IS NULL THEN 'N/A' ELSE ad.CITY END as CITY
,CASE when ad.state IS NULL THEN 'N/A' ELSE ad.state END as State
,CASE when ad.POSTALCODE IS NULL THEN 'N/A' ELSE ad.POSTALCODE END as ZIP
,CASE when ad.COUNTRY IS NULL THEN 'US' ELSE ad.state END as COUNTRY
,CASE when c.ContactID IS NULL THEN 'N/A' ELSE c.ContactID END as
contactID
,CASE when c.firstname+' '+c.lastname IS NULL THEN 'N/A' ELSE
c.firstname+' '+c.lastname END as Agent
,b.HouseNumber as House
,B.DEPTARTMENTCODE
,co.companycode
,coalesce(co.companyname1,'')+' '+coalesce(co.companyname2,'')+'
('+co.companycode+')' as CompanyName
,b.SubmissionNumber
,b.Underwriter
,b.Insured1
,b.PolicyTypeCode
,b.Submissioncreatedate as SubmitDate
,month(b.Submissioncreatedate) as Submit_Month
,year(b.Submissioncreatedate) as Submit_Year
,b.FirstQuoteDate
,month(b.FirstQuoteDate) as FirstQuote_Month
,year(b.FirstQuoteDate) as FirstQuote_Year
,b.BoundDate
,month(b.BoundDate) as Bound_Month
,year(b.BoundDate) as Bound_Year
,b.PolicyEffectiveDate
,month(b.PolicyEffectiveDate) as Effective_Month
,year(b.PolicyEffectiveDate) as Effective_Year
,case when (month(b.PolicyEffectiveDate)<=3) then
year(b.PolicyEffectiveDate)-1 else year(b.PolicyEffectiveDate) end as
Effective_FiscalYear
,b.PolicyExpirationDate
,month(b.PolicyExpirationDate) as Expiration_Month
,year(b.PolicyExpirationDate) as Expiration_Year
,case when (month(b.PolicyExpirationDate)<=3) then
year(b.PolicyExpirationDate)-1 else year(b.PolicyExpirationDate) end as
Expiration_FiscalYear
,CASE when b.LOBCode IS NULL THEN 'N/A' ELSE b.LOBCode end as LOBCode
,CASE when b.LOBName IS NULL THEN 'N/A' ELSE b.LOBName end as LOBName
,CASE when B.LOBClass IS NULL THEN 'N/A' ELSE B.LOBClass end as LOBClass
,CASE when b.IndustryDOBCode IS NULL THEN 'N/A' ELSE b.IndustryDOBCode
end AS DOBCode
,CASE when b.IndustryDOBName IS NULL THEN 'N/A' ELSE b.IndustryDOBName
end AS DOBName
,CASE when B.IndustryDOBClass IS NULL THEN 'N/A' ELSE B.IndustryDOBClass
end as DOBClass
,case
when SubmissionStatusCode='SUB' then 'Submitted'
when SubmissionStatusCode='QUO' then 'Quoted'
when SubmissionStatusCode='BIN' then 'Bound'
when SUBSTRING(SubmissionStatusCode,1,1)='T' then 'Termintated'
else 'Other'
end SubmissionStatus
,SubmissionStatusCode
,case when (FIRSTQUOTEDATE is not null or submissionstatuscode = 'BIN')
then 'Y' else 'N' end Quoted
,0 AS ActivityCounter
,CASE when b.ExpSubmissionPolicyPremium IS NULL THEN 0 ELSE
b.ExpSubmissionPolicyPremium END AS ExpSubmissionPolicyPremium
,1 SubCounter
,CurrentPremium SubDol
,case when (FIRSTQUOTEDATE is not null or submissionstatuscode = 'BIN')
then 1 else 0 end QuoteCounter
,case when (FIRSTQUOTEDATE is not null or submissionstatuscode = 'BIN')
then CurrentPremium else 0 end QuoteDol
,case when SubmissionStatusCode='BIN' then 1 else 0 end BoundCounter
,case when SubmissionStatusCode='BIN' then CurrentPremium else 0 end
BoundDol
,case when SUBSTRING(SubmissionStatusCode,1,1)='T' then 1 else 0 end
TerminateCounter
,case when SUBSTRING(SubmissionStatusCode,1,1)='T' then CurrentPremium
else 0 end TerminateDol
,case when b.CURRENTHOUSECOMMISSION IS not NULL then
CURRENTHOUSECOMMISSION else 0 end SubCommission
,case when (FIRSTQUOTEDATE is not null or submissionstatuscode = 'BIN')
and b.CURRENTHOUSECOMMISSION IS not NULL then CURRENTHOUSECOMMISSION else
0 end QuoteCommission
,case when SubmissionStatusCode='BIN' and b.CURRENTHOUSECOMMISSION IS not
NULL then CURRENTHOUSECOMMISSION else 0 end BoundCommission
,case when SUBSTRING(SubmissionStatusCode,1,1)='T' then
CURRENTHOUSECOMMISSION else 0 end TerminateCommission
-- Projected
,0 AS Projected_ExpSubmissionPolicyPremium
,0 AS Projected_SubCounter
,0 AS Projected_SubDol
,0 AS Projected_QuoteCounter
,0 AS Projected_QuoteDol
,0 AS Projected_BoundCounter
,0 AS Projected_BoundDol
,0 AS Projected_TerminateCounter
,0 AS Projected_TerminateDol
,0 AS Projected_SubCommission
,0 AS Projected_QuoteCommission
,0 AS Projected_BoundCommission
,0 AS Projected_TerminateCommission
from ABCXYZ.SYSDBA.c_Binder b
left join ABCXYZ.SYSDBA.account a on a.accountid =b.accountid
left join ABCXYZ.SYSDBA.address ad on ad.addressid =a.addressid
left join ABCXYZ.SYSDBA.Contact c on c.contactid =b.contactid
left join ABCXYZ.SYSDBA.USERInfo u on u.userid =b.Underwriteruserid
left join company co on co.companycode=b.companycode
union all
-- New Business Projections
Select
'Projection' as RowType
,'New Business' as RowSubType
,up.userID
,u.username
,'N/A' AS ACCOUNTID
,'N/A' AS Agency
,'N/A' AS CITY
,'N/A' AS State
,'N/A' AS ZIP
,'N/A' AS COUNTRY
,'N/A' AS contactID
,'N/A' Agent
,coalesce(substring(u.division,1,2),'N/A') AS House
,'N/A' AS DEPTARTMENTCODE
,'N/A' AS companycode
,'N/A' AS CompanyName
,'N/A' AS SubmissionNumber
,up.usertext1 AS Underwriter
,'N/A' AS Insured1
,'N/A' AS PolicyTypeCode
,case when (mn.month<=3 and month(getdate())<=3) or (mn.month>3 and
month(getdate())>3)
then
convert(datetime,mn.month+'/01/'+convert(char(4),YEAR(getdate())-2000),1)
when (mn.month>3 and month(getdate())<=3)
THEN
convert(datetime,mn.month+'/01/'+convert(char(4),YEAR(getdate())-2000-
1),1)
when (mn.month<=3 and month(getdate())>3)
THEN
convert(datetime,mn.month+'/01/'+convert(char(4),YEAR(getdate())-2000+1),
1) end as SubmitDate,mn.month as Submit_Month
,case when (mn.month<=3 and month(getdate())<=3) or (mn.month>3 and
month(getdate())>3)
convert(datetime,mn.month+'/01/'+convert(char(4),YEAR(getdate())-2000+1),
1) end as SubmitDate,mn.month as Submit_Month
,case when (mn.month<=3 and month(getdate())<=3) or (mn.month>3 and
month(getdate())>3)
then YEAR(getdate())
when (mn.month>3 and month(getdate())<=3)
THEN YEAR(getdate())-1
when (mn.month<=3 and month(getdate())>3)
THEN YEAR(getdate())+1
end as Submit_Year
,case when (mn.month<=3 and month(getdate())<=3) or (mn.month>3 and
month(getdate())>3)
then
convert(datetime,mn.month+'/01/'+convert(char(4),YEAR(getdate())-2000),1)
when (mn.month>3 and month(getdate())<=3)
THEN
convert(datetime,mn.month+'/01/'+convert(char(4),YEAR(getdate())-2000-
1),1)
when (mn.month<=3 and month(getdate())>3)
THEN
convert(datetime,mn.month+"/01/"convert(char(4),YEAR(getdate())-2000+1),
1) end as FirstQuoteDate
,mn.month as FirstQuote_Month
,case when (mn.month<=3 and month(getdate())<=3) or (mn.month>3 and
month(getdate())>3)
then YEAR(getdate())
when (mn.month>3 and month(getdate())<=3)
THEN YEAR(getdate())-1
when (mn.month<=3 and month(getdate())>3)
THEN YEAR(getdate())+1
end as FirstQuote_Year
,case when (mn.month<=3 and month(getdate())<=3) or (mn.month>3 and
month(getdate())>3)
then
convert(datetime,mn.month+'/01/'+convert(char(4),YEAR(getdate())-2000),1)
when (mn.month>3 and month(getdate())<=3)
THEN
convert(datetime,mn.month+'/01/'+convert(char(4),YEAR(getdate())-2000-
1),1)
when (mn.month<=3 and month(getdate())>3)
THEN
convert(datetime,mn.month+'/01/'+convert(char(4),YEAR(getdate())-2000+1),
1)end as BoundDate
,mn.month as Bound_Month
,case when (mn.month<=3 and month(getdate())<=3) or (mn.month>3 and
month(getdate())>3)
then YEAR(getdate())
when (mn.month>3 and month(getdate())<=3)
THEN YEAR(getdate())-1
when (mn.month<=3 and month(getdate())>3)
THEN YEAR(getdate())+1
end as Bound_Year
,case when (mn.month<=3 and month(getdate())<=3) or (mn.month>3 and
month(getdate())>3)
then
convert(datetime,mn.month+'/01/'+convert(char(4),YEAR(getdate())-2000),1)
when (mn.month>3 and month(getdate())<=3)
THEN
convert(datetime,mn.month+'/01/'+convert(char(4),YEAR(getdate())-2000-
1),1)
when (mn.month<=3 and month(getdate())>3)
THEN
convert(datetime,mn.month+'/01/'+convert(char(4),YEAR(getdate())-2000+1),
1)
end as PolicyEffectiveDate
,mn.month as Effective_Month
,case when (mn.month<=3 and month(getdate())<=3) or (mn.month>3 and
month(getdate())>3)
then YEAR(getdate())
when (mn.month>3 and month(getdate())<=3)
THEN YEAR(getdate())-1
when (mn.month<=3 and month(getdate())>3)
THEN YEAR(getdate())+1
end as Effective_Year
,case when (Month(GETDATE())<=3)
then YEAR(getdate())-1
else
YEAR(getdate())
end
as Effective_FiscalYear
,case when (mn.month<=3 and month(getdate())<=3) or (mn.month>3 and
month(getdate())>3)
then
convert(datetime,mn.month+'/01/'+convert(char(4),YEAR(getdate())-2000+1),
1) when (mn.month>3 and month(getdate())<=3)
THEN
convert(datetime,mn.month+'/01/'+convert(char(4),YEAR(getdate())-2000),1)
when (mn.month<=3 and month(getdate())>3)
THEN
convert(datetime,mn.month+'/01/'+convert(char(4),YEAR(getdate())-2000+2),
1)
end as PolicyExpirationDate
,mn.month as Expiration_Month
,case when (mn.month<=3 and month(getdate())<=3) or (mn.month>3 and
month(getdate())>3)
then YEAR(getdate())+1
when (mn.month>3 and month(getdate())<=3)
THEN YEAR(getdate())
when (mn.month<=3 and month(getdate())>3)
THEN YEAR(getdate())+2
end as Expiration_Year
,case when (Month(GETDATE())<=3)
then YEAR(getdate())
else
YEAR(getdate())+1
end
as Expiration_FiscalYear
,'N/A' AS LOBCode
,'N/A' AS LOBName
,'N/A' AS LOBClass
,'N/A' AS DOBCode
,'N/A' AS DOBName
,'N/A' AS DOBClass
,'Bound' AS SubmissionStatus
,'BIN' AS SubmissionStatusCode
,'Y' as Quoted
,0 AS ActivityCounter
---- Actual
,0 AS ExpSubmissionPolicyPremium
,0 AS SubCounter
,0 AS SubDol
,0 AS QuoteCounter
,0 AS QuoteDol
,0 AS BoundCounter
,0 as BoundDol
,0 AS TerminateCounter
,0 AS TerminateDol
,0 AS SubCommission
,0 AS QuoteCommission
,0 AS BoundCommission
,0 AS TerminateCommission
-- Projected
,0 AS Projected_ExpSubmissionPolicyPremium
,0 AS Projected_SubCounter
,0 AS Projected_SubDol
,0 AS Projected_QuoteCounter
,0 AS Projected_QuoteDol
,0 AS Projected_BoundCounter
,CASE when up.USERNUM1 IS NULL THEN 0 ELSE (up.USERNUM1/12) END AS
Projected_BoundDol
,0 AS Projected_TerminateCounter
,0 AS Projected_TerminateDol
,0 AS Projected_SubCommission
,0 AS Projected_QuoteCommission
,(up.USERNUM1/12)*up.usernum2 AS Projected_BoundCommission
,0 AS Projected_TerminateCommission
from ABCXYZ.SYSDBA.USERPROFILE up
left join ABCXYZ.SYSDBA.USERInfo u on u.userid = up.userid
join dbo.MonthNumber mn on 1=1
where up.usertext1 is not null AND up.usernum1 is not null AND
up.usernum1>0
-- Renewal Bussness Projections
UNION ALL
Select
'Projection' as RowType
,'Renewal' as RowSubType
,CASE when b.Underwriteruserid IS NULL THEN 'N/A' ELSE
b.Underwriteruserid END AS userID
,CASE when u.username IS NULL THEN 'N/A' ELSE u.username END AS username
,CASE when B.ACCOUNTID IS NULL THEN 'N/A' ELSE B.ACCOUNTID END AS
ACCOUNTID
,CASE when a.account IS NULL THEN 'N/A' ELSE a.account END as Agency
,CASE when ad.CITY IS NULL THEN 'N/A' ELSE ad.CITY END as CITY
,CASE when ad.state IS NULL THEN 'N/A' ELSE ad.state END as State
,CASE when ad.POSTALCODE IS NULL THEN 'N/A' ELSE ad.POSTALCODE END as ZIP
,CASE when ad.COUNTRY IS NULL THEN 'US' ELSE ad.state END as COUNTRY
,CASE when c.ContactID IS NULL THEN 'N/A' ELSE c.ContactID END as
contactID
,CASE when c.firstname+' '+c.lastname IS NULL THEN 'N/A' ELSE
c.firstname+' '+c.lastname END as Agent
,b.HouseNumber as House
,B.DEPTARTMENTCODE
,co.companycode
,coalesce(co.companyname1,'')+' '+coalesce(co.companyname2,'')+'
('+co.companycode+')' as CompanyName
,b.SubmissionNumber
,b.Underwriter
,b.Insured1
,b.PolicyTypeCode
,DATEADD(YYYY,1,b.Submissioncreatedate) as SubmitDate
,month(DATEADD(YYYY,1,b.Submissioncreatedate)) as Submit_Month
,year(DATEADD(YYYY,1,b.Submissioncreatedate)) as Submit_Year
,DATEADD(YYYY,1,b.FirstQuoteDate) AS FirstQuoteDate
,month(DATEADD(YYYY,1,b.FirstQuoteDate)) as FirstQuote_Month
,year(DATEADD(YYYY,1,b.FirstQuoteDate)) as FirstQuote_Year
,DATEADD(YYYY,1,b.BoundDate) AS BoundDate
,month(DATEADD(YYYY,1,b.BoundDate)) as Bound_Month
,year(DATEADD(YYYY,1,b.BoundDate)) as Bound_Year
,DATEADD(YYYY,1,b.PolicyEffectiveDate) as PolicyEffectiveDate
,month(b.PolicyEffectiveDate) as Effective_Month
,year(b.PolicyEffectiveDate)+1 as Effective_Year
,case when (month(b.PolicyEffectiveDate))<=3 then
year(b.PolicyEffectiveDate) else year(b.PolicyEffectiveDate)+1 end as
Effective_FiscalYear
,DATEADD(YYYY,1,b.PolicyExpirationDate) as PolicyExpirationDate
,month(b.PolicyExpirationDate) as Expiration_Month
,year(b.PolicyExpirationDate)+1 as Expiration_Year
,case when (month(b.PolicyExpirationDate))<=3 then
year(b.PolicyExpirationDate) else year(b.PolicyExpirationDate)+1 end as
Expiration_FiscalYear
,CASE when b.LOBCode IS NULL THEN 'N/A' ELSE b.LOBCode end as LOBCode
,CASE when b.LOBName IS NULL THEN 'N/A' ELSE b.LOBName end as LOBName
,CASE when B.LOBClass IS NULL THEN 'N/A' ELSE B.LOBClass end as LOBClass
,CASE when b.IndustryDOBCode IS NULL THEN 'N/A' ELSE b.IndustryDOBCode
end AS DOBCode
,CASE when b.IndustryDOBName IS NULL THEN 'N/A' ELSE b.IndustryDOBName
end AS DOBName
,CASE when B.IndustryDOBClass IS NULL THEN 'N/A' ELSE B.IndustryDOBClass
end as DOBClass
,case
when SubmissionStatusCode='SUB' then 'Submitted'
when SubmissionStatusCode='QUO' then 'Quoted'
when SubmissionStatusCode='BIN' then 'Bound'
when SUBSTRING(SubmissionStatusCode,1,1)='T' then 'Termintated'
else 'Other'
end SubmissionStatus
,SubmissionStatusCode
,case when (FIRSTQUOTEDATE is not null or submissionstatuscode = 'BIN')
then 'Y' else 'N' end Quoted
,0 AS ActivityCounter
---- Actual
,0 AS ExpSubmissionPolicyPremium
,0 AS SubCounter
,0 AS SubDol
,0 AS QuoteCounter
,0 AS QuoteDol
,0 AS BoundCounter
,0 as BoundDol
,0 AS TerminateCounter
,0 AS TerminateDol
,0 AS SubCommission
,0 AS QuoteCommission
,0 AS BoundCommission
,0 AS TerminateCommission
--- Projected
,CASE when b.CurrentPremium IS NULL THEN 0 ELSE b.CurrentPremium END AS
Projected_ExpSubmissionPolicyPremium
,0 as Projected_SubCounter
,0 as Projected_SubDol
,0 AS Projected_QuoteCounter
,0 AS Projected_QuoteDol
,case when SubmissionStatusCode='BIN' then 1*up.usernum3 else 0 end AS
Projected_BoundCounter
,case when SubmissionStatusCode='BIN' then CurrentPremium*up.usernum3
else 0 end AS Projected_BoundDol
,0 as Projected_TerminateCounter
,0 as Projected_TerminateDol
,0 AS Projected_SubCommission
,0 AS Projected_QuoteCommission
,case when SubmissionStatusCode='BIN' and b.CURRENTHOUSECOMMISSION IS not
NULL then CURRENTHOUSECOMMISSION*up.usernum3 else 0 end AS
Projected_BoundCommission
,0 AS Projected_TerminateCommission
from ABCXYZ.SYSDBA.c_Binder b
left join ABCXYZ.SYSDBA.account a on a.accountid =b.accountid
left join ABCXYZ.SYSDBA.address ad on ad.addressid =a.addressid
left join ABCXYZ.SYSDBA.Contact c on c.contactid =b.contactid
left join ABCXYZ.SYSDBA.USERInfo u on u.userid =b.Underwriteruserid
left join ABCXYZ.SYSDBA.USERPROFILE up on uP.userid =b.Underwriteruserid
left join company co on co.companycode=b.companycode
where up.usertext1 is not null AND up.usernum3 is not null AND
up.usernum3>0
and case when (month(b.PolicyEffectiveDate))<=3 then
year(b.PolicyEffectiveDate) else year(b.PolicyEffectiveDate)+1 end
= case when (Month(GETDATE())<=3)
then YEAR(getdate())-1
else
YEAR(getdate())
end
Union all
-- Open Activity Data
Select
'Open Activity' as RowType
,at.atype as RowSubType
,CASE when h.userID IS NULL THEN 'N/A' ELSE h.userID END AS userID
,CASE when u.username IS NULL THEN 'N/A' ELSE u.username END AS username
,CASE when h.ACCOUNTID IS NULL THEN 'N/A' ELSE h.ACCOUNTID END AS
ACCOUNTID
,CASE when a.account IS NULL THEN 'N/A' ELSE a.account END as Agency
,CASE when ad.CITY IS NULL THEN 'N/A' ELSE ad.CITY END as CITY
,CASE when ad.state IS NULL THEN 'N/A' ELSE ad.state END as State
,CASE when ad.POSTALCODE IS NULL THEN 'N/A' ELSE ad.POSTALCODE END as ZIP
,CASE when ad.COUNTRY IS NULL THEN 'US' ELSE ad.state END as COUNTRY
,CASE when c.ContactID IS NULL THEN 'N/A' ELSE c.ContactID END as
contactID
,CASE when c.firstname+' '+c.lastname IS NULL THEN 'N/A' ELSE
c.firstname+' '+c.lastname END as Agent
,coalesce(substring(u.division,1,2),'N/A') AS House
,'N/A' AS DEPTARTMENTCODE
,'N/A' AS companycode
,'N/A' AS CompanyName
,'N/A' AS SubmissionNumber
,up.usertext1 AS Underwriter
,'N/A' AS Insured1
,'N/A' as PolicyTypeCode
,h.startdate as SubmitDate
,month(h.startdate) as Submit_Month
,year(h.startdate) as Submit_Year
,h.startdate as FirstQuoteDate
,month(h.startdate) as FirstQuote_Month
,year(h.startdate) as FirstQuote_Year
,h.startdate as BoundDate
,month(h.startdate) as Bound_Month
,year(h.startdate) as Bound_Year
,h.startdate as PolicyEffectiveDate
,month(h.startdate) as Effective_Month
,year(h.startdate) as Effective_Year
,case when (month(h.startdate)<=3) then year(h.startdate)-1 else
year(h.startdate) end as Effective_FiscalYear
,h.startdate as PolicyExpirationDate
,month(h.startdate) as Expiration_Month
,year(h.startdate) as Expiration_Year
,case when (month(h.startdate)<=3) then year(h.startdate)-1 else
year(h.startdate) end as Expiration_FiscalYear
,'N/A' AS LOBCode
,'N/A' AS LOBName
,'N/A' AS LOBClass
,'N/A' AS DOBCode
,'N/A' AS DOBName
,'N/A' AS DOBClass
,'N/A' AS SubmissionStatus
,'N/A' AS SubmissionStatusCode
,'N/A' as Quoted
,1 AS ActivityCounter
,0 AS ExpSubmissionPolicyPremium
,0 AS SubCounter
,0 AS SubDol
,0 AS QuoteCounter
,0 AS QuoteDol
,0 AS BoundCounter
,0 AS BoundDol
,0 AS TerminateCounter
,0 AS TerminateDol
,0 AS SubCommission
,0 AS QuoteCommission
,0 AS BoundCommission
,0 AS TerminateCommission
-- Projected
,0 AS Projected_ExpSubmissionPolicyPremium
,0 AS Projected_SubCounter
,0 AS Projected_SubDol
,0 AS Projected_QuoteCounter
,0 AS Projected_QuoteDol
,0 AS Projected_BoundCounter
,0 AS Projected_BoundDol
,0 AS Projected_TerminateCounter
,0 AS Projected_TerminateDol
,0 AS Projected_SubCommission
,0 AS Projected_QuoteCommission
,0 AS Projected_BoundCommission
,0 AS Projected_TerminateCommission
from ABCXYZ.sysdba.activity h
join (select pld.ID as Type,pld.TEXT as AType
from ABCXYZ.sysdba.PICKLIST pld
join ABCXYZ.sysdba.PICKLIST plh on plh.itemid=pld.PICKLISTID
where plh.PICKLISTID='PICKLISTLIST' and plh.TEXT='Activity Types') AT
on h.TYPE=AT.Type
left join ABCXYZ.SYSDBA.account a on a.accountid =h.accountid
left join ABCXYZ.SYSDBA.address ad on ad.addressid =a.addressid
left join ABCXYZ.SYSDBA.Contact c on c.contactid =h.contactid
left join ABCXYZ.SYSDBA.USERPROFILE up on up.userid =h.userID
left join ABCXYZ.SYSDBA.USERInfo u on u.userid =h.userID
Union all
-- Completed Activity Data
Select
'Completed Activity' as RowType
,at.atype as RowSubType
,CASE when h.userID IS NULL THEN 'N/A' ELSE h.userID END AS userID
,CASE when u.username IS NULL THEN 'N/A' ELSE u.username END AS username
,CASE when h.ACCOUNTID IS NULL THEN 'N/A' ELSE h.ACCOUNTID END AS
ACCOUNTID
,CASE when a.account IS NULL THEN 'N/A' ELSE a.account END as Agency
,CASE when ad.CITY IS NULL THEN 'N/A' ELSE ad.CITY END as CITY
,CASE when ad.state IS NULL THEN 'N/A' ELSE ad.state END as State
,CASE when ad.POSTALCODE IS NULL THEN 'N/A' ELSE ad.POSTALCODE END as ZIP
,CASE when ad.COUNTRY IS NULL THEN 'US' ELSE ad.state END as COUNTRY
,CASE when c.ContactID IS NULL THEN 'N/A' ELSE c.ContactID END as
contactID
,CASE when c.firstname+' '+c.lastname IS NULL THEN 'N/A' ELSE
c.firstname+' '+c.lastname END as Agent
,coalesce(substring(u.division,1,2),'N/A') AS House
,'N/A' AS DEPTARTMENTCODE
,'N/A' AS companycode
,'N/A' AS CompanyName
,'N/A' AS SubmissionNumber
,up.usertext1 AS Underwriter
,'N/A' AS Insured1
,'N/A' as PolicyTypeCode
,h.COMPLETEDDATE as SubmitDate
,month(h.COMPLETEDDATE) as Submit_Month
,year(h.COMPLETEDDATE) as Submit_Year
,h.COMPLETEDDATE as FirstQuoteDate
,month(h.COMPLETEDDATE) as FirstQuote_Month
,year(h.COMPLETEDDATE) as FirstQuote_Year
,h.COMPLETEDDATE as BoundDate
,month(h.COMPLETEDDATE) as Bound_Month
,year(h.COMPLETEDDATE) as Bound_Year
,h.COMPLETEDDATE as PolicyEffectiveDate
,month(h.COMPLETEDDATE) as Effective_Month
,year(h.COMPLETEDDATE) as Effective_Year
,case when (month(h.COMPLETEDDATE)<=3) then year(h.COMPLETEDDATE)-1 else
year(h.COMPLETEDDATE) end as Effective_FiscalYear
,h.COMPLETEDDATE as PolicyExpirationDate
,month(h.COMPLETEDDATE) as Expiration_Month
,year(h.COMPLETEDDATE) as Expiration_Year
,case when (month(h.COMPLETEDDATE)<=3) then year(h.COMPLETEDDATE)-1 else
year(h.COMPLETEDDATE) end as Expiration_FiscalYear
,'N/A' AS LOBCode
,'N/A' AS LOBName
,'N/A' AS LOBClass
,'N/A' AS DOBCode
,'N/A' AS DOBName
,'N/A' AS DOBClass
,'N/A' AS SubmissionStatus
,'N/A' AS SubmissionStatusCode
,'N/A' as Quoted
,1 AS ActivityCountera
,0 AS ExpSubmissionPolicyPremium
,0 AS SubCounter
,0 AS SubDol
,0 AS QuoteCounter
,0 AS QuoteDol
,0 AS BoundCounter
,0 AS BoundDol
,0 AS TerminateCounter
,0 AS TerminateDol
,0 AS SubCommission
,0 AS QuoteCommission
,0 AS BoundCommission
,0 AS TerminateCommission
-- Projected
,0 AS Projected_ExpSubmissionPolicyPremium
,0 AS Projected_SubCounter
,0 AS Projected_SubDol
,0 AS Projected_QuoteCounter
,0 AS Projected_QuoteDol
,0 AS Projected_BoundCounter
,0 AS Projected_BoundDol
,0 AS Projected_TerminateCounter
,0 AS Projected_TerminateDol
,0 AS Projected_SubCommission
,0 AS Projected_QuoteCommission
,0 AS Projected_BoundCommission
,0 AS Projected_TerminateCommission
from ABCXYZ.sysdba.history h
join (select pld.ID as Type,pld.TEXT as AType
from ABCXYZ.sysdba.PICKLIST pld
join ABCXYZ.sysdba.PICKLIST plh on plh.itemid=pld.PICKLISTID
where plh.PICKLISTID='PICKLISTLIST' and plh.TEXT='Activity Types') AT
on h.TYPE=AT.Type
left join ABCXYZ.SYSDBA.account a on a.accountid =h.accountid
left join ABCXYZ.SYSDBA.address ad on ad.addressid =a.addressid
left join ABCXYZ.SYSDBA.Contact c on c.contactid =h.contactid
left join ABCXYZ.SYSDBA.USERPROFILE up on up.userid =h.userID
left join ABCXYZ.SYSDBA.USERInfo u on u.userid =h.userID