Necesito unir las sumas de los price
en función del type
Tabla tableA
:
type | priceA |
---|---|
1 | 100 |
2 | 400 |
Tabla tableB
:
type | priceB |
---|---|
1 | 500 |
Tabla type
:
is | type |
---|---|
1 | A |
2 | B |
Lo que intenté:
DROP TABLE IF EXISTS type;
CREATE TABLE type
(
id varchar(300),
type varchar(255)
);
INSERT INTO type ( id , type )
VALUES
('1', 'A'),
('2', 'B');
DROP TABLE IF EXISTS tableA;
CREATE TABLE tableA
(
type_id varchar(300),
price varchar(255)
);
INSERT INTO tableA ( type_id , priceA )
VALUES
('1', '100'),
('2', '400');
DROP TABLE IF EXISTS tableB;
CREATE TABLE tableB
(
type_id varchar(300),
price varchar(255)
);
INSERT INTO tableB ( type_id , priceB )
VALUES
('1', '500');
SELECT type, COALESCE(SUM(Aprice), 0 ) as sumAprice, COALESCE(SUM(Bprice), 0 ) as sumBprice FROM type
LEFT JOIN ( SELECT type_id, COALESCE (( priceA ), 0 ) AS Aprice FROM tableA GROUP BY type_id ) A ON type.id = A.type_id
LEFT JOIN ( SELECT type_id, COALESCE (( priceB ), 0 ) AS Bprice FROM tableB GROUP BY type_id ) B ON type.id = B.type_id
GROUP BY type;
Se puede ver en: https://sqliteonline.com/#fiddle=ca8bddb8f96c76bf990d28bfdeb34de89758bf3dd81c7f1c4827d1b931dafc8e
Obteniendo:
type | sumAprice | sumBprice |
---|---|---|
A | 100 | 500 |
B | 400 | 0 |
Pero no está correcto, necesito que se muestre de la siguiente manera:
type | sumAprice | sumBprice |
---|---|---|
A | 600 | 500 |
B | 400 | 0 |
Cómo puedo corregir o cambiar la consulta para obtener los datos correctamente?