Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Help with query joining same table top 1 Expand / Collapse
Author
Message
Posted Friday, August 23, 2013 5:25 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, October 13, 2014 4:52 PM
Points: 101, Visits: 236
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
Post #1487751
Posted Friday, August 23, 2013 5:44 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 12:25 PM
Points: 5,174, Visits: 12,028
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.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1487761
Posted Friday, August 23, 2013 5:57 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, October 13, 2014 4:52 PM
Points: 101, Visits: 236
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
Post #1487772
Posted Friday, August 23, 2013 7:50 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 13,085, Visits: 12,551
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)
Post #1487829
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse