tags:

views:

173

answers:

4
+1  A: 

I have no idea what step 3 means, but all the other steps can be done in SQL, also, they probably should be done in SQL where its a lot more efficient.

I assume from your tags that you're using a MySQL database (it would have been helpful to know).

My initial thots would be somethnig along the lines of:

 SELECT outdata.Pos,
     outdata.OutData,
     outdata.Warn,
     MIN(outdata.Mismatch),    
     outdata.Producer,
     outdata.Depl,
     outdata.DeplOffMax,        
     outdata.DeplOffMin,
     outdata.Axis
     outdata.Connection
 FROM outdata, indata
 WHERE
   (//some nasty type juggling here
     outdata.depl='*'
     OR outdata.deploffmin='*'
     OR indata.depl='*'
     OR (
        outdata.depl<>'*'
        AND outdata.deploffmin<>'*'
        AND indata.depl<>'*'
        AND outdata.depl-outdata.deploffmin>indata.depl
     )
   )  
 AND ( outdata.outdata=indata.outdata
   OR outdata.outdata='*'
   OR indata.outdata='*' )
 AND ( outdata.connection=indata.connection
   OR outdata.connection = '*'
   OR indata.connection='*' )
 AND (outdata.axis=indata.axis
   OR outdata.axis='*' 
   OR indata.axis='*' )
 ....repeat for all the fields you want to match       
 GROUP BY outdata.Pos,
     outdata.OutData,
     outdata.Warn,    
     outdata.Producer,
     outdata.Depl,
     outdata.DeplOffMax,        
     outdata.DeplOffMin,
     outdata.Axis
     outdata.Connection
 ORDER BY 4 ASC;

Simples.

C.

symcbean
Thanks, will look inteo the answer! As for step #3, that is what gets me from result after filter #2 to the lines under Print for each example. It utputs the field OutData from Pos=10 then from Pos=20 and Pos=30 and Pos=40. If there are more than one row with Pos=20 for example, then I will generate more than one Print row. See examples. Needs more clearification? I am not out putting other fields as in your example. Will edit the question.
Petter Magnusson
Your solution seems to solve steps 1 and 1b, perhaps 2, but not 3 and 4....and this is when it gets tricky, and I start thinking temp tables and PHP....
Petter Magnusson
It would be great to solve steps 1-2 in an sql query, step 2 is not obvious for me.....then I could use PHP for steps 3 and 4.....
Petter Magnusson
This solved big parts of my problem. Thanks!
Petter Magnusson
+2  A: 

First, the question is too long.

Second, the rules (#1, #1b, #2, #3, and #4) are confusing and need clarification.

However, in spite of that, this is my advice.

If it's not obvious how to do it in SQL, don't.

For something like this, with "rules" for matching ("star is matching anything", "select the row with lowest Mismatch when duplicates exist", etc.) you have two choices.

  1. Simplify the processing steps so that they can be implemented in SQL. This is generally a good thing. Your current statement is confused and hard to follow. Time spent simplifying would be time well spent.

  2. Use an ordinary programming language. When you reach things which are hard to do in SQL, don't "force" them. Get the data from SQL and process it in PHP.

SQL -- generally -- is slow. It's best used when you have vast volumes of data that won't fit in memory and you have transactional updates going on. 1000 rows and 30 fields is a trivial amount of data that easily fits in memory. If you're given a batch of data to analyze (with no updates going on) SQL's not helping much.

In a very busy website -- using PHP -- you may have problems with lots of concurrent users. In which case, get it out of PHP so it's not running under Apache. Until such time as Apache runs out of memory, just do it in PHP.

S.Lott
Hi Scott, the algorith is a bit hard to explain so I included examples instead, hoping the logic would be clear from those. But I have now improved the question a bit. If possible give some more step by step consideration like #1-#2 select to a temp table, #3 use PHP to generate new temp table, #4 sort in sql...
Petter Magnusson
@Petter Magnusson: It is still very hard to follow. The "see examples" isn't helpful because the examples are long and hard to follow.
S.Lott
OK, will have to think if I can simplify the examples, but dont think so.....trying to explain them: "Indata" is a row with criteria for the filter in step #1, "Results after filter #1" is the rows left after step #1, "Results after filter #2" is the rows left after step #2, and "Print for indata 1" is the end result presenting the contect of field OutData after the second filtering.
Petter Magnusson
@Petter Magnusson: Don't add comments here. Please fix the question so everyone can see the clear explanation. If it's hard to explain, then it will be nearly impossible to program -- and be sure the program is correct. Simplifying the explanation is absolutely central to writing a program that works. Please update the question with an explanation which is simpler and more clear.
S.Lott
Sorry for everything! Any better?
Petter Magnusson
A: 

One thing I've learned. Anything that can be calculated should not be saved in the database

Roland
+1  A: 

I don't fully understand the business logic, but for such a tiny amount of data I would not bother going through the pain of trying to implement in SQL. It is likely to be faster just pulling all the data over and using PHP to apply your filters.

RedFilter
Good comment! Could be the solution....
Petter Magnusson