0

产品名称 利润

戴尔Vostro--------15714

诺基亚510---------13392

索尼E151----------10506

LGE15-------------10326

诺基亚20------------8830

三星1516--------7910

三星1556-----7560

三星1517-----7430

福特GT49--------7410

三星1560-----7350

…………………………………………………………………………………………
_

我有这样两列产品名称和利润的表格,所以我想显示最多的产品,它为此提供了总利润或收入的 50% 我使用 Sql server 存储过程实现了解决方案,但我希望使用相同的解决方案来完成R 或 GreenPlum

实际查询是

“找出去年,自今年年初以来,以及总共产生 50% 年利润/收入的顶级产品。”

我在 sql server 中实现的逻辑是

 create proc [dbo].[prod50pershare] as begin

CREATE TABLE #LocalTempTable(
productname varchar(150),[SumSold] int)

CREATE TABLE #LocalTempTable1(
productname varchar(150),[SumSold] int)

insert #LocalTempTable
select productname,sum(margin)As Profit  from sale
join product on product.productid=sale.productid
where datepurchase between '1/1/2012' and '1/1/2013' 
group by productname order by 2 desc

DECLARE @sum INT
Declare @count int
declare @50PerRevenue int
declare @pcount int
declare @i int

SET @sum = 0
set @i=1
select @50PerRevenue= sum(sumsold)/2 from #LocalTempTable
select @count=COUNT(*) from #LocalTempTable

WHILE (@i<=@count)
BEGIN
   insert #LocalTempTable1 select top (@i)productname,SumSold from #LocalTempTable  
   order  by [SumSold] desc
   select @sum=SUM(SumSold) from #LocalTempTable1

 if(@sum<=@50PerRevenue)
 begin
      set @pcount=@i
      delete from #LocalTempTable1
      set @sum=0
      Set @i=@i+1
 end
 else
     begin
     break
     end
End
select top (@i) Productname,Sumsold from #LocalTempTable1 
drop table #LocalTempTable
drop table #LocalTempTable1
end
4

1 回答 1

1

这是一种方法——

df <- read.csv(textConnection(
'DellVostro,15714
Nokia510,13392
SonyE151,10506
LGE15,10326
Nokia20,8830
samsung1516,7910
samsung1556,7560
samsung1517,7430
fordgt49,7410
samsung156,07350'), header = FALSE)

df <- df[order(df$V2,decreasing=TRUE),]
df$V3 <- cumsum(df$V2)
df$V3 <- df$V3/sum(df$V2)

V3 告诉你在哪里标记你的 50% -

> df
            V1    V2        V3
1   DellVostro 15714 0.1629610
2     Nokia510 13392 0.3018418
3     SonyE151 10506 0.4107935
4        LGE15 10326 0.5178786
5      Nokia20  8830 0.6094495
6  samsung1516  7910 0.6914797
7  samsung1556  7560 0.7698801
8  samsung1517  7430 0.8469324
9     fordgt49  7410 0.9237773
10  samsung156  7350 1.0000000
于 2013-12-06T04:56:16.963 回答