首先我要说的是我没有设计这个数据库;只是想尝试使用它。
我正在尝试检索一组自行车的故障,其中最重要的决定因素是自行车中的任何部件是否具有特定属性。该属性在parts表中设置。该零件是组件的一部分,该组件引用了更大的组件。组件可以具有分配给它的特定自行车类型;如果没有,我们假设所有自行车类型都分配给组件。这些部件也可能配有特定的自行车,由序列号标识。
所以,我们可以假设:
当搜索存在具有特定属性的零件的故障时,如果零件具有对特定自行车的引用,我们只想找到那些。如果没有,并且零件的装配具有对特定自行车类型的引用,我们只希望找到与引用这些类型的装配相关的故障,并且包含这些部分。否则,我们希望找到与包含部件的较高程序集相关的所有故障。
我的问题是,如果我加入序列号,我总是只得到分配序列号的部分,如果我加入自行车类型,我只得到其程序集已分配类型的部分。我不确定我是否正在尝试一些根据数据库设计不切实际的东西,或者我是否正在接近错误的连接。
以下是迄今为止的查询。
SELECT f_bicycle_type, f_serial_number, f_big_assembly
FROM (
SELECT DISTINCT f.f_bicycle_type, f.f_serial_number, f.f_big_assembly, p_important_attr
from failures f
left outer join (
select distinct bt.bt_bicycle_type, b_serial_number, a_big_assembly, p_important_attr
from (
select distinct b.b_serial_number, a.a_big_assembly, p.p_assembly_id, p.p_important_attr
from parts p
join assemblies a on p.p_assembly_id = a.a_assembly_id
left outer join parts_bicycles b on b.b_part_id = p.p_id
where p.p_important_attr = 'awesome'
) p_join_a_and_b
left outer join assembly_bicycle_types bt on bt.bt_assembly_id = p_join_a_and_b.p_assembly_id
) p_join_a_and_b_join_bt
on f.f_big_assembly = p_join_a_and_b_join_bt.a_big_assembly
-- problem join clause - if an explicit type has not been assigned to the assembly, we want to include ALL types
and f_bicycle_type = p_join_a_and_b_join_bt.bt_bicycle_type
-- problem join clause - there may not be explicit serial numbers assigned to a given part
and f_serial_number = b_serial_number
) z
WHERE p_important_attr = 'awesome';
测试用例sql(对于Oracle):
CREATE TABLE failures (
f_bicycle_type VARCHAR(20),
f_serial_number NUMBER(20),
f_big_assembly VARCHAR(5));
CREATE TABLE parts (
p_id NUMBER(20),
p_assembly_id NUMBER(20),
p_important_attr VARCHAR(20));
CREATE TABLE assemblies (
a_assembly_id NUMBER(20),
a_big_assembly VARCHAR(5));
CREATE TABLE parts_bicycles (
b_part_id NUMBER(20),
b_serial_number NUMBER(20));
CREATE TABLE assembly_bicycle_types (
bt_assembly_id NUMBER(20),
bt_bicycle_type VARCHAR(20));
INSERT ALL
INTO failures (f_bicycle_type, f_serial_number, f_big_assembly)
VALUES ('tandem', 1000001, 'A1000')
INTO failures (f_bicycle_type, f_serial_number, f_big_assembly)
VALUES ('bmx', 1000002, 'A1000')
INTO failures (f_bicycle_type, f_serial_number, f_big_assembly)
VALUES ('tandem', 1000003, 'B1000')
INTO failures (f_bicycle_type, f_serial_number, f_big_assembly)
VALUES ('cruiser', 1000004, 'B1000')
INTO failures (f_bicycle_type, f_serial_number, f_big_assembly)
VALUES ('bmx', 1000005, 'C1000')
INTO failures (f_bicycle_type, f_serial_number, f_big_assembly)
VALUES ('motocross', 1000006, 'C1000')
INTO failures (f_bicycle_type, f_serial_number, f_big_assembly)
VALUES ('cruiser', 1000007, 'C1000')
INTO failures (f_bicycle_type, f_serial_number, f_big_assembly)
VALUES ('bmx', 1000008, 'D1000')
INTO failures (f_bicycle_type, f_serial_number, f_big_assembly)
VALUES ('bmx', 1000009, 'D1000')
INTO failures (f_bicycle_type, f_serial_number, f_big_assembly)
VALUES ('cruiser', 1000010, 'E1000')
INTO parts (p_id, p_assembly_id, p_important_attr)
VALUES (1, 1001, 'awesome')
INTO parts (p_id, p_assembly_id, p_important_attr)
VALUES (2, 1001, 'ordinary')
INTO parts (p_id, p_assembly_id, p_important_attr)
VALUES (3, 2001, 'awesome')
INTO parts (p_id, p_assembly_id, p_important_attr)
VALUES (4, 3001, 'awesome')
INTO parts (p_id, p_assembly_id, p_important_attr)
VALUES (5, 4001, 'awesome')
INTO parts (p_id, p_assembly_id, p_important_attr)
VALUES (6, 5001, 'ordinary')
INTO assemblies (a_assembly_id, a_big_assembly)
VALUES (1001, 'A1000')
INTO assemblies (a_assembly_id, a_big_assembly)
VALUES (2001, 'B1000')
INTO assemblies (a_assembly_id, a_big_assembly)
VALUES (3001, 'C1000')
INTO assemblies (a_assembly_id, a_big_assembly)
VALUES (4001, 'D1000')
INTO assemblies (a_assembly_id, a_big_assembly)
VALUES (5001, 'E1000')
INTO parts_bicycles (b_part_id, b_serial_number)
VALUES (4, 1000005)
INTO parts_bicycles (b_part_id, b_serial_number)
VALUES (4, 1000006)
INTO parts_bicycles (b_part_id, b_serial_number)
VALUES (5, 1000008)
INTO assembly_bicycle_types (bt_assembly_id, bt_bicycle_type)
VALUES (02001, 'tandem')
INTO assembly_bicycle_types (bt_assembly_id, bt_bicycle_type)
VALUES (04001, 'bmx')
SELECT * FROM DUAL;
对于MySQL:
CREATE TABLE failures (
f_bicycle_type VARCHAR(20),
f_serial_number INTEGER(20),
f_big_assembly VARCHAR(5));
CREATE TABLE parts(
p_id INTEGER( 20 ) ,
p_assembly_id INTEGER( 20 ) ,
p_important_attr VARCHAR( 20 )
);
CREATE TABLE assemblies(
a_assembly_id INTEGER( 20 ) ,
a_big_assembly VARCHAR( 5 )
);
CREATE TABLE parts_bicycles(
b_part_id INTEGER( 20 ) ,
b_serial_number INTEGER( 20 )
);
CREATE TABLE assembly_bicycle_types(
bt_assembly_id INTEGER( 20 ) ,
bt_bicycle_type VARCHAR( 20 )
);
INSERT INTO failures (f_bicycle_type, f_serial_number, f_big_assembly)
VALUES ('tandem', 1000001, 'A1000'),('bmx', 1000002, 'A1000'), ('tandem', 1000003, 'B1000'), ('cruiser', 1000004, 'B1000') ,('bmx', 1000005, 'C1000'), ('motocross', 1000006, 'C1000')
,('cruiser', 1000007, 'C1000')
,('bmx', 1000008, 'D1000')
,('bmx', 1000009, 'D1000')
, ('cruiser', 1000010, 'E1000');
insert INTO parts (p_id, p_assembly_id, p_important_attr)
VALUES (1, 1001, 'awesome'), (2, 1001, 'ordinary'), (3, 2001, 'awesome'), (4, 3001, 'awesome'), (5, 4001, 'awesome'),(6, 5001, 'ordinary');
INSERT INTO assemblies (a_assembly_id, a_big_assembly)
VALUES (1001, 'A1000'), (2001, 'B1000'), (3001, 'C1000'), (4001, 'D1000'),(5001, 'E1000');
INSERT INTO parts_bicycles (b_part_id, b_serial_number)
VALUES (4, 1000005),(4, 1000006),(5, 1000008)
INSERT INTO assembly_bicycle_types (bt_assembly_id, bt_bicycle_type)
VALUES (02001, 'tandem'), (04001, 'bmx');
样本数据和所需结果:
-- failures table
-- f_bicycle_type || f_serial_number || f_big_assembly
---------------------------------------------------------
tandem 1000001 A1000
bmx 1000002 A1000
tandem 1000003 B1000
cruiser 1000004 B1000
bmx 1000005 C1000
motocross 1000006 C1000
cruiser 1000007 C1000
bmx 1000008 D1000
bmx 1000009 D1000
cruiser 1000010 E1000
-- parts table
-- p_id || p_assembly_id || p_important_attr
------------------------------------------------
1 1001 awesome
2 1001 ordinary
3 2001 awesome
4 3001 awesome
5 4001 awesome
6 5001 ordinary
-- assemblies table
-- a_assembly_id || a_big_assembly
-----------------------------------
1001 A1000
2001 B1000
3001 C1000
4001 D1000
5001 E1000
-- parts_bicycles table
-- b_part_id || b_serial_number
--------------------------------
4 1000005
4 1000006
5 1000008
-- assembly_bicycle_types table
-- bt_assembly_id || bt_bicycle_type
------------------------------------
02001 tandem
04001 bmx
-- desired results from failures table
-- f_bicycle_type || f_serial_number || f_big_assembly
---------------------------------------------------------
tandem 1000001 A1000
bmx 1000002 A1000
tandem 1000003 B1000
bmx 1000005 C1000
motocross 1000006 C1000
bmx 1000008 D1000
实际结果,问题加入到位:
-- actual results from failures table
-- f_bicycle_type || f_serial_number || f_big_assembly
---------------------------------------------------------
bmx 1000008 D1000
答案 0 :(得分:2)
你去(PostgreSQL风味):
WITH chosen_parts AS (
SELECT * FROM parts LEFT JOIN parts_bicycles ON b_part_id = p_id
WHERE p_important_attr = 'awesome'
), chosen_assemblies AS (
SELECT * FROM assemblies JOIN chosen_parts ON p_assembly_id = a_assembly_id
LEFT JOIN assembly_bicycle_types ON bt_assembly_id = a_assembly_id
WHERE b_serial_number IS NULL
)
SELECT failures.* FROM chosen_parts JOIN failures
ON f_serial_number = b_serial_number
UNION
SELECT failures.* FROM chosen_assemblies JOIN failures
ON f_big_assembly = a_big_assembly
WHERE bt_bicycle_type = f_bicycle_type
OR bt_bicycle_type IS NULL;
如果需要重复,请随意添加外部SELECT DISTINCT * FROM
。
答案 1 :(得分:1)
此查询连接parts,parts_bicycles,assembly,assembly_bicycle_type。我们将其保存为视图:
create view j_parts as
select p_important_attr, b_serial_number, a_big_assembly, bt_bicycle_type
from
parts left join parts_bicycles
on parts.p_id = parts_bicycles.b_part_id
left join assemblies
on parts.p_assembly_id=assemblies.a_assembly_id
left join assembly_bicycle_types
on assemblies.a_assembly_id =assembly_bicycle_types.bt_assembly_id
这个(我认为!)是一个给你想要的结果的查询:
SELECT failures.*
FROM
failures inner join j_parts
on f_serial_number=b_serial_number
and p_important_attr = 'awesome'
UNION
SELECT failures.*
FROM
failures inner join j_parts
on f_big_assembly=a_big_assembly
and b_serial_number is null
and j_parts.bt_bicycle_type=f_bicycle_type
and p_important_attr = 'awesome'
UNION
SELECT failures.*
FROM
failures inner join j_parts
on f_big_assembly=a_big_assembly
and b_serial_number is null
and j_parts.bt_bicycle_type is null
and p_important_attr = 'awesome'
编辑:我想这样写,因为它更容易阅读和保持。然后可以优化查询。这里只有一个选择的所有条件:
SELECT failures.*
FROM
failures inner join
(parts left join parts_bicycles
on parts.p_id = parts_bicycles.b_part_id
left join assemblies
on parts.p_assembly_id=assemblies.a_assembly_id
left join assembly_bicycle_types
on assemblies.a_assembly_id =assembly_bicycle_types.bt_assembly_id)
on f_serial_number=b_serial_number
or (f_big_assembly=a_big_assembly
and b_serial_number is null
and (bt_bicycle_type=f_bicycle_type
or bt_bicycle_type is null))
and p_important_attr = 'awesome'
答案 2 :(得分:1)
以下查询返回所需的结果集。从本质上讲,这首先构建零件,装配体和自行车类型之间的关系,然后执行复杂的优先级连接,以获得实际结果的失败。
SELECT DISTINCT f.f_bicycle_type, f.f_serial_number, f.f_big_assembly
FROM parts p
LEFT JOIN parts_bicycles pb
ON p.p_id = pb.b_part_id
LEFT JOIN assemblies a
ON p.p_assembly_id = a.a_assembly_id
LEFT JOIN assembly_bicycle_types abt
ON a.a_assembly_id = abt.bt_assembly_id
LEFT JOIN failures f
ON -- First priority is parts that map directly
pb.b_serial_number = f.f_serial_number
-- Second priority is assemblies that map to type
OR (pb.b_serial_number IS NULL
AND abt.bt_bicycle_type = f.f_bicycle_type)
-- Third priority is assemblies that map directly
OR (pb.b_serial_number IS NULL
AND abt.bt_bicycle_type IS NULL
AND a.a_big_assembly = f.f_big_assembly)
WHERE p.p_important_attr = 'awesome'
ORDER BY f.f_serial_number
II不认为这解决了未分配的程序集属于所有自行车的问题,但是从样本数据中不清楚这是如何工作的。
答案 3 :(得分:1)
修改后的查询可以
SELECT f_bicycle_type, f_serial_number, f_big_assembly
FROM (
SELECT DISTINCT f.f_bicycle_type, f.f_serial_number, f.f_big_assembly, p_important_attr
from failures f
left outer join (
select distinct bt.bt_bicycle_type, b_serial_number, a_big_assembly, p_important_attr
from (
select distinct b.b_serial_number, a.a_big_assembly, p.p_assembly_id, p.p_important_attr
from parts p
join assemblies a on p.p_assembly_id = a.a_assembly_id
left outer join parts_bicycles b on b.b_part_id = p.p_id
where p.p_important_attr = 'awesome'
) p_join_a_and_b
left join assembly_bicycle_types bt on bt.bt_assembly_id = p_join_a_and_b.p_assembly_id
) p_join_a_and_b_join_bt
on f.f_big_assembly = p_join_a_and_b_join_bt.a_big_assembly
-- problem join clause - if an explicit type has not been assigned to the assembly, we want to include ALL types
and (f_bicycle_type = p_join_a_and_b_join_bt.bt_bicycle_type or p_join_a_and_b_join_bt.bt_bicycle_type is null)
-- problem join clause - there may not be explicit serial numbers assigned to a given part
and (f_serial_number = b_serial_number or b_serial_number is null)
) z
WHERE p_important_attr = 'awesome';