Question

I tried a lot allready, but im not getting the result that i want.

Lets say i got the following data in the first table:

ColA  ColB   ColC  ColD
AA     11     1    50€
AA     11     1    60€
AA     11     1    10€
BB     22     1    15#

And in Table B I have this:

ColA  ColB   ColC   ColD
AA     11     1      Delta
AA     11     1      Delta
AA     11     1      Delta
BB     22     1      Gamma
BB     22     1      Gamma

I need to create a table that looks like this - note only the 4 rows, not the 11 that the inner join I was using produces.

ColA  ColB   ColC  ColD     ColE
AA     11     1    50€      Delta
AA     11     1    60€      Delta
AA     11     1    10€      Delta
BB     22     1    15€      Gamma

If anyone has an idea on how to make this work that would be greatly appreciated.

Was it helpful?

Solution

Try this:

SELECT A.ColA,
   A.ColB,
   A.ColC,
   A.ColD,
   B.Cold AS ColE
FROM Table_A AS A
JOIN Table_B AS B
  ON B.ColA = A.ColA
 AND B.ColB = A.ColB
GROUP BY A.ColA,
   A.ColB,
   A.ColC,
   A.ColD,
   B.Cold

OTHER TIPS

Try this;

SELECT A.ColA,A.ColB,A.ColC,A.ColD,B.ColD as BCol
FROM TableA A JOIN
     TableB B ON A.ColA=B.ColA AND A.ColB=B.ColB AND A.ColC=B.ColC
GROUP BY A.ColA,A.ColB,A.ColC,A.ColD,B.ColD

Result:

COLA    COLB    COLC    COLD    BCOL
AA      11      1       10€     Delta
AA      11      1       50€     Delta
AA      11      1       60€     Delta
BB      22      1       15#     Gamma

See result in SQL Fiddle.

Can you use this:

select a.ColA,a.ColB,a.ColC,a.ColD,b.ColD
from table_a a inner join table_b b
on a.ColA = b.ColA
group by a.ColA,a.ColB,a.ColC,a.ColD,b.ColD
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top