SELECT
SUM(TransactionList.UniqueCust) AS UniqueChargedUsers,
ItemList.ItemID,
ProductName,
TransactionList.ArtistName,
TransactionList.ArtistID,
AlbumList.AlbumName,
AlbumList.AlbumID,
CPList.ContentProviderName,
TransactionList.CreateDate,
ItemList.ContentProviderID,
SUM(TransactionList.Request) AS TotalCount,
TransactionList.ItemType,
TransactionList.Price,
SUM(TransactionList.Gross) AS GrossRevenue,
TransactionList. STATUS
FROM
(
SELECT
Count(DISTINCT Customer) AS UniqueCust,
ItemId AS ContentID,
Count(*) AS Request,
ItemType,
CONVERT (VARCHAR(10), CreateDate, 102) AS CreateDate,
(
SELECT
[ Name_other ]
FROM
dbo.ViewArtistName
WHERE
dbo.ViewArtistName.ArtistID = (
SELECT
TOP 1 ArtistID
FROM
dbo.ItemArtist
WHERE
dbo.ItemArtist.ItemID = TransactionInfo.ItemId
)
) AS ArtistName,
(
SELECT
TOP 1 ArtistID
FROM
dbo.ItemArtist
WHERE
dbo.ItemArtist.ItemID = TransactionInfo.ItemId
) AS ArtistID,
Price,
Price * (Count(*)) AS Gross,
STATUS,
Paid,
MediaCode = CASE
WHEN isNumeric (
SUBSTRING(
attribute,
CHARINDEX (
'MaxisMediaCode=',
attribute
) + len ('MaxisMediaCode='),
2
)
) >= 1 THEN
SUBSTRING(
attribute,
CHARINDEX (
'MaxisMediaCode=',
attribute
) + len ('MaxisMediaCode='),
2
)
ELSE
'00'
END
FROM
[ AortaDB ].[ dbo ].[ TransactionInfo ]
WHERE
Attribute NOT LIKE '%redownload=Y%'
AND CHARINDEX (
'MaxisMediaCode=',
attribute
) > 0
AND Price IS NOT NULL
AND Price != '0.00'
AND ParentTransCode IS NULL
AND Channel <> 'Streaming'
AND Attribute NOT LIKE '%ServiceType=UNLMITED_MUSIC%'
AND CONVERT (VARCHAR(10), CreateDate, 102) = < Date >
AND ItemType = < ItemType >
AND Channel = < Channel >
GROUP BY
ItemId,
CONVERT (VARCHAR(10), CreateDate, 102),
Price,
Customer,
STATUS,
Paid
) TransactionList
INNER JOIN (
SELECT
V1.ItemID AS ItemID,
(
SELECT
LanguageType + ' | '
FROM
dbo.viewItemInfo V2
WHERE
V2.ItemID = V1.ItemID
AND LanguageType != 'Other' FOR XML PATH ('')
) AS OriLang,
MAX(VALUE) AS ProductName,
contentProviderID AS ContentProviderID
FROM
dbo.viewItemInfo V1
WHERE
V1.LanguageType = 'Other'
GROUP BY
V1.ItemID,
VALUE
,
contentProviderID,
ItemType
) ItemList ON TransactionList.ContentID = ItemList.ItemID
INNER JOIN (
SELECT
dbo.ContentProviderInfo.ContentProviderID AS ContentProviderID,
dbo.ContentProviderInfo. NAME AS ContentProviderName
FROM
dbo.ContentProviderInfo
WHERE
ContentProviderID = < ContentProviderID >
) CPList ON ItemList.ContentProviderID = CPList.ContentProviderID
LEFT OUTER JOIN dbo.ItemAlbum ON ItemList.ItemID = dbo.ItemAlbum.ItemID
LEFT OUTER JOIN (
SELECT
dbo.ViewAlbumInfo.AlbumID AS AlbumId,
dbo.ViewAlbumInfo.
VALUE
AS AlbumName
FROM
dbo.ViewAlbumInfo
WHERE
dbo.ViewAlbumInfo.LanguageType = 'Other'
) AlbumList ON dbo.ItemAlbum.AlbumID = AlbumList.AlbumId
GROUP BY
CPList.ContentProviderName,
ItemList.ContentProviderID,
TransactionList.CreateDate,
TransactionList.Price,
AlbumList.AlbumName,
AlbumList.AlbumID,
ItemList.ItemID,
ItemList.ProductName,
TransactionList.ArtistName,
TransactionList.ArtistID,
TransactionList. STATUS