views:

316

answers:

5

Table1
...
LogEntryID *PrimaryKey*
Value
ThresholdID - - - Link to the appropriate threshold being applied to this log entry.
...

Table2
...
ThresholdID *PrimaryKey*
Threshold
...

All fields are integers.
The "..." thingies are there to show that these tables hold a lot more imformation than just this. They are set up this way for a reason, and I can't change it at this point.

I need write a SQL statement to select every record from Table1 where the Value field in that particular log record is less than the Threshold field in the linked record of Table2.

I'm newish to SQL, so I know this is a basic question.
If anyone can show me how this SQL statement would be structured, it would be greatly appreciated.

+1  A: 
SELECT * FROM Table1
JOIN Table2
ON table1.ThresholdID = table2.ThresholdID --(assuming table 2 holds the same value to link them together)
WHERE
value < thresholdvalue

A 'JOIN' connects 2 tables based on the 'ON' clause (which can be multipart, using 'AND' and 'OR')

If you have 3 entries in table 2 which share table1's primary key (a one-to-many association) you will receive 3 rows in your result set.

for the tables below, for example:

Table 1:
Key     Value
1       Hi
2       Bye

Table 2:
Table1Key  2nd_word
1           You
1           fellow
1           friend
2           now

this query:

SELECT * FROM Table1 JOIN Table2 on table1.key = table2.table1key

gets this result set:

Key    Value    Table1Key   2nd_word
1      Hi        1          You
1      Hi        1          fellow
1      Hi        1          friend
2      Bye       2          now

Note that JOIN will only return results when there is a match in the 2nd table, it will not return a result if there is no match. You can LEFT JOIN for that (all fields from the second table will be NULL).

JOINs can also be strung together, the result from the previous JOIN is used in place of the original table.

Jeff
Where did you get the JOIN on logentryid from? And the where clause on "threshold value"?
gbn
A hasty SQL line that I typed out as quickly as possible so that I could then put in a much better response: a discussion of JOINs...which I felt would be much more helpful than simply giving him a fish.
Jeff
hasty to get first answer? ;-)
gbn
Also, it's worth noting that I had edited the errors you mentioned out 2 minutes before you posted your comment.
Jeff
If you don't get the first answer, you don't get looked at on SO...and I wanted to make sure he got the chance to see my discussion on JOIN
Jeff
A simple overlap. However, it's still "threshold > value" in the question. Note I also removed my downvote on correction of JOIN and comments
gbn
+4  A: 
SELECT T1.*
  FROM Table1 T1
  JOIN Table2 T2 ON T2.ThresholdID = T1.ThresholdID
 WHERE T2.Threshold > T1.Value
n8wrl
Additionally it should be noted that you can do "JOIN Table2 T2 ON T2.ThresholdID = T1.ThresholdID AND T2.Threshold > T1.Value" which will cause the join to only occur on the rows which meet your filter conditions, instead of joining all matching rows and then filtering in the WHERE.
cfeduke
It would be interesting to compare the execution plans.
n8wrl
@Both. Probably would give the same plan. I'd suggest the WHERE construct is clearer because it's a filter after the thresholdid JOIN.
gbn
Thanks! You guys are all awesome!
Giffyguy
i agree with gbn, even if the plans AREN'T the same - clarity matters MUCh more than speed, when the differences are minor.It's only when the differences are significant that you should choose the less clear way (but be sure you comment it!)
Jeff
The plan is different depending on the presence of [appropriate] indexes. Introducing an AND into a JOIN clause in a several hundred thousand row table join took a query from taking 7 seconds to milliseconds in our production system.
cfeduke
That would be one of the 'significant' differences ;)
Jeff
@cfeduke: on outer joins, more likely. Otherwise I doubt there would be a plan difference in this case. I can't comment on your other case. Try the older join syntax in the WHERE clause to see what I mean too.
gbn
+1  A: 
SELECT t1.*
FROM dbo.Table1 t1 INNER JOIN dbo.Table2 t2 ON t1.ThresholdID = t2.ThresholdID
WHERE t2.Threshold > t1.Value
Chris Klepeis
+1  A: 

SELECT * from table1 t1 join table2 t2 on (t1.thresholdId = t2.thresholdId) where t1.value < t2.threshold;

RC
+1  A: 
SELECT t1.LogEntryID, t1.Value, t1.ThresholdID
FROM Table1 t1 
INNER JOIN Table2 t2 ON t1.ThresholdID = t2.ThresholdID 
WHERE t1.Value < t2.threshold
RedFilter