For some reason I am unable to figure out - my "ip.Maintenance_Revenue_USD__c" field is duplicating, i.e. if there is one record with $630 it will multiply it by two or three times - no idea why!
I am trying to SUM up all the values for Maintenance Revenue in the field where they are Active & Customer for the Product Line.
CODE:-
select distinct
[Softrax ID],
[Account ID],
[Owner],
[Owner Role],
[Account Owner LOB],
[Account Name],
[Account Record Type],
[Class],
[Primary Address Line 1],
[Primary City],
[Primary State],
[Primary County],
[Primary Postal Code],
[Primary Country],
[Region],
[Sub-Region],
[D&B SIC Code 1],
[D&B SIC Code Description],
Ann_Rev_USD as [D&B Annual Revenue],
[D&B Employees],
[SIC Code 1],
[SIC Description],
[DUNS],
[DUDUNS],
[Parent/HQ DUNS],
[GUDUNS],
[Maintenance Status],
[Industry],
[Sub-Industry],
[Revenue Range],
[Employee Range],
[Product Line],
--[SKU],
--[SKU Description],
--[SKU Class],
[Maintenance Revenue USD],
--[Version],
[# of Licenses] -- max
--[Active Flag]--,
--[MBM],
--[Maintenance Start Date],
--[Maintenance Expiry Date]
from (
select
a.SofTrax_ID__c as [Softrax ID],
a.Unique_Id__c as [Account ID],
uo.name [Owner],
uo.Role_Name__c as [Owner Role],
uo.user_lob__c as [Account Owner LOB],
a.Name as [Account Name],
rt.name [Account Record Type],
a.Class__c as [Class],
a.Primary_Address_Line_1__c as [Primary Address Line 1],
a.Primary_City__c as [Primary City],
a.Primary_State__c as [Primary State],
a.Primary_County__c as [Primary County],
a.Primary_Postal_Code__c as [Primary Postal Code],
a.Primary_Country__c as [Primary Country],
a.Region__c as [Region],
a.Sub_Region__c as [Sub-Region],
a.D_B_SIC_1__c as [D&B SIC Code 1],
a.D_B_SIC_Desc_1__c as [D&B SIC Code Description],
a.D_B_of_Employees__c as [D&B Employees],
s.Name as [SIC Code 1],
s.Description__c as [SIC Description],
a.DUNS__c as [DUNS],
a.DU_DUNS__c as [DUDUNS],
a.Parent_DUNS__c as [Parent/HQ DUNS],
a.GU_DUNS__c as [GUDUNS],
a.Maintenance_Status__c as [Maintenance Status],
a.Main_Industry__c as [Industry],
a.Sub_Industry__c as [Sub-Industry],
vrr.range_desc as [Revenue Range],
vre.range_desc as [Employee Range],
ip.Product_Line__c as [Product Line],
--prod.SKU__c as [SKU],
--ip.Product_Class__c as [SKU Class],
sum(ip.Maintenance_Revenue_USD__c) as [Maintenance Revenue USD], -- sum at product level maintenance
max(ip.Quantity_Licenses__c) as [# of Licenses],
max(a.d_b_annual_revenue__c / isnull(c2.conversionrate,1)) as Ann_Rev_USD
from dbo.vwAccount a
JOIN vwInstalled_Product_Line__c IP on a.id = ip.Account__c
and ip.Active_Flag__c in ('true')
join dbo.RecordType rt on a.RecordtypeID = rt.ID
and rt.name in ('Customer')
LEFT Join gdw.dbo.vwUser UO (Nolock) ON a.ownerid = uo.id
left join dbo.AccountTerritory at (nolock) on a.id = at.accountid
left join vwproduct2 prod (nolock) on prod.id = ip.product__c
LEFT JOIN dbo.CurrencyType C2 (NoLock) ON c2.IsoCode = a.CurrencyIsoCode
left join vwinfor_sic_code__c s (nolock)
on s.id=a.Local_Industry_Lookup__c
--rev range
LEFT JOIN salesforce_prod.dbo.CurrencyType C (NoLock)
ON c.IsoCode = a.CurrencyIsoCode
LEFT JOIN gdw.dbo.valueranges vrr
ON vrr.range_code = 'revenue'
AND COALESCE(CASE
WHEN a.d_b_annual_revenue__c < 1 THEN NULL
ELSE a.d_b_annual_revenue__c / c.conversionrate
END, a.site_revenue__c / c.conversionrate , 0) BETWEEN vrr.low_range AND vrr.high_range
--emp range
left JOIN gdw.dbo.valueranges vre
ON vre.range_code = 'employee'
AND COALESCE(CASE
WHEN a.D_B_of_Employees__c < 1 THEN NULL
ELSE a.D_B_of_Employees__c
END, a.employee_count__c, 0) BETWEEN vre.low_range AND vre.high_range
Group by
a.SofTrax_ID__c,
a.Unique_Id__c,
uo.name,
uo.Role_Name__c,
uo.user_lob__c ,
a.Name,
rt.name,
a.Class__c,
a.Primary_Address_Line_1__c ,
a.Primary_City__c,
a.Primary_State__c ,
a.Primary_County__c,
a.Primary_Postal_Code__c ,
a.Primary_Country__c,
a.Region__c ,
a.Sub_Region__c,
a.D_B_SIC_1__c,
a.D_B_SIC_Desc_1__c ,
--[D&B Annual Revenue],
a.D_B_of_Employees__c,
s.Name ,
s.Description__c ,
a.DUNS__c ,
a.DU_DUNS__c ,
a.Parent_DUNS__c ,
a.GU_DUNS__c ,
a.Maintenance_Status__c ,
a.Main_Industry__c ,
a.Sub_Industry__c ,
ip.Product_Line__c,
vrr.range_desc ,
vre.range_desc) temp
order by 2