Solved

selecting rows base on sum of field

Posted on 2014-04-25
7
348 Views
Last Modified: 2014-05-10
hi i have one table like this:

TBPRODUCT( ID AS INTEGER, PRODUCT AS STRING, PRICE AS DOUBLE )

this table have 500 rows and i want to select 6 products at a time that mach sum of price = 150.

and on the second time i select i don't want the same products i select on the first time.

how can i accomplish this?
0
Comment
Question by:rafaelrgl
7 Comments
 
LVL 22

Accepted Solution

by:
plusone3055 earned 167 total points
Comment Utility
can you please explain what the purpose of this function is ?

I understand what your asking for

500 Rows

select 6 rows that will total a price of 150
select another 6 rows (not including the previously selected 6 rows) also totaling 150

Can you elaborate on the purpose for this equation ?
0
 
LVL 1

Author Comment

by:rafaelrgl
Comment Utility
i need to create kits to sell that total 150 each an each kit must be unique.
0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 167 total points
Comment Utility
We understand what you're trying to do, but I'm not sure you can do this with pure sql. What are the minimum and maximum prices? Do the contain cents or are they even dollars? What is the average price? Can you post just the product_id and price (we don't need to know what the products are) for the 500 row table? Is there any tolerance (e.g. would one kit of 149.95 and another of 150.05 be acceptable)?  I also assume you would want the maximum number of kits that can be created.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Assisted Solution

by:plusone3055
plusone3055 earned 167 total points
Comment Utility
awking:
I agree with you as well
I could see a for loop done with .NET
or would a pivot table work here ?
0
 
LVL 26

Assisted Solution

by:skullnobrains
skullnobrains earned 83 total points
Comment Utility
in order to "select rows based on a field's sum" you would use something like this in sql

select sum(field) as sum, ...
from table
group by whatever
having sum=150

but then given what is understand from your goals this will not help since sql will not try every possible grouping by itself
0
 
LVL 35

Assisted Solution

by:David Todd
David Todd earned 83 total points
Comment Utility
Hi,

to do as requested with pure SQL would require 5 cross joins, producing a table that is 500^6 rows!

And who said those kits would make sense?

Imagine, if you will, a combined plumbers and electrical merchant - we had a national chain doing this in my neck of the woods, but for the purposes of illustration please bear with me - what sense does it make to build a kit out of an oven and some low rated fuse wire and a toilet cistern flush valve?

And generally, kits are sold as such for the whole costing less than the individual parts, so there is some rounding involved as well.

Regards
  David
0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 167 total points
Comment Utility
Can you post just the prices? There may be a means of at least approximating a sum of $150 for kits of six products, but that possibility diminishes if there can be a price of $145, for example, or if the average product price is no where near $25.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now