Hi,
i am just looking for a way of improving the performance of the attached query.
currently it takes about 4 minutes to return results.
with AssetCount
as
(select AssetType
,AssetCategory
,ObjectType
,CatalogProfile
,MainStatus
,Ownership
,Count(Distinct AssetID) CountOfAsset
from moskpi.MDAssetHeaderInfo
Group by AssetType
,AssetCategory
,ObjectType
,CatalogProfile
,MainStatus
,Ownership),
HeaderStat
as (select AssetType
,AssetCategory
,ObjectType
,CatalogProfile
,MainStatus
,Owner
,AssetID
,CharDesc
,CharValue
from (
select AssetType
,AssetCategory
,ObjectType
,CatalogProfile
,MainStatus
,Ownership Owner
,AssetID
,convert(nvarchar(100),Ownership) Ownership
,convert(nvarchar(100),Installdate) InstallDate
,convert(nvarchar(100),FunctionalLoc) FunctionalLoc
,convert(nvarchar(100),Room) Room
,convert(nvarchar(100),OperatingCircuit) OperatingCircuit
,convert(nvarchar(100),OperatingVoltage) OperatingVoltage
from moskpi.MDAssetHeaderInfo
--where CatalogProfile = '7501'
) c
unpivot ( CharValue for CharDesc in ("Ownership","Installdate","FunctionalLoc","Room","OperatingCircuit","OperatingVoltage")) unpvt
)
select ac.*, a.CharDesc, case when CharCount is null then 0 else CharCount end CharCount
from AssetCount ac
cross join (select distinct CharDesc from HeaderStat) a
left outer join (select AssetType
,AssetCategory
,ObjectType
,CatalogProfile
,MainStatus
,Owner
,CharDesc
,Count(CharValue) CharCount
from HeaderStat
Group by AssetType
,AssetCategory
,ObjectType
,CatalogProfile
,MainStatus
,Owner
,CharDesc ) hs
on ac.AssetType = hs.AssetType
and ac.AssetCategory = hs.AssetCategory
and ac.ObjectType = hs.ObjectType
and ac.CatalogProfile = hs.CatalogProfile
and ac.MainStatus = hs.MainStatus
and ac.Ownership = hs.Owner
and a.CharDesc = hs.CharDesc
thank you in advance.