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 |
|
jcw12000
Starting Member
5 Posts |
Posted - 2011-11-11 : 04:57:49
|
| Hi I need helpI have a table with invoice transactions in it.I need to show only the transactions that form 80% of the total value of this invoice transaction table. example there is 100 invoice that total a amount of $1204745 but only the top 20 transactions make up 80% of the total value. |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-11-11 : 05:03:45
|
| Table definition?--Gail ShawSQL Server MVP |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-11 : 05:04:20
|
| [code];with cteas(SELECT row_number() over (order by invoiceamount desc) as rn,sum(invoiceamount) over () as totalinvamt,*FROM table)select c1.*from cte c1cross apply (select sum(invoiceamount) as totalamt from cte where rn < = c1.rn )c2where totalamt <= 0.8 * totalinvamt[/code]make sure you use actual column names (i've just given indicative ones)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jcw12000
Starting Member
5 Posts |
Posted - 2011-11-11 : 05:17:13
|
| that seems to work for that part of my report.The next part needs to show the transactions making up 80% of the total vale the same as the above query but per week. I have a column Creation_Week that make the date the invoice is loaded the same weeks friday date. |
 |
|
|
jcw12000
Starting Member
5 Posts |
Posted - 2011-11-11 : 05:43:20
|
| this part I do get right:;with cteas(SELECT row_number() over (PARTITION BY [Creation Week] order by [Claim Amount] desc) as rn,sum([Claim Amount]) over (PARTITION BY [Creation Week] ) as totalinvamt,*FROM Step1_Top80)but now I cant get the select statement right |
 |
|
|
jcw12000
Starting Member
5 Posts |
Posted - 2011-11-11 : 05:55:25
|
| Got it fixed thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-11 : 06:03:12
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|