Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
cragi
Starting Member
14 Posts |
Posted - 2004-09-12 : 10:41:39
|
| I guess I tried to oversimplify the problem too much. There is an outer query containing about 10 tables joined. For each record in one of the joined tables, say "MasterTable", I want to see if any rows from a related (by "GroupID") table will match a set of criteria, say we check its "Value", and of those matching, I want the one with the lowest "Priority", or to ignore the entire row altogether.MasterTable (MT)MTID GroupID0001 10000002 1001SlaveTable (ST)STID GroupID GradeID Priority Value0087 1000 X 0 870088 1000 Y 2 120089 1000 Z 1 400090 1001 A 0 1240091 1001 A 0 760092 1001 B 1 850so lets say the value I want to match is <50so, for the MasterTable, I would be reading the first row (0001), then try to return the GradeID whose GroupID matches (down to records 0087,0088,0089) and whose value is less than 50 (down to records 0088,0089) and now I'll take the first record available that has the lowest priority (0089)Here is my attempt:select ..... MT.* .....from ..... join MasterTable MT .... join (select TOP 1 GradeID,GroupID from SlaveTable Where Value<50 ORDER BY Priority ASC ) ST on MT.GroupID=ST.GroupIDThe problem above was that the inner select evaluated before I could use the constraint limiting it to the rows matching the outer table, but I can't do the constraint on the inner table table either, because it doesnt recognize the outer table |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-09-12 : 10:52:27
|
will this help?select ..... MT.* .....from ..... join MasterTable MT .... join (select min(Priority) as Priority, GroupID from SlaveTable Where Value<50 GROUP BY GroupID) ST on MT.GroupID = ST.GroupIDGo with the flow & have fun! Else fight the flow |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-09-12 : 11:43:00
|
| or to get the rowsselect *from mtjoin ston st.groupid = mt.groupidwhere st.priority = (select min(priority) from st where st.groupid = mt.groupid and st.value < 50)==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|
|
|