Link to home
Start Free TrialLog in
Avatar of jsbx
jsbxFlag for Chile

asked on

Join 2 tables (MS Sql 2005)

Hello, I'm sure this is pretty easy, but I can't find a way to do it.

TABLE A:

CODIGO    VENTA    COMPRA
10        100      0
20        100      0
30        100      0

TABLE B:

CODIGO    VENTA    COMPRA
40        0        100
20        0        100
50        0        100

I need to get this in just one select:

CODIGO    VENTA    COMPRA
10        100      0
20        100      100
30        100      0
40        0        100
50        0        100

Open in new window


Thank you,
band.
Avatar of Sean
Sean
Flag of United States of America image

Been awhile since i've done sql but i believe this should get you what you want.

SELECT CODIGO,VENTA,COMPRA
FROM TableA
GROUP BY CODIGO
UNION ALL
SELECT CODIGO,VENTA,COMPRA
FROM TableB
GROUP BY CODIGO
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jsbx

ASKER

Worked like a charm.

Thanks Sean also, but I got errors on that code.