Carregar apresentação
A apresentação está carregando. Por favor, espere
PublicouIsabela Inocencio Alterado mais de 9 anos atrás
1
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:")
2
Patrocinadores
3
Agenda Set Based vs Row by Row O que são windows functions? Novas funções implementadas no SQL2012 O que ainda falta? Window Frame Demos Perguntas e respostas
4
Set Based vs Row by Row
5
O que sã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
6
Clausula OVER 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 Default WINDOW FRAME ORDER BY função de agregação + partition by + order by + window frame
7
SQL Server 2012 Suporte quase completo a clausula OVER() Order by Partition By Window frame Novas functions: LEAD(), LAG(), FISRT_VALUE(), LAST_VALUE(), CUME_DIST(), PERCENT_RANK(), PERCENTILE_CONT(), PERCENTILE_DISC()
8
Window Frame [ROWS | RANGE] BETWEEN AND UNBOUNDED PECEDING: Window inicia na primeira linha da partição. CURRENT ROW: Window inicia na linha atual. PRECEDING ou FOLLOWING UNBOUNDED FOLLOWING: Window termina na última linha da partição. CURRENT ROW: Window termina na linha atual. PRECEDING ou FOLLOWING
9
Window Frame, duas “janelas” USE NorthWind GO SELECT OrderID, CustomerID FROM Orders WHERE CustomerID IN (1,2) ORDER BY CustomerID
10
Window Frame, “janelas” coexistem
11
Window Frame – First_Value SELECT OrderID, CustomerID, FIRST_VALUE(OrderID) OVER(PARTITION BY CustomerID ORDER BY OrderID) AS FirstOrderID FROM Orders WHERE CustomerID IN (1,2)
12
Window Frame – Last_Value SELECT OrderID, CustomerID, LAST_VALUE(OrderID) OVER(PARTITION BY CustomerID ORDER BY OrderID) AS FirstOrderID FROM Orders WHERE CustomerID IN (1,2)
13
Window Frame – Last_Value …LAST_VALUE(OrderID) OVER(PARTITION BY CustomerID ORDER BY OrderID ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS FirstOrderID...
14
Demonstração
15
O que ainda falta no SQL2012? Funções: FIRST, retorna o primeiro valor de um grupo ordenado MAX(Cidade) KEEP (DENSE_RANK FIRST ORDER BY SUM(Valor_Pedido))* LAST: último valor de um grupo ordenado MIN(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 SELECT LAG(Col1) OVER MinhaWin AS Col1 FROM Tabela1 WINDOW MinhaWin AS (ORDER BY Coluna1 ROWS 2 PRECEDING)
16
Recursos Treinamentos Sr.Nimbus http://www.srnimbus.com.br/ http://www.srnimbus.com.br/ Artigos simple-talk: http://tinyurl.com/SQLSat100-WF1 http://tinyurl.com/SQLSat100-WF1 http://tinyurl.com/SQLSat100-WF2 http://tinyurl.com/SQLSat100-WF2 Artigos meu blog: http://blogfabiano.com/category/windows- functions/ http://blogfabiano.com/category/windows- functions/ Video SQL Bits IX Dave Ballantyne http://www.sqlbits.com/Sessions/Event9/Whats_n ew_in_Denali-TSQL http://www.sqlbits.com/Sessions/Event9/Whats_n ew_in_Denali-TSQL
17
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')) AS Tab("Sobre mim:")
Apresentações semelhantes
© 2024 SlidePlayer.com.br Inc.
All rights reserved.