|
jwalborn
Starting Member
2 Posts |
Posted - 2009-10-09 : 19:07:22
|
| I'm sure most of you have seen some form of this before... I need to select the top (n) rows for each group in a query. I've seen several examples online, but they all used a single table and I can't get them to work for my situation.I'll simplify as much as possible and present the problem with only two tables [ASSET] and [DATAPOINT].[ASSET]id,name1,'assetone'2,'assettwo'3,'assetthree'[DATAPOINT]id,name,value,asset_id101,'dp101','aaa',1102,'dp102','bbb',1103,'dp103','ccc',1201,'dp201','ddd',2202,'dp202','eee',2301,'dp301','fff',3302,'dp302','ggg',3303,'dp303','hhh',3I need to select the top (n) [DATAPOINT] values for each [ASSET]. For the sake of an example, we can return the TOP 1 value for each asset, needing to end up with a result like:asset_id,asset_name,datapoint_id,datapoint_name,datapoint_value1,'assetone',103,'dp103','ccc'2,'assettwo',202,'dp202','eee'3,'assetthree',303,'dp303','hhh'We're obviously going to have to use grouping and probably some sort of self-relationship within the [ASSET] table, but I can't quite figure out how to resolve this.Any thoughts?-----I'd like to avoid temporary tables and / or cursors if possible. If we have to go that route, we'll solve this one application-side.Ask not what your country can do for you;'Cause you're not gonna like the answer... |
|