Графики в Microsoft SQL Server Management Studio:
Отображение каких-либо данных в виде графиков — это часто понятнее и нагляднее, чем таблицы.
Для того, чтобы представить какие-либо данные в виде того или иного графика существуют множество инструментов, начиная простого и мощного Excel, который почти всегда под рукой.
Но что если Excel не установлен или не доступен?
И даже если установлен — часто разработчик или администратор базы данных имеет некий набор скриптов, которые позволяют быстро оценить текущее состояние системы и создавать отдельный dashboard в Excel или где-то еще не хочется.
В общем — если очень хочется, то графики можно рисовать и в SSMS.
Допустим, есть некие процессы, прогресс выполнения которых хочется отслеживать.
Для выполнения примера ниже нужна база данных в режиме совместимости 110.
Можно выполнять и на SQL2008 и/или режиме совместимости 100, но тогда придется отказаться от функции BufferWithCurves() — линии графиков будут тонкими. Для этого замените строку @line.BufferWithCurves(1) на @line в конце скрипта:
declare @issue table( id int primary key, name varchar(800) not null ) -- Issues to monitor insert into @issue(name, id) values('Hang one', 0), ('Fast', 1), ('Normal', 2), ('Slow', 10), ('Strange', 12) declare @issuedata table( issue_id int not null, date_time datetime not null, total int not null, done int not null ) -- There can be any dates and values insert into @issuedata([issue_id], [date_time], [total], [done]) values (0, '2014-01-01', 100, 50), (0, '2014-01-04', 100, 50), (0, '2014-01-07', 100, 50), (0, '2014-11-10', 100, 50), (1, '2014-01-01', 300, 0), (1, '2014-01-04', 300, 50), (1, '2014-01-07', 300, 300), (1, '2014-11-10', 300, 300), (2, '2014-01-01', 300, 0), (2, '2014-03-01', 300, 50), (2, '2014-09-01', 300, 200), (2, '2014-11-10', 300, 300), (10, '2014-01-01', 300, 0), (10, '2014-03-01', 300, 10), (10, '2014-09-01', 300, 60), (10, '2014-11-10', 300, 80), (12, '2014-01-01', 300, 50), (12, '2014-03-01', 300, 330), (12, '2014-09-01', 300, 280), (12, '2014-11-10 08:00', 300, 150) declare @points table( issue_id int not null, date_time datetime not null, compl money not null primary key(issue_id, date_time) ) insert into @points( [issue_id], [date_time], [compl] ) select sd.[issue_id], sd.[date_time], 100*(convert(money, sd.[done])/sd.[total]) as compl from @issuedata as sd declare @min_dt datetime, @max_dt datetime -- Calculate time boundaries to scale spatial results graph select @min_dt = min( p.[date_time] ), @max_dt = max( p.[date_time] ) from @points p declare @graph table( name varchar(800) not null, line geometry null ) -- Prepare the border for the graph (with 3:1 ratio) insert into @graph values('border', geometry::STLineFromText('LINESTRING (0 0, 300 0, 300 100, 0 100, 0 0)', 0)) -- Go through issues declare C cursor local fast_forward for select id, name from @issue as i open C declare @issue_id int, @name varchar(800), @line geometry, -- Ratio for X axis @ratio money = datediff(mi, @min_dt, @max_dt)/300. while 1=1 begin fetch next from C into @issue_id, @name if @@fetch_status <> 0 break; -- Aggregate all points and convert them into a line geometry oinstance set @line = geometry::STLineFromText('LINESTRING ('+substring(convert(varchar(max), ( select ', ' + convert(varchar(20), datediff(mi, @min_dt, p.[date_time])/@ratio) + ' ' + convert(varchar(20), p.compl) from @points p where p.[issue_id] = @issue_id order by p.[date_time] for xml path('') )), 3, 2000000000)+')', 0); insert into @graph( [name], [line] ) values( @name, -- Extend lines for better visualization @line.BufferWithCurves(1) ) end -- Return data and enjoy select * from @graph
Автор: Дмитрий Костылёв
*** *** *** *** *** *** *** *** *** *** *** ***
Полезные Скрипты
Рубрика Проверь себя
Ссылка на наш канал YouTube