SQL如何JOIN 2表和具有特定过滤器的返回行

时间:2019-03-14 21:17:27

标签: sql join

他们可以帮助我解决以下问题: 我有3个相关表格.. 物品,人员和账单

我需要从表Items中获取其“数量”列中的所有数据与表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 :(得分:0)

您可以执行以下操作:

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