簡體   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