首先,我声称没有SQL的极端专业知识。
我要做的是根据几个不同的标准从SQL Server数据库中提取一些事件计数,并将它们放入SSRS中,在一个折线图中显示每个月的计数。我最初创建的两个查询都会下拉正确的数据,但SSRS不允许在一个图表上使用多个数据集。这是我被挂断的地方。我确信我可以进行某种子查询,将两个数据集合成一个只需将计数分成列的数据集,但到目前为止我所尝试的数据集合了整个计数。
我需要的数据是报告的日期,它是日期/时间值和发生的事件计数(无需汇总计数,或者不应该,因为SSRS将允许我按月对它们进行分组,这是报告应该是什么样的)匹配2个标准中的每一个。
以下是我正在使用的两个查询:
SELECT DISTINCT v_Incident.ReportedDate, COUNT(*) AS Count
FROM v_Incident INNER JOIN
v_IncidentMaxWorkLog_Category ON v_Incident.IncidentID = v_IncidentMaxWorkLog_Category.Incident_Number
WHERE (v_Incident.Summary LIKE N'Intelligent Incident for Impacted CI%') AND (v_Incident.CustomerFirstName = N'BMC') AND
(v_Incident.CustomerLastName = N'Impact Manager') AND (v_IncidentMaxWorkLog_Category.WORK_LOG_TYPE IN ('General Information', 'Incident Task / Action',
'Working Log')) AND (v_Incident.ReportedDate >= '7/1/2014') AND (v_Incident.ReportedDate >= DATEADD(year, - 1, GETDATE()))
GROUP BY v_Incident.ReportedDate
FROM v_Incident
第二次查询
SELECT DISTINCT v_Incident.ReportedDate, COUNT(*) AS Count
FROM v_Incident INNER JOIN
v_IncidentMaxWorkLog_Category ON v_Incident.IncidentID = v_IncidentMaxWorkLog_Category.Incident_Number
WHERE (v_Incident.Summary LIKE N'Intelligent Incident for Impacted CI%') AND (v_Incident.CustomerFirstName = N'BMC') AND
(v_Incident.CustomerLastName = N'Impact Manager') AND (v_Incident.ReportedDate >= '7/1/2014') AND (v_Incident.ReportedDate >= DATEADD(year, - 1, GETDATE()))
GROUP BY v_Incident.ReportedDate
FROM v_Incident
正如你所看到的,我正在拉下报告的日期和计数。如果我只是将这两个语句放在一个语句中[select(query1)as filtered,(query2)as base]并将组移到外面,我得到结果,但它是整个每一行的每个值,以及大约10k行太多。
是否有一个简单的解决方案可以获取每个日期,并使用这两个或类似的查询进行基数和过滤的单一计数?非常感谢你提前!
答案 0 :(得分:1)
根据我的想法,我将使用UNION并将联合查询视为subquery。
SELECT *
FROM
((SELECT DISTINCT
v_Incident.ReportedDate, COUNT(*) AS Count, 'BASE' as [Designator]
FROM
v_Incident
INNER JOIN
v_IncidentMaxWorkLog_Category ON v_Incident.IncidentID = v_IncidentMaxWorkLog_Category.Incident_Number
WHERE
(v_Incident.Summary LIKE N'Intelligent Incident for Impacted CI%')
AND (v_Incident.CustomerFirstName = N'BMC')
AND (v_Incident.CustomerLastName = N'Impact Manager')
AND (v_IncidentMaxWorkLog_Category.WORK_LOG_TYPE IN ('General Information', 'Incident Task / Action', 'Working Log'))
AND (v_Incident.ReportedDate >= '7/1/2014')
AND (v_Incident.ReportedDate >= DATEADD(year, - 1, GETDATE()))
GROUP BY
v_Incident.ReportedDate
FROM v_Incident)
UNION ALL
(SELECT DISTINCT v_Incident.ReportedDate, COUNT(*) AS Count, 'Filtered' as [Designator]
FROM v_Incident INNER JOIN
v_IncidentMaxWorkLog_Category ON v_Incident.IncidentID = v_IncidentMaxWorkLog_Category.Incident_Number
WHERE (v_Incident.Summary LIKE N'Intelligent Incident for Impacted CI%') AND (v_Incident.CustomerFirstName = N'BMC') AND
(v_Incident.CustomerLastName = N'Impact Manager') AND (v_Incident.ReportedDate >= '7/1/2014') AND (v_Incident.ReportedDate >= DATEADD(year, - 1, GETDATE()))
GROUP BY v_Incident.ReportedDate
FROM v_Incident)) ORDER BY ReportedDate, Designator
答案 1 :(得分:1)
select isnull(a.ReportedDate, b.ReportedDate) ReportedDate,
a.Count ACount, b.Count BCount
from
(
SELECT DISTINCT v_Incident.ReportedDate, COUNT(*) AS Count
FROM v_Incident INNER JOIN
v_IncidentMaxWorkLog_Category ON v_Incident.IncidentID = v_IncidentMaxWorkLog_Category.Incident_Number
WHERE (v_Incident.Summary LIKE N'Intelligent Incident for Impacted CI%') AND (v_Incident.CustomerFirstName = N'BMC') AND
(v_Incident.CustomerLastName = N'Impact Manager') AND (v_IncidentMaxWorkLog_Category.WORK_LOG_TYPE IN ('General Information', 'Incident Task / Action',
'Working Log')) AND (v_Incident.ReportedDate >= '7/1/2014') AND (v_Incident.ReportedDate >= DATEADD(year, - 1, GETDATE()))
GROUP BY v_Incident.ReportedDate
FROM v_Incident
) a
full outer join
(
SELECT DISTINCT v_Incident.ReportedDate, COUNT(*) AS Count
FROM v_Incident INNER JOIN
v_IncidentMaxWorkLog_Category ON v_Incident.IncidentID = v_IncidentMaxWorkLog_Category.Incident_Number
WHERE (v_Incident.Summary LIKE N'Intelligent Incident for Impacted CI%') AND (v_Incident.CustomerFirstName = N'BMC') AND
(v_Incident.CustomerLastName = N'Impact Manager') AND (v_Incident.ReportedDate >= '7/1/2014') AND (v_Incident.ReportedDate >= DATEADD(year, - 1, GETDATE()))
GROUP BY v_Incident.ReportedDate
FROM v_Incident
) b on b.ReportedDate = a.ReportedDate
这会处理您的每个原始查询,并将其作为表格进行处理。到ReportedDate连接它们的第三个查询。它使用Full Outer Join,因此如果缺少b,仍将检索a,如果缺少a,则仍然会报告b。这种方式有3种组合:
请记住,在任何SQL Select中,您都可以使用Select查询替换表名。
答案 2 :(得分:1)
您的查询非常相似。看起来您有一个总计数,然后是一些具有特定工作日志类型的记录的部分计数。这是一个返回date,BaseCount和'WorkLogFilteredCount'的查询.SSRS应该能够将日期作为轴,而其他两列作为单独的系列非常容易用于您的图表。
SELECT v_Incident.ReportedDate
, COUNT(*) AS BaseCount
, SUM(CASE WHEN v_IncidentMaxWorkLog_Category.WORK_LOG_TYPE IN ('General Information', 'Incident Task / Action', 'Working Log') THEN 1 ELSE 0 END) AS WorkLogFilteredCount
FROM v_Incident
INNER JOIN v_IncidentMaxWorkLog_Category
ON v_Incident.IncidentID = v_IncidentMaxWorkLog_Category.Incident_Number
WHERE (v_Incident.Summary LIKE N'Intelligent Incident for Impacted CI%')
AND (v_Incident.CustomerFirstName = N'BMC')
AND (v_Incident.CustomerLastName = N'Impact Manager')
AND (v_Incident.ReportedDate >= '7/1/2014')
AND (v_Incident.ReportedDate >= DATEADD(year, - 1, GETDATE()))
GROUP BY v_Incident.ReportedDate
FROM v_Incident