繁体   English   中英

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

[英]SQL How JOIN 2 Tables and return rows with specific filter

他们可以帮助我解决以下问题:我有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

您可以执行以下操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM