Solved

Select Top row from child table

Posted on 2011-03-14
5
481 Views
Last Modified: 2012-05-11
I am writing a sproc to return rows from tableA and I need the first row that was entered in child tableB.

 TableA
A_ID              PK Identity
JobNumber  

TableB
B_ID                 PK Identity
A_ID                 FK  int
DateEntered
Comment

I am close but not there yet. Since the PK in tableB is an identity I think I'm safe selecting the row that has the min(B_ID) WHERE tableB.A_ID = tableA.A_ID

So I have this
SELECT * FROM tableA, (SELECT DateEntered,Comment FROM tableB  WHERE tableB.A_ID = tableA.A_ID)

Hung up getting the rest. Still trying but would like some help.
0
Comment
Question by:AkAlan
  • 3
5 Comments
 
LVL 26

Expert Comment

by:tigin44
ID: 35133883
SELECT A.*, B. DateEntered, B.Comment
FROM tableA A
   INNER JOIN (SELECT A_ID, MIN(DateEntered) AS DateEntered, MIN(Comment) AS Comment FROM tableB GROUP BY A_ID) B ON A.A_ID = B.A_ID
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35133986


SELECT * FROM tableA, (SELECT top 1 DateEntered,Comment FROM tableB WHERE tableB.A_ID = tableA.A_ID order by dateentered)
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35134028
Posted the wrong one

SELECT *, (SELECT top 1 DateEntered,Comment FROM tableB WHERE tableB.A_ID = tableA.A_ID order by dateentered)
 FROM tableA
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35134059

Disregard my post
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 35134150
try this query.
SELECT * 
  FROM (SELECT t1.A_ID,t1.JobNumber,t2.B_ID,t2.DateEntered,t2.Comment, 
               ROW_NUMBER() 
                 OVER(PARTITION BY t1.A_ID ORDER BY t2.B_ID) rn 
          FROM TableA t1 
               JOIN TableB t2 
                 ON t1.A_ID = t2.A_ID) t1 
 WHERE rn = 1

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now