DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(D.DESTINATION)
from BillingDetail B inner join BillingDialDestinationtranz D on B.CDid=D.TDid
group by D.DESTINATION
order by D.DESTINATION
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT Carrier, ' + @cols + ' ,CCost
from
(
select CarrierDD.DESTINATION,C.CarrierName as Carrier,round(sum(((cast(BM.CallDuration as real)))*BM.CRates )/sum(BM.CallDuration),6) as CRates,round(sum(BM.CallDuration) ,4) as Minutes,round(sum(((cast(BM.CallDuration as real)))*BM.CRates ),4 ) as CCost
from [4BSales].[dbo].[BillingDetail] as BM inner join [4BSales].[dbo].BillingCarrier C on BM.CarrierId= C.CarrierId
left outer join [4BSales].[dbo].BillingDialDestinationtranz CarrierDD on BM.CDid=CarrierDD.TDid
left outer join [4BSales].[dbo].BillingDialDestinationtranz VendorDD on BM.VDid=VendorDD.TDid
left outer join [4BSales].[dbo].BillingCarrier v on BM.vendorid= V.CarrierId
group by C.CarrierName,CarrierDD.DESTINATION
) x
pivot
(
sum(CRates)
for DESTINATION in (' + @cols + ')
) p '
execute(@query)
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(D.DESTINATION)
from BillingDetail B inner join BillingDialDestinationtranz D on B.CDid=D.TDid
group by D.DESTINATION
order by D.DESTINATION
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT Carrier, ' + @cols + ' ,CCost
from
(
select CarrierDD.DESTINATION,C.CarrierName as Carrier,round(sum(((cast(BM.CallDuration as real)))*BM.CRates )/sum(BM.CallDuration),6) as CRates,round(sum(BM.CallDuration) ,4) as Minutes,round(sum(((cast(BM.CallDuration as real)))*BM.CRates ),4 ) as CCost
from [4BSales].[dbo].[BillingDetail] as BM inner join [4BSales].[dbo].BillingCarrier C on BM.CarrierId= C.CarrierId
left outer join [4BSales].[dbo].BillingDialDestinationtranz CarrierDD on BM.CDid=CarrierDD.TDid
left outer join [4BSales].[dbo].BillingDialDestinationtranz VendorDD on BM.VDid=VendorDD.TDid
left outer join [4BSales].[dbo].BillingCarrier v on BM.vendorid= V.CarrierId
group by C.CarrierName,CarrierDD.DESTINATION
) x
pivot
(
sum(CRates)
for DESTINATION in (' + @cols + ')
) p '
execute(@query)
No comments :
Post a Comment