tags:

views:

195

answers:

3

This question is based on this other question of mine, and uses all of the same basic info. That link shows my table layouts and the basic gist of the simple join.

I would like to write another query that would select EVERY record from Table1, and simply sort them by whether or not Value is less than the linked Threshold.

Again, I appreciate anyone who is willing to take a stab at this. Databases have never been a strong point of mine.

+6  A: 
SELECT t1.LogEntryID, t1.Value, t1.ThresholdID, 
    case when t1.Value < t2.threshold then 1 else 0 end as Rank
FROM Table1 t1 
INNER JOIN Table2 t2 ON t1.ThresholdID = t2.ThresholdID 
ORDER By Rank

You can add DESC after ORDER By Rank if you want the reverse order.

RedFilter
You'll need to add the rest of the fields you want manually, but you should probably be doing that anyway.
Dave
I don't understand this comment, the query is showing all records from Table1, which is the requirement.
RedFilter
I mentioned that there are far more fields in these tables than I am showing. I believe Dave is referring to that.
Giffyguy
In the end: I think you are correct in presuming, OrbMan. We purposely did NOT add a column for the "rank" to Table1, because we planned on calculating it on the fly in our queries. But what good does that do us if we just have to manually loop back through all the data to figure it out anyway? Having it return in an added column is perfect. That way it's only there when we need it, and it's guaranteed to be accurate.
Giffyguy
+4  A: 

Similar to the answer given by OrbMan, but I do prefer the CASE to be explicit in the ORDER BY, so that you are not forced to display your order by column.

SELECT
  t1.LogEntryID
 ,t1.Value
 ,t1.ThresholdID
FROM 
  Table1 t1
  JOIN Table2 t2 ON t2.ThresholdID = t1.ThresholdID
ORDER BY
  CASE WHEN t1.Value < t2.threshold
       THEN 1
       ELSE 0
  END ASC
JosephStyons
OrbMan's solution gives you a quick way to check the condition of each Row, though, which, if he wants the full result set, is probably going to be information he needs.
Dave
This one looks cleaner to me ... I'm not sure what the major differences in the execution plan are, apart from the JOIN type. I don't care so much about the results of Table2. I just want every row of Table1 ordered by whether it passes it's given threshold.
Giffyguy
I presumed you wanted to see the rank in the output, since without showing the records from Table2, you have no way of knowing which ones had t1.Value < t2.threshold without it.
RedFilter
Ah, true. I'm going to have to deal with that eventually, but I'm just trying to get my bearings on the SQL statements for now - figure out what it is that I have at my disposal, and how I want to use it.
Giffyguy
A: 

Just a thought, but couldn't you use 'threshold - value' as one of your return columns?

If you did (and the columns were numeric) you would also be able to see how close (or far) from the threshold the value was.

anything positive or 0 would be under the threshold, anything negative wouldn't.

Jeff