SQL How JOIN 2 Таблицы и возвращаемые строки с определенным фильтром - PullRequest
0 голосов
/ 15 марта 2019

Они могут помочь мне решить следующую проблему: у меня есть 3 связанные таблицы. Предметы, наемные работники и счета

Мне нужно получить из таблицы Предметы все данные, которые в столбце «количество» делаютне соответствует точно сумма суммы всех элементов этого типа в таблице Hireds и добавить столбец с отсутствующим значением, чтобы завершить его ...

1.Таблица предметов

create table items
(
  id             bigserial        not null
    constraint items_pkey
    primary key,
  group_id       integer          not null
    constraint items_group_id_foreign
    references groups
    on delete cascade,
  description    text             not null,
  quantity       double precision not null,
  measurement_id integer          not null
    constraint items_measurement_id_foreign
    references measurements
    on delete cascade,
  model          varchar(255),
  unitary        double precision not null,
  price          double precision not null,
  currency_id    integer          not null
    constraint items_currency_id_foreign
    references currencies
    on delete cascade,
  created_at     timestamp(0),
  updated_at     timestamp(0)
);

2.Таблица найма

create table hireds
(
  id             bigserial        not null
    constraint hireds_pkey
    primary key,
  bill_id        integer          not null
    constraint hireds_bill_id_foreign
    references bills
    on delete cascade,
  item_id        integer          not null
    constraint hireds_item_id_foreign
    references items
    on delete cascade,
  quantity       double precision not null,
  measurement_id integer          not null
    constraint hireds_measurement_id_foreign
    references measurements
    on delete cascade,
  price          double precision not null,
  created_at     timestamp(0),
  updated_at     timestamp(0)
);

3.Таблица счетов

create table bills
(
  id           bigserial        not null
    constraint bills_pkey
    primary key,
  name         varchar(255)     not null
    constraint bills_name_unique
    unique,
  date         date             not null,
  contract_id  integer          not null
    constraint bills_contract_id_foreign
    references contracts
    on delete cascade,
  shipping_id  integer          not null
    constraint bills_shipping_id_foreign
    references shippings
    on delete cascade,
  price        double precision not null,
  currency_id  integer          not null
    constraint bills_currency_id_foreign
    references currencies
    on delete cascade,
  observations text,
  created_at   timestamp(0),
  updated_at   timestamp(0)
);

Пример:

1.Таблица элементов

id | description | quantity
-----------------------------
1  | Item 1      | 30
2  | Item 2      | 40 
3  | Item 3      | 50 
4  | Item 4      | 60 
5  | Item 5      | 70 

2.Наемный стол

id | item_id | quantity
-----------------------------
1  | 1       | 5
2  | 1       | 15 
3  | 1       | 10 
4  | 2       | 20 
5  | 3       | 20 

1.Ожидаемый результат

id | item_id | quantity | missing
-----------------------------
4  | 2       | 20       | 40
5  | 3       | 20       | 50

1 Ответ

0 голосов
/ 15 марта 2019

Вы можете сделать следующее:

select i.id , i.quanity - h.q 
from Items i
join
(
    select item_id, sum(quantity) as q  
    from Hireds 
    group by item_id 
) h on i.id = h.item_id
where quanity > q
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...