| Бьюсь второй день над задачей по MySQL - не могу решить!
Единственное (некорректное) решение, которое у меня получилось (через UNION и "ручное" прописывание 2х фирм в запросе, а ведь их может быть больше!)
SELECT c.name, g.name, SUM( s.quantity )
FROM company c
JOIN shipment s ON c.compid = s.compid
JOIN goods g ON g.goodid = s.goodid
WHERE c.compid=1
GROUP BY g.goodid
UNION
SELECT c.name, g.name, SUM( s.quantity )
FROM company c
JOIN shipment s ON c.compid = s.compid
JOIN goods g ON g.goodid = s.goodid
WHERE c.compid=2
GROUP BY g.goodid
Помогите, пожалуйста. В какую сторону смотреть, где искать ответ?
---------------------------------------------------------------------------
Задача.
Вывести общий объем поставок каждого из продуктов для каждой фирмы
с указанием даты последней поставки.
Дамп таблиц:
create table company (compid int identity(1,1) primary key, name nvarchar(100) not null)
create table goods (goodid int identity(1,1) primary key, name nvarchar(100) not null)
create table shipment (shipid int identity(1,1) primary key, compid int not null, goodid int not null, quantity
float not null, shipdate datetime not null)
set identity_insert company on
insert company (compid, name) values (1, 'Intel')
insert company (compid, name) values (2, 'IBM')
insert company (compid, name) values (3, 'Compaq')
set identity_insert company off
set identity_insert goods on
insert goods (goodid, name) values (1, 'Pentium IV')
insert goods (goodid, name) values (2, 'Celeron')
set identity_insert goods off
insert shipment (compid, goodid, quantity, shipdate) values (1, 1, 100, '02/04/2004')
insert shipment (compid, goodid, quantity, shipdate) values (1, 1, 200, '02/12/2004')
insert shipment (compid, goodid, quantity, shipdate) values (1, 2, 300, '03/02/2004')
insert shipment (compid, goodid, quantity, shipdate) values (1, 2, 400, '03/09/2004')
insert shipment (compid, goodid, quantity, shipdate) values (2, 1, 100, '01/29/2004')
insert shipment (compid, goodid, quantity, shipdate) values (2, 1, 200, '02/06/2004')
insert shipment (compid, goodid, quantity, shipdate) values (2, 2, 300, '02/29/2004')
insert shipment (compid, goodid, quantity, shipdate) values (2, 2, 400, '03/01/2004') | |