简体   繁体   中英

SQL How JOIN 2 Tables and return rows with specific filter

They could help me with the following problem: I have 3 related tables .. Items, Hireds and Bills

I need to obtain from the table Items, all the data that in its column 'quantity' do not correspond exactly with the sum of the amount of all the items of that type in the table Hireds and add a column with the missing value to complete it...

1. Items Table

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. Hireds Table

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. Bills Table

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)
);

Example:

1. Items Table

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

2. Hireds Table

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

1. Expected Result

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

You can do the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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