Order by or filter in join Sql Server

0

You can use order by or some filter in inner join or left join or I'm trying to do this in the wrong way. I need to bring in a query a column that is in a daughter table, but I need to be the most recent record column of the daughter table records. To solve this problem I made a sub-query and solved the problem well, but I was wondering if I could do a join with top 1 and ordered by a column.

    
asked by anonymous 30.07.2016 / 16:49

2 answers

0

Below is a script of how to do what you would like, but if you can post what you did to better understand it will be clearer for everyone.

Select TOP 10 TB1.COLUNANOVA 
from Tabela1 TB1
   inner join Tabela2 TB2
      on TB1.CODPRIMARIO = TB2.CODIGOPRIMARIO
where TB1.COLUNAANTIGA = "resultado"
order by TB1.COLUNANOVA
    
01.08.2016 / 18:40
0

What do you want can not be solved with Cross Apply ? Here is an example of use, you are not ordering, but then you edit to what applies to you.

SELECT  Employee.EmployeeId ,
        FirstName ,
        LastName ,
        PhoneType ,
        PhoneNumber
FROM    dbo.Employee
        CROSS  APPLY ( SELECT TOP 1
                                PhoneNumber ,
                                PhoneType ,
                                EmployeeId
                      FROM      dbo.PhoneNumber
                      WHERE     PhoneType = 4
                                AND EmployeeId = Employee.EmployeeId ) p
ORDER BY EmployeeId;
    
08.01.2018 / 18:43