Solved

Select Top row from child table

Posted on 2011-03-14
5
479 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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

15 Experts available now in Live!

Get 1:1 Help Now