Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
nizguy
Starting Member
37 Posts |
Posted - 2009-10-29 : 18:32:34
|
| Hello, Can anyone help me with this.How to find the percentage of each individual weight.I have a simple bill table:BillNum as char 9,Weight as intAnd my sql querySelect BillNum, WeightFrom BillReturnBill# Weight 290180579 21014 290202843 1899 290559426 277 290750356 1432 290763900 292 290790720 320 I want to re-write my sql to return a new column Wt% asBill# Weight Wt%290180579 21014 0.832765290202843 1899 0.075256290559426 277 0.010977290750356 1432 0.056749290763900 292 0.011572290790720 320 0.012681the Wt% is calculate as follow:Wt% = individual weight / sum of all bills Weighti.e sum(weight) = 25234 21014 / 25234 = 0.832765 1899 / 25234 = 0.075256 277 / 25234 = 0.010977 etc... |
|
|
PeterNeo
Constraint Violating Yak Guru
357 Posts |
Posted - 2009-10-30 : 06:03:53
|
If u r using SQL2005 or later u can use[CODE]SELECT BillNum, Weight, (Weight*1.0000)/SUM(Weight) OVER() AS "Wt%" FROM Bill[/CODE]if in 2000[CODE]SELECT BillNum, Weight, (Weight*1.0000)/ (SELECT SUM(Weight) FROM Bill) AS "Wt%" FROM Bill[/CODE]"There is only one difference between a dream and an aim.A dream requires soundless sleep to see,whereas an aim requires sleepless efforts to achieve..!!" |
 |
|
|
nizguy
Starting Member
37 Posts |
Posted - 2009-10-30 : 15:32:58
|
| PeterNeo, It works great. thank you for you help!I want to add one more column to my result set, (the "Revenue" column)Revenue = sum(amount) * Wt%i.e6600 * .83276537 = 5496.251096600 * .075255608 = 496.68700966600 * .010977253 = 72.44986922etc....Is that possible? thank youBill#________Weight__amount__Wt%________Revenue290180579__21014__6000__0.83276537___5496.25109290202843__1899___500___0.075255608__496.6870096290559426__277____100___0.010977253__72.44986922290750356__1432___0000__0.056748831__374.5422842290763900__292____0000__0.011571689__76.37314734290790720__320____0000__0.012681303__83.69659983 |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2009-10-30 : 16:04:27
|
| Yes, (sum(amount) * ( [SUBSTITUE FORMULA FROM ABOVE] )) as Revenue"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
nizguy
Starting Member
37 Posts |
Posted - 2009-11-02 : 11:00:03
|
| Thanks jhocutt, I tried this and it gave me error. Is something wrong with my last query?use Northwindgocreate table #testBill (BillNum char(9), Weight int,Amount money)select * from #testbillinsert #testbill (BillNum, Weight,Amount) VALUES ('290180579', 21014, 6000)insert #testbill (BillNum, Weight,Amount) VALUES ('290202843', 1899, 500)insert #testbill (BillNum, Weight,Amount) VALUES ('290559426', 277, 100)insert #testbill (BillNum, Weight,Amount) VALUES ('290750356', 1432, 0)insert #testbill (BillNum, Weight,Amount) VALUES ('290763900', 292, 0)insert #testbill (BillNum, Weight,Amount) VALUES ('290790720', 320, 0)select billNum, Weight, Amount, (Weight*1.0000) / (select sum(weight) from #testbill) as "Wt%"from #testBillselect billNum, Weight, Amount, (Weight*1.0000) / (select sum(weight) from #testbill) as "Wt%", (sum(amount) * (Weight*1.0000) / (select sum(weight) from #testbill))from #testBill |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2009-11-02 : 11:17:18
|
| SELECT BillNum, Weight, (Weight*1.0000)/ a.SumWeight AS [Wt%] , SumAmount*((Weight*1.0000)/ a.SumWeight) as RevenueFROM #testBill , (select sum(Weight) as SumWeight, Sum(Amount) as SumAmount from #testBill) ago"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
nizguy
Starting Member
37 Posts |
Posted - 2009-11-02 : 17:21:20
|
| Work great! Thank you for both of your help. |
 |
|
|
nizguy
Starting Member
37 Posts |
Posted - 2009-11-03 : 18:30:09
|
| Hi Guys, I need more HELP on this query.First, I add Zipcode char(5) column to #testBill tablecreate table #testBill (BillNum char(9), Weight int,Amount money,Zipcode char(5))insert #testbill (BillNum, Weight,Amount,Zipcode) VALUES ('290180579', 21014, 6000,'90012')insert #testbill (BillNum, Weight,Amount,Zipcode) VALUES ('290202843', 1899, 500,'90012')insert #testbill (BillNum, Weight,Amount,Zipcode) VALUES ('290559426', 277, 100,'90720')insert #testbill (BillNum, Weight,Amount,Zipcode) VALUES ('290750356', 1432, 0,'90720')insert #testbill (BillNum, Weight,Amount,Zipcode) VALUES ('290763900', 292, 0,'91395')insert #testbill (BillNum, Weight,Amount,Zipcode) VALUES ('290790720', 320, 0,'91395')Next, I create a new table #Area Costcreate table #AreaCost(Zipcode char(5),Area char(1),Min money,Max money)insert #AreaCost (Zipcode, Area, Min, Max) VALUES ('90012','A', 9, 200)insert #AreaCost (Zipcode, Area, Min, Max) VALUES ('90720','B', 9.50, 250)insert #AreaCost (Zipcode, Area, Min, Max) VALUES ('91395','C', 10.50, 300.50)AreaCost result90012 A___9.0000__200.000090720 B___9.5000__250.000091395 C__10.5000__300.5000SELECT BillNum, Weight, Amount, (Weight*1.0000)/ a.SumWeight AS [Wt%] , SumAmount*((Weight*1.0000)/ a.SumWeight) as Revenue, c.area , (SumAmount*((Weight*1.0000)/ a.SumWeight)*.12) as CostFROM #testBill tb inner join #AreaCost c on tb.zipcode = c.zipcode, (select sum(Weight) as SumWeight, Sum(Amount) as SumAmount from #testBill) a gothe result from this query290180579__21014__6000.0000__.832765316636284__5496.2510897995__A__659.550130776290202843__1899___500.0000__.075255607513672___496.6870095902___A__59.602441151290559426__277____100.0000__.010977252912736___72.4498692241____B__8.693984307290750356__1432___.0000_____.056748830942379___374.5422842197___B__44.945074106290763900__292____.0000_____.011571688991043___76.3731473409____C__9.16477681290790720__320____.0000_____.012681303003883___83.6965998256____C__10.043591979how do I overwrite the Min/Max from the #AreaCost table if appliedI want the result look like this290180579__21014__6000.0000__.832765316636284__5496.2510897995__A__200__(Should overwrite with 200 Max in area A #AreaCost table)290202843__1899___500.0000__.075255607513672___496.6870095902___A__59.602441151290559426__277____100.0000__.010977252912736___72.4498692241____B__9.50__(Should overwrite with 9.50 Min in Area B) 290750356__1432___.0000_____.056748830942379___374.5422842197___B__44.945074106290763900__292____.0000_____.011571688991043___76.3731473409____C__10.50__(Should overwrite with 10.50 Min in Area C)290790720__320____.0000_____.012681303003883___83.6965998256____C__10.50__(Should overwrite with 10.50 Min in Area C)Appreciate your help |
 |
|
|
|
|
|
|
|