Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 1st, 2011, 01:45 PM
Registered User
 
Join Date: Jul 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default sql need help on query

8rb,6rb,3rb,5rb,4rb

hello everybody...
i do need help about sql query..
this is the problem, for example i have table like this:

no | name |sale
--------------
1 doni 5000
2 jack 3000
3 owen 4000
4 luiz 8000
5 dinho 6000

and then i need to find peoples that give 80% of total sale??
total sale is 26,000. so 80% of it should hold value about 20,800.
so the result should look like this:
no | name |sale
--------------
1 doni 8000
2 jack 6000
3 owen 5000
4 luiz 4000

total of that is 23,000, and that meet the condition.
i'm trying to limit condition based on 80% of total but it doesnt work...

select * from table where sum(sale) < 0.8 * sum(sale) order by sale desc

i hope that query will stop when total is still 80% of total_sale.
but it doesnot work...
i really want to know about how to solve this problem...
i do hope every body can help me...
thanks before...
 
Old July 2nd, 2011, 09:22 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Smile

well there r wasy to achieve that result although i dont know y u gonna do it
anyway, first thing comes to my mind is Cursor & function base
I myself dont recommend cursor for its performance, & also solve it simply
lets go with function
(I used ur field names)

first create a function
Code:
CREATE FUNCTION [dbo].[GetTotalSale] (@no int, @Sum int) RETURNS int AS BEGIN return (SELECT @Sum+SUM(sale) from aaaa WHERE no <= @no)
END
then:
Code:
DECLARE @D bigint
SET @D = 0

SELECT *
FROM (
	select * ,dbo.GetTotalSale(no, @D) Sm
	from table) Tb
where Sm < 26000
HTH, let me now about it
__________________
Always,
Hovik Melkomian.





Similar Threads
Thread Thread Starter Forum Replies Last Post
sql query ldp101068 SQL Server 2000 7 December 18th, 2007 04:52 PM
Output Query to txt file from SQL Query everest SQL Server 2005 4 November 22nd, 2007 01:49 AM
SQL Query!! dpkbahuguna Beginning VB 6 5 October 12th, 2007 12:39 AM
sql query vohra_vikas SQL Server 2000 2 September 28th, 2004 07:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.