我一直在寻找文档和示例,并尝试进行这项工作,但是没有运气,所以我希望有人可以指出正确的方向。
我通过基于月的查询从名为“维护”的表中获得前5个项目,基本上给了我本月呼叫次数最多的前5个节点。
前任。前5名选择
select top 5
maint.node_id
,maint.sc_tot
FROM
server.dbo.maintenance as maint
where
maint.province_name='provname
and
maint.system_code='syscode'
and
maint.city_name='cityname'
and
(
year(maint.startdate)=2015
and
month(maint.startdate)=07
)
group by
maint.node_id
,maint.sc_tot
order by
sum(isnull(maint.je_tot,0)+isnull(maint.sc_tot,0)+isnull(maint.tt_tot,0))
desc
输出为
node_id sc_tot节点1 30节点2 28 节点3 27节点4 23 节点5 23
现在,我需要为没有时间范围的每个节点选择当月的呼叫总数。基本上从同一维护表中为我提供了每个节点的历史记录以及最终的趋势。
前任。快速摘要(不是确切的详细信息)
节点STARTDATE sc_tot 节点1年01月10 节点1年02月15 节点1擦伤36 节点2年1月14 节点2 2月22日 等...
然后,此查询将在sql报表构建器中用于某些报表和图形显示,但是只要我具有我擅长的值即可。
仅使用一个SQL查询就可以做到吗?
未测试...
也许像...
我认为您正在追随2015年7月呼叫量最高的前5个节点,然后您希望按月/年按系统中所有前5个节点的所有节点的节点历史记录。
WITH CTE AS (
SELECT top 5 a.node_id, a.sc_tot
FROM server.dbo.maintenance as a
WHERE a.province_name='provname'
and a.system_code='syscode'
and a.city_name='cityname'
and year(a.startdate)=2015
and month(a.startdate)=07
GROUP BY a.node_id ,a.sc_tot
order by sum(isnull(a.je_tot,0)+isnull(a.sc_tot,0)+isnull(a.tt_tot,0)) desc)
SELECT TopRec.node_ID, sum(maint.sc_tot) as SumOfCalls,
year(maint.startdate) as YR, month(maint.startdate) as MO
FROM CTE as TopRec
INNER JOIN server.dbo.maintenance as maint
on CTE.Node_Id = Maint.Node_ID
GROUP BY TopRec.node_ID, year(maint.startdate), month(maint.startdate)
基本上与此(内联视图)相同:但是使用更新的技术...您可以看到为什么人们更喜欢它以提高可读性。
SELECT TopRec.node_ID, sum(maint.sc_tot) as SumOfCalls,
year(maint.startdate) as YR, month(maint.startdate) as MO
FROM (
SELECT top 5 a.node_id, a.sc_tot
FROM server.dbo.maintenance as a
WHERE a.province_name='provname'
and a.system_code='syscode'
and a.city_name='cityname'
and year(a.startdate)=2015
and month(a.startdate)=07
GROUP BY a.node_id ,a.sc_tot
order by sum(isnull(a.je_tot,0)+isnull(a.sc_tot,0)+isnull(a.tt_tot,0)) desc))
as TopRec
INNER JOIN server.dbo.maintenance as maint
on CTE.Node_Id = Maint.Node_ID
GROUP BY TopRec.node_ID, year(maint.startdate), month(maint.startdate)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句