Hi, I have a problem which I already solved using T-SQL(cursor & loop).(SQL server 2005)
But I am looking for the solution using SQL.
I have a Master Table with a column say MasterRecord(all are unique and type Varchar and PK)
MasterRecord
MRecord1
MRecord2
MRecord3
MRecord4
................
..................
MRecord[n]
Now the Master-Detail table has two columns MasterRecord(Varchar & FK) and DetailRecord(Varchar)
MasterRecord DetailRecord
MRecord1 MRecord1_DetailRecord1
MRecord1 MRecord1_DetailRecord2
MRecord1 MRecord1_DetailRecord3
MRecord1 MRecord1_DetailRecord4
MRecord2 MRecord2_DetailRecord1
MRecord2 MRecord2_DetailRecord2
MRecord2 MRecord2_DetailRecord3
MRecord2 MRecord2_DetailRecord4
...............................................
................................................
MRecord[n] MRecord[n] _DetailRecord1
MRecord[n] MRecord[n] _DetailRecord2
MRecord[n] MRecord[n] _DetailRecord3
MRecord[n] MRecord[n] _DetailRecord4
where [n] can be any number
The problem is that for each unique Master Record, I should fetch the top 2 detail records
O/P:
MasterRecord DetailRecord
MRecord1 MRecord1_DetailRecord1
MRecord1 MRecord1_DetailRecord2
MRecord2 MRecord2_DetailRecord1
MRecord2 MRecord2_DetailRecord2
MRecord3 MRecord3_DetailRecord1
MRecord3 MRecord3_DetailRecord2
...............................................
..............................................
MRecord[n] MRecord[n] _DetailRecord1
MRecord[n] MRecord[n] _DetailRecord2
Hope I clearly explained my problem.
Please let me know for further clarification.
Thanks in advance