大家下午好。我将在它的全部荣耀中发布存储过程。随意将其撕成碎片。作者不会介意的。
DECLARE @itemTypeID INT
SELECT @itemTypeID=ItemTypeID FROM dbo.ItemTypes WHERE ItemTypeName = 'Advert'
BEGIN
SELECT a.Active,
a.ParentClass,
a.Classification,
a.Variant,
FV."Full Views",
PV."Print Views",
EE."Email Enquiries",
a.ItemRef,
a.SiteID
FROM
(
SELECT DISTINCT i.ItemID,
i.ItemRef,
i.SiteID,
i.ParentClass,
i.Classification,
i.Summary AS "Variant",
i.Active
FROM Items i
JOIN Actions a
ON a.ItemID = i.ItemID
JOIN ActionTypes at
ON a.ActionTypeID = at.ActionTypeID
WHERE i.ItemTypeID = 1
AND a.DateAndTime BETWEEN @startDate AND @endDate
AND at.ActionTypeName IN ('Full view', 'Print view', 'Email enquiry')
AND ((@siteID = -1) OR (i.SiteID = @siteID))
AND ((@parentClass = '%') OR (i.ParentClass = @parentClass))
AND ((@class = '%') OR (i.classification = @class))
) a LEFT JOIN
(
SELECT i.ItemID,
COUNT(*) AS "Full Views"
FROM CustomerSites cs JOIN Items i
ON cs.SiteID = i.SiteID
JOIN Actions a
ON a.ItemID = i.ItemID
JOIN ActionTypes at
ON a.ActionTypeID = at.ActionTypeID
JOIN Sites s
ON cs.SiteID = s.SiteID
WHERE a.DateAndTime BETWEEN @startDate AND @endDate
AND i.ItemTypeID = @itemTypeID
AND at.ActionTypeName = 'Full view'
AND ((@customerID IS NULL) OR (cs.CustomerID = @customerID))
AND ((@siteID = -1) OR (cs.SiteID = @siteID))
AND ((@parentClass = '%') OR (i.ParentClass = @parentClass))
AND ((@class = '%') OR (i.classification = @class))
GROUP BY
i.ItemID
) FV
ON a.ItemID = FV.ItemID
LEFT JOIN
(
SELECT i.ItemID,
COUNT(*) AS "Print Views"
FROM CustomerSites cs JOIN Items i
ON cs.SiteID = i.SiteID
JOIN Actions a
ON a.ItemID = i.ItemID
JOIN ActionTypes at
ON a.ActionTypeID = at.ActionTypeID
JOIN Sites s
ON cs.SiteID = s.SiteID
WHERE a.DateAndTime BETWEEN @startDate AND @endDate
AND i.ItemTypeID = @itemTypeID
AND at.ActionTypeName = 'Print view'
AND ((@customerID IS NULL) OR (cs.CustomerID = @customerID))
AND ((@siteID = -1) OR (cs.SiteID = @siteID))
AND ((@parentClass = '%') OR (i.ParentClass = @parentClass))
AND ((@class = '%') OR (i.classification = @class))
GROUP BY
i.ItemID
) PV
ON a.ItemID = PV.ItemID
LEFT JOIN
(
SELECT i.ItemID,
COUNT(*) AS "Email Enquiries"
FROM CustomerSites cs JOIN Items i
ON cs.SiteID = i.SiteID
JOIN Actions a
ON a.ItemID = i.ItemID
JOIN ActionTypes at
ON a.ActionTypeID = at.ActionTypeID
JOIN Sites s
ON cs.SiteID = s.SiteID
WHERE a.DateAndTime BETWEEN @startDate AND @endDate
AND i.ItemTypeID = @itemTypeID
AND at.ActionTypeName = 'Email enquiry'
AND ((@customerID IS NULL) OR (cs.CustomerID = @customerID))
AND ((@siteID = -1) OR (cs.SiteID = @siteID))
AND ((@parentClass = '%') OR (i.ParentClass = @parentClass))
AND ((@class = '%') OR (i.classification = @class))
GROUP BY
i.ItemID
) EE
ON a.ItemID = EE.ItemID
UNION
SELECT '0','','','','','','','',''
现在最终所有这一切都是返回一些记录以及针对它们发生特定操作的次数。
一个小子集看起来像。
项目 描述 查看打印的电子邮件
Item1 Desc1 12 NULL 1
Item2 Desc2 NULL NULL 3
Item3 Desc3 5 6 2
希望你能看到发生了什么。
我想要一个在特定日期范围内针对特定站点的特定客户对它们执行过操作的项目列表,并且查询应该可以根据父类和分类进行过滤。好的
第一个选择返回所有符合选择条件的不同项目。
其他 3 个查询都只返回针对每个项目的 1 种操作类型的计数。即使针对少量数据,查询也很慢。这永远不会上线,它只是行不通。
希望您能看到“作者”方式的错误并纠正他/她。