sql server - t-sql select all values based on top 5 query -
i've been hunting through documentation , examples , playing try , make work having no luck i'm hoping might point me in right direction.
i have top 5 items table called maintenance via query based month, giving me top 5 nodes highest calls month.
ex. top 5 select
select top 5 maint.node_id ,maint.sc_tot server.dbo.maintenance maint maint.province_name='provname , maint.system_code='syscode' , maint.city_name='cityname' , ( year(maint.startdate)=2015 , month(maint.startdate)=07 ) group maint.node_id ,maint.sc_tot order sum(isnull(maint.je_tot,0)+isnull(maint.sc_tot,0)+isnull(maint.tt_tot,0)) desc
output is
node_id sc_tot node1 30 node2 28 node3 27 node4 23 node5 23
what need select sum of calls month each of nodes without time frame. giving me history , trend each node, same maintenance table.
ex. quick summary(not exact details)
node startdate sc_tot node1 jan 10 node1 feb 15 node1 mar 36 node2 jan 14 node2 feb 22 etc....
this query used in sql report builder reporting , graphing, long have values i'm good.
is possible single sql query?
untested...
perhaps like...
i think you're after top 5 nodes highest call volume in july of 2015, , want node history month/year of in system top 5 nodes.
with cte ( select top 5 a.node_id, a.sc_tot server.dbo.maintenance a.province_name='provname' , a.system_code='syscode' , a.city_name='cityname' , year(a.startdate)=2015 , month(a.startdate)=07 group a.node_id ,a.sc_tot order 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) sumofcalls, year(maint.startdate) yr, month(maint.startdate) mo cte toprec inner join server.dbo.maintenance maint on cte.node_id = maint.node_id group toprec.node_id, year(maint.startdate), month(maint.startdate)
this same (inline view): using newer technique... can see why people prefer readability.
select toprec.node_id, sum(maint.sc_tot) sumofcalls, year(maint.startdate) yr, month(maint.startdate) mo ( select top 5 a.node_id, a.sc_tot server.dbo.maintenance a.province_name='provname' , a.system_code='syscode' , a.city_name='cityname' , year(a.startdate)=2015 , month(a.startdate)=07 group a.node_id ,a.sc_tot order sum(isnull(a.je_tot,0)+isnull(a.sc_tot,0)+isnull(a.tt_tot,0)) desc)) toprec inner join server.dbo.maintenance maint on cte.node_id = maint.node_id group toprec.node_id, year(maint.startdate), month(maint.startdate)
Comments
Post a Comment