0
How do I select with sum
and inner join
to sum an x value between 3 tables. A query
to return as few results as possible.
create table credit_card (
credit_card_id int primary key,
nome varchar (30)
);
create table store (
store_id int primary key,
nome varchar (30)
);
create table sale (
id serial primary key,
data date,
valor numeric,
credit_card_fk int references credit_card (id),
store_id_fk int references store (id)
);
select * from store
select * from credit_card
select * from sale
DROP TABLE CREDIT_CARD CASCADE
That I don’t know how to do:
select store.nome, store_id_fk, credit_card.nome, credit_card_fk, sale.valor, sale.data
from sale
inner join store on (store.id= sale.store_id_fk)
inner join credit_card on (credit_card.id= sale.credit_card_fk)
order by store
You will have to make the 3 selects, unite them with
UNION
(search the command, it’s quite simple). After that, you will make a select in that group you created, using theSUM()
to sum the values and theMIN()
to see which is the smallest. If you can’t, post your bank to http://sqlfiddle.com/ so we can simulate.– rbz