PostgreSQL查找每个实体的缺失属性。

huangapple go评论64阅读模式
英文:

PostgreSQL find missing attributes for each entity

问题

I can help you with the translation of your request into a query. Here's the translated query in SQL:

-- Find entity_key that doesn't have mandatory attributes_type_key
SELECT DISTINCT e.entity_key
FROM entities e
LEFT JOIN attributes_type a ON e.entity_type_key = a.entity_type_key
WHERE a.mandatory = true
AND e.attributes_type_key NOT IN (
  SELECT attributes_type_key
  FROM attributes_type
  WHERE entity_type_key = e.entity_type_key
  AND mandatory = true
);

This query should return the entity_key values that don't have all the mandatory attributes_type_key for their corresponding entity_type_key.

英文:

I have two datasets attributes_type and entities.

attributes_type dataset contains multiple attributes_type_key's to each entity_type.

CREATE TABLE attributes_type (
	entity_type_key int,
  attributes_type_key serial PRIMARY KEY,
  name varchar,
  mandatory boolean
);
    
INSERT INTO attributes_type (entity_type_key, name, mandatory) VALUES
  (1, 'car_Name',  true),
  (1, 'Price',  true),
  (1, 'Location',  false),
  (2, 'Color_name',  true),
  (2, 'Color_code', false);

CREATE TABLE entities (
  entity_key int,
  entity_type_key int,
  attributes_type_key int,
  values varchar
);

INSERT INTO entities (entity_key, entity_type_key, attributes_type_key, values) VALUES
  (1, 1, 1, 'Ford'),
  (1, 1, 2, '25000'),
  (2, 1, 2, '30000'),
  (2, 1, 3, 'Los Angeles'),
  (3, 2, 4, 'Blue'),
  (3, 2, 5, 'xy1500'),
  (4, 1, 1, 'Toyota'),
  (4, 1, 3, 'New York'),
  (5, 2, 5, 'xc000'),
  (5, 2, 4, 'Nilkamal'),
  (7, 1, 3, 'london');

DB Fiddle

if attributes_type_key's has mandatory true then entity_key should have those mandatory attributes_type_key in entities dataset for that entity_type.

Example: 1 entity_type_key has name and price attribute_type mandatory=true.
so if entities dataset has entity_key of entity_type_key=1 then attributes_type_key 1 and 2 must be there.

Now I want query entities table and find entity_key that doesn't have mandatory attributes_type_key .
Example: entity_key 2 doesn't mandatory attributes_type_key 1 , similarly entity_key 4 and 7 have missing mandatory attributes_type_key.

query should return 2,4 and 7 entity_key

Could you help with query

答案1

得分: 0

作为一个入门指南:您的数据模型应该有一个单独的表来存储实体,以及它们的类型。您当前的模式在映射表的每一行上冗余地存储了实体类型(实际上应该命名为类似entites_attributes而不仅仅是entities)。

其中一个选项是从中提取不同的实体,然后使用join获取相应的必填属性;然后我们可以使用not exists筛选出映射表中不存在的元组:

select e.entity_key, a.*
from (select distinct entity_key, entity_type_key from entities) e
inner join attributes_type a on a.entity_type_key = e.entity_type_key
where a.mandatory and not exists (
    select 1
    from entities ea
    where ea.entity_key = e.entity_key and ea.attributes_type_key = a.attributes_type_key
)

这将为每个缺失的实体/属性元组生成一行,所以对于您的示例数据:

entity_key entity_type_key attributes_type_key name mandatory
2 1 1 car_Name t
4 1 2 Price t
7 1 1 car_Name t
7 1 2 Price t

如果您只想要实体,您可以使用group by代替。

fiddle

英文:

As a starter: your data model should have a separate table to store the entities, along with their type. Your current schema redondently stores the entity type on each row of the mapping table (which should actually be called something like entites_attributes rather than just entities).

One option is to extract distinct entities from the , and then bring the corresponding mandatory attributes with a join; we can then filter on the tuples that do not exist in the mapping table with not exists:

select e.entity_key, a.*
from (select distinct entity_key, entity_type_key from entities) e
inner join attributes_type a on a.entity_type_key = e.entity_type_key
where a.mandatory and not exists (
    select 1
    from entities ea
    where ea.entity_key = e.entity_key and ea.attributes_type_key = a.attributes_type_key
)

This generates one row for each missing entity/attribute tuple, so for your sample data:

entity_key entity_type_key attributes_type_key name mandatory
2 1 1 car_Name t
4 1 2 Price t
7 1 1 car_Name t
7 1 2 Price t

If you just want the entities, you can group by instead.

fiddle

huangapple
  • 本文由 发表于 2023年5月18日 03:14:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定