Inquiridor
Criando curva ABC no T-SQL

Pergunta
-
Boa tarde a todos,
Estou criando uma curva ABC no SQL SERVER.
Estive lendo o excelente artigo do Gustavo Maia http://gustavomaiaaguiar.wordpress.com/2009/10/02/o-principio-de-pareto-a-curva-abc-e-consultas-sql/ porém não consegui inserir a coluna %acumulado onde traz a soma das porcentagens acumulando-as. Alguém pode me ajudar? Segue abaixo meu fonte:
DECLARE @total DECIMAL(8,2) SET @Total=(SELECT SUM(SC6010.C6_VALOR) FROM SC6010 where SC6010.C6_TES IN('501','502','503','504','505','506','507','508','509','520') and SC6010.C6_DATFAT between GETDATE()-2 and GETDATE ()-1 and SC6010.D_E_L_E_T_ <> '*') select SC6010.C6_PRODUTO COD_PRODUTO, SC6010.C6_DESCRI DESCRICAO, SUM(SC6010.C6_QTDENT)QUANTIDADE, SUM(SC6010.C6_VALOR) VALOR_TOTAL, ROUND(SUM(SC6010.C6_VALOR)*100/@total,2) percentual from SC6010 where SC6010.C6_TES IN('501','502','503','504','505','506','507','508','509','520') and SC6010.C6_DATFAT between GETDATE()-2 and GETDATE ()-1 and SC6010.D_E_L_E_T_ <> '*' group by SC6010.C6_PRODUTO,SC6010.C6_DESCRI order by SUM(SC6010.C6_VALOR) DESC
Grato desde já.
Todas as Respostas
-
Olá,
A melhor coisa seria ter isso no relatório, se voce está usando o protheus, pergunte para a totvs qual a função.
Se for usar reporting services use a função RunningValue (ROUND(SUM(SC6010.C6_VALOR)*100/@total,2) percentual,Nothing)
Se não der, para fazer em transact, melhor coisa pra mim é por cursor, eu uso sempre o mesmo exemplo básico quando precisa. Peguei na net e segue aqui para voce só ajustar
-- Cria tabela do exemplo CREATE TABLE [dbo].[Sales] ( [TransactionID] [int] IDENTITY (1, 1) NOT NULL, [StoreID] [int] NOT NULL, [productID] [int] NOT NULL, [transactionTime] [datetime] NOT NULL, [amount] [money] NOT NULL ) ON [PRIMARY] GO ALTER TABLE [dbo].[Sales] WITH NOCHECK ADD CONSTRAINT [PK_Sales] PRIMARY KEY CLUSTERED ( [TransactionID] ) ON [PRIMARY] GO --Popula a tabela do exemplo set noCount on declare @i int set @i = 0 while @i < 100000 begin insert into dbo.Sales(StoreID, productId, transactionTime, amount) select 1, p.productID, dateAdd(minute, @i*-1, getDate()), case (p.ProductID) when 1 then 10 when 2 then 100 end from (select 1 as productID union all select 2 as productID) as p if @i % 1000 = 0 print @i set @i = @i+1 end set nocount off go set noCount on declare @i smallint set @i = 0 while @i < 1000 begin insert into dbo.Sales(StoreID, productId, transactionTime, amount) select 2, p.productID, dateAdd(minute, @i*-1, getDate()), case (p.ProductID) when 1 then 10 when 2 then 100 end from (select 1 as productID union all select 2 as productID) as p set @i = @i+1 end set nocount off go --Cria indice para ajudar CREATE INDEX [IX_Sales] ON [dbo].[Sales]([StoreID], [productID], [transactionID]) ON [PRIMARY] GO --Procedure que faz a mágica com o cursor: create procedure dbo.Sales_sel_by_StoreID_ProductID @StoreID int, @ProductID int as begin set noCount on declare @report table( transactionID int primary key clustered, transactionTime dateTime not null, amount money not null, total money not null ) declare runningTotalsCursor cursor for select transactionID, TransactionTime, Amount from dbo.Sales where StoreID = @StoreID and ProductID = @ProductID order by transactionID declare @transactionID int declare @transactionTime dateTime declare @amount money declare @total money set @total = 0 open RunningTotalsCursor while (0=0) begin fetch next from RunningTotalsCursor into @transactionID, @transactionTime, @amount if @@fetch_status <> 0 break set @total = @total + @amount insert into @report(transactionID, transactionTime, amount, total) values(@transactionID, @transactionTime, @amount, @total) end close runningTotalsCursor deallocate runningTotalsCursor select transactionID, transactionTime, amount, total from @report order by transactionID set noCount off end --Roda a procedure execute dbo.Sales_sel_by_StoreID_ProductID @storeID = 2, @productID = 1
Boa Sorte,
Miguel Salles Analista Programador BI- Sugerido como Resposta Junior Galvão - MVPMVP sábado, 13 de agosto de 2011 21:23