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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Only showing transactions that form part of 80%

Author  Topic 

jcw12000
Starting Member

5 Posts

Posted - 2011-11-11 : 04:57:49
Hi

I need help

I 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 Shaw
SQL Server MVP
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-11 : 05:04:20
[code]
;with cte
as
(
SELECT row_number() over (order by invoiceamount desc) as rn,sum(invoiceamount) over () as totalinvamt,*
FROM table
)

select c1.*
from cte c1
cross apply (select sum(invoiceamount) as totalamt
from cte
where rn < = c1.rn
)c2
where totalamt <= 0.8 * totalinvamt
[/code]

make sure you use actual column names (i've just given indicative ones)

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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.
Go to Top of Page

jcw12000
Starting Member

5 Posts

Posted - 2011-11-11 : 05:43:20
this part I do get right:

;with cte
as
(
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
Go to Top of Page

jcw12000
Starting Member

5 Posts

Posted - 2011-11-11 : 05:55:25
Got it fixed thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-11 : 06:03:12
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -