Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help with query joining same table top 1


Help with query joining same table top 1

Author
Message
skb 44459
skb 44459
SSC-Enthusiastic
SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 Visits: 267
following query works

SELECT matltran.trans_num, matltran.trans_type, matltran.trans_date, matltran.ITEM,matltran.qty,matltran.matl_cost ,ledger.dom_amount,
(SELECT TOP 1 bmatltran.ref_num FROM dbo.matltran AS bmatltran WHERE bmatltran.trans_type = 'f' AND bmatltran.lot = dbo.matltran.lot) AS jobjob ,
FROM dbo.matltran

But I want to do a Join on another table based on the result of field jobjob
(SELECT TOP 1 bmatltran.ref_num FROM dbo.matltran AS bmatltran WHERE bmatltran.trans_type = 'f' AND bmatltran.lot = dbo.matltran.lot) AS jobjob ,


SELECT matltran.trans_num, matltran.trans_type, matltran.trans_date, matltran.ITEM,matltran.qty,matltran.matl_cost ,ledger.dom_amount,
(SELECT TOP 1 bmatltran.ref_num FROM dbo.matltran AS bmatltran WHERE bmatltran.trans_type = 'f' AND bmatltran.lot = dbo.matltran.lot) AS jobjob ,
FROM dbo.matltran
join rs_qcrcvr on jobjob = rs_qcrcvr.job
Phil Parkin
Phil Parkin
SSCrazy Eights
SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)

Group: General Forum Members
Points: 8373 Visits: 19499
Are you able to provide DDL, sample data and desired output, as per the link in my signature?

Can I also suggest that you read up on column aliases - it might make things more readable.

So instead of

select reallylongtablename.col1, reallylongtablename.col2
from dbo.reallylongtablename

you get

select t1.col1, t2.col2
from dbo.reallylongtablename t1

and the icing on the cake is to surround the T-SQL with IFCode tags:

select t1.col1, t2.col2
from dbo.reallylongtablename t1




Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
skb 44459
skb 44459
SSC-Enthusiastic
SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 Visits: 267
Here is the simplified query

select
m.trans_num ,
m.trans_type ,
m.lot
from matltran m
/*I want to find the top 1 record from same tables with 'WHERE trans_type = 'F' and lot = m.lot */
inner join top 1 record of matltran table where trans_type = 'F' and lot = m.lot
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16582 Visits: 17024
skb 44459 (8/23/2013)
Here is the simplified query

select
m.trans_num ,
m.trans_type ,
m.lot
from matltran m
/*I want to find the top 1 record from same tables with 'WHERE trans_type = 'F' and lot = m.lot */
inner join top 1 record of matltran table where trans_type = 'F' and lot = m.lot


Top 1 as ordered by what?

I don't even begin to understand what you mean by joining to the top 1 row. We don't join to rows, you join to a table. Why would you want to select 1 row and join it to itself? I think it would serve you well to post some solid details and a clear description of what you are trying to do.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search