Query about SQL INNER JOIN SELECT TOP 1

1

I have the following question: in a table I have a ID that is related to the other table, but the daughter table has several ID of the table Father then at the time of doing the INNER JOIN brings me N records. I'm trying to do a SELECT TOP 1 in the JOIN but I have not succeeded.

In the table B I have N times the ID_A , but I want you to just bring me the first.

SELECT * FROM A 
INNER JOIN B ON B.ID_A = A.ID_A

So if I do the query in a normal way it brings me several times the same record because in table B there are several records associated with that ID.

Then I tried this:

SELECT * FROM A 
INNER JOIN B ON B.ID_A = (SELECT TOP 1 ID_A FROM B WHERE ID_A = A.ID_A)

But I still get the same result, I still throwing the same number of results as the first query. Any ideas?

Thank you.

    
asked by M. Gress 13.06.2017 в 18:55
source

1 answer

2

Depending on the data you need from the B table, you can do something like this:

SELECT A.*,
       B.*
       FROM TablaA A
       INNER JOIN ( SELECT ID, OtroCampo
                           FROM TablaB
                           GROUP BY ID, OtroCampo
                   ) B
                   ON B.ID_A = A.ID_A
    
answered by 13.06.2017 / 19:00
source