|
Hi All... I want to write an SQL Query....Please help me... I have a table Batch Ledger which consist of 3 Fields(Batch No.,Qty. and Rate). The table consists of around 200 records.. Now i want those 40 records whose quantity when summed and then divide by 40 should lie between 100-200 and Rate when summed and divide lie between 10-15.. .Please help... I am also providing a sample data of 15 Batch No. with their qty and Rate. Now i want 5 Batch No. whose (Sum of qty) /5 lies in between 110-115 and Rate in between 10-15.. Lets take an example...I have 16 records in a table... Now I WANT TO GET 5 Batch No. whose avg. of qty lies in b/w 110-115 and Rate in between 10-15.. Here is the data and below there are some possible solutions.. Because the avg. comes is 113.6 and 13.6 ...
(comments are locked)
|
|
I am not sure if this is the answer that you want as you have mentioned a lot of requirements (SUMs AVERAGEs and so on) but you should be able to work from this: Now if you want to test the values of the average columns and filter if they are between certain values then you may do this with a simple Please note that I have used a different set of data from you as your sample gave no chance of returning @Fatherjack - I think within the OPs data that Batch is a PK (your data allows duplicates) and he is looking to select something a little more complicated than you have assumed. My understanding: check the first 5 and see if
Nov 24 '10 at 01:58 PM
Blackhawk-17
yeah, I figured that after Williams comment but as there has been no response we are all a little in the dark...
Nov 24 '10 at 02:04 PM
Fatherjack ♦♦
@Blackhawk - that is what I was trying to say this morning. It was before coffee and I'm not sure it was so clear. I think it will be terrible in terms of performance - self joins with inequalities to achieve what you want or something like that. My brain doesn't want to really consider a solution because it is basically a terrible problem to solve - brute force only and will not be nice at all.
Nov 24 '10 at 02:43 PM
WilliamD
@William - sorry about attributing your comment to Cyborg (saw comment and picture...) The only bright side of this is the OP stating there are only 200 rows in the table :)
Nov 25 '10 at 05:10 AM
Blackhawk-17
@Blackhawk - no problem about attribution. The silver lining of only 200 rows,however , is miniscule.
Nov 25 '10 at 06:14 AM
WilliamD
(comments are locked)
|
|
This is my solution to the problem. Running this query produces 140,760 results. Note that this produced permutations, so rows representing the same combination will appear several times e.g. combination (D-0001, D-0002, D-0003, D-0004, D-0006) and combination (D-0001, D-0002, D-0003, D-0006, D-0004). And that's with only 15 rows! Imagine the chugging with 200 or more.
Nov 25 '10 at 05:12 AM
Blackhawk-17
(comments are locked)
|
|
I've amended the query above to produce combinations instead of permutations. This yields 1173 results and executes a lot faster than the previous one! The next step is to make the code generic to take an arbitray number of batch numbers...
(comments are locked)
|
|
Ok, Here is my first attempt - it works very happily for groups of 10 or less, after that it starts to take a bit of time... Sample output : 93
(comments are locked)
|


I may be mistaken, or does this not sound like an evil problem à la the travelling salesman problem? If so, that is logartihmic complexity and will be a performance dog!
You will have to take each discrete value in your table, sum it with 39 other records and divide by 40, check if this value is valid, then find the next 39 records except the first value out of the previous 39 from the fist iteration - rinse and repeat.
Do you have to do it this way, or have I totally misunderstood the problem?
Do you just need 1 result , some results or ALL possible combinations?