170 likes | 304 Views
SQL Saturday #100 Brazil. Windowing Functions no SQL Server 2012. SELECT * FROM ( VALUES ( 'Fabiano Neves Amorim' ), ( 'Sr.Nimbus – SQL Server MVP' ), ( 'fabiano.amorim@srnimbus.com.br | @mcflyamorim' ), ( 'http://blogfabiano.com' )) AS Tab ( "Sobre mim:" ). Patrocinadores. Agenda.
E N D
SQL Saturday #100 Brazil Windowing Functions no SQL Server 2012 SELECT* FROM (VALUES('Fabiano Neves Amorim'), ('Sr.Nimbus – SQL Server MVP'), ('fabiano.amorim@srnimbus.com.br | @mcflyamorim'), ('http://blogfabiano.com'))ASTab("Sobre mim:")
Agenda Set Based vs Row by Row O quesão windows functions? Novasfunçõesimplementadas no SQL2012 O queaindafalta? WindowFrame Demos Perguntas e respostas
O quesão windows functions? • Similar as funções de agregação • Retornam um valor a partir de dados agregados • Padrão SQL:2008 • Suportado no Oracle, DB2, PostegreSQL • It’s all about Sets
Clausula OVER função de agregação+ partition by + order by + window frame SELECT ID_Conta, DT_Lancamento, VL_Lancamento, SUM(VL_Lancamento) OVER(PARTITION BY ID_Conta ORDER BY DT_Lancamento RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM tbLancamentos GO PARTITION BY ORDER BY Default WINDOW FRAME
SQL Server 2012 • Suporte quasecompletoa clausula OVER() • Order by • Partition By • Window frame • Novas functions: • LEAD(), LAG(), FISRT_VALUE(), LAST_VALUE(), CUME_DIST(), PERCENT_RANK(), PERCENTILE_CONT(), PERCENTILE_DISC()
WindowFrame [ROWS | RANGE] BETWEEN <Start expr> AND <End expr> <Start expr> UNBOUNDED PECEDING: Windowinicia na primeira linha da partição. CURRENT ROW: Windowinicia na linha atual. <unsignedinteger literal> PRECEDING ou FOLLOWING <Endexpr> UNBOUNDED FOLLOWING: Windowtermina na última linha da partição. CURRENT ROW: Windowtermina na linha atual. <unsignedinteger literal> PRECEDING ou FOLLOWING
WindowFrame, duas “janelas” USE NorthWind GO SELECT OrderID, CustomerID FROM Orders WHERE CustomerID IN (1,2) ORDER BY CustomerID
WindowFrame – First_Value SELECT OrderID, CustomerID, FIRST_VALUE(OrderID) OVER(PARTITION BY CustomerID ORDER BY OrderID) AS FirstOrderID FROM Orders WHERE CustomerID IN (1,2)
WindowFrame – Last_Value SELECT OrderID, CustomerID, LAST_VALUE(OrderID) OVER(PARTITION BY CustomerID ORDER BY OrderID) AS FirstOrderID FROM Orders WHERE CustomerID IN (1,2)
WindowFrame – Last_Value …LAST_VALUE(OrderID) OVER(PARTITION BY CustomerIDORDER BY OrderID ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS FirstOrderID...
O que ainda falta no SQL2012? SELECT LAG(Col1) OVER MinhaWin AS Col1 FROM Tabela1 WINDOW MinhaWin AS (ORDER BY Coluna1 ROWS 2 PRECEDING) • Funções: • FIRST, retorna o primeiro valor de um grupoordenadoMAX(Cidade) KEEP (DENSE_RANK FIRST ORDER BY SUM(Valor_Pedido))* • LAST: último valor de um grupoordenadoMIN(Cidade) KEEP (DENSE_RANK LAST ORDER BY SUM(Valor_Pedido))* • NULLs FIRST, NULLs LAST • OVER(ORDER BY Coluna1 NULLs FIRST) • Interval (Year, Month, Day, Hour, Minute, Second) • Window Clause * não standard
Recursos • Treinamentos Sr.Nimbus • http://www.srnimbus.com.br/ • Artigos simple-talk: • http://tinyurl.com/SQLSat100-WF1 • http://tinyurl.com/SQLSat100-WF2 • Artigosmeu blog: • http://blogfabiano.com/category/windows-functions/ • Video SQL Bits IX • Dave Ballantyne http://www.sqlbits.com/Sessions/Event9/Whats_new_in_Denali-TSQL
Dúvidas e brindes! SELECT"Sobre Mim:" FROM (VALUES('Fabiano Neves Amorim'), ('Sr.Nimbus – SQL Server MVP'), ('fabiano.amorim@srnimbus.com.br|@mcflyamorim'), ('http://blogfabiano.com'))ASTab("Sobre mim:") ?