Eu tenho uma tabela com prazos de entrega de tarefas, que é baseada no departamento de tarefas (apenas), categoria de tarefas (apenas) ou tarefa e categoria do produto. Essas regras podem ser aplicadas diretamente a um cliente, um grupo de clientes ou a todos os clientes.
Exemplo:
- Cliente 01 + Departamento: Design + Categoria: Criar Logo = 24 horas
- Cliente 01 + Departamento: Design + Qualquer Categoria = 48 horas Todos
- Clientes + Departamento: Design + Qualquer Categoria = 72 horas Todos
- Clientes + Qualquer Categoria + Categoria: Criar Logo = 48 horas
Esta tabela tem a estrutura abaixo:
|rule_id|customer_id|departament_id|category_id|deadline|
SQL para criar tabelas:
CREATE TABLE public.test_customer (
id INTEGER NOT NULL,
name varchar(50) NOT NULL
);
CREATE TABLE public.test_departament (
id INTEGER NOT NULL,
name varchar(50) NOT NULL
);
CREATE TABLE public.test_category (
id INTEGER NOT NULL,
name varchar(50) NOT NULL
);
CREATE TABLE public.test_task (
id INTEGER NOT NULL,
customer_id INTEGER,
departament_id INTEGER,
category_id INTEGER,
description varchar(50) NOT NULL
);
CREATE TABLE public.test_time_rule (
id INTEGER NOT NULL,
customer_id INTEGER,
departament_id INTEGER,
category_id INTEGER,
time_deadline INTEGER
);
INSERT INTO public.test_customer (id, name) VALUES(1, 'Customer with Rule');
INSERT INTO public.test_customer (id, name) VALUES(2, 'Customer without Rule');
INSERT INTO public.test_departament (id, name) VALUES(1, 'Front-End');
INSERT INTO public.test_departament (id, name) VALUES(2, 'Back-End');
INSERT INTO public.test_departament (id, name) VALUES(3, 'DBA');
INSERT INTO public.test_departament (id, name) VALUES(1, 'Design');
INSERT INTO public.test_departament (id, name) VALUES(2, 'Create HTML Pages');
INSERT INTO public.test_departament (id, name) VALUES(3, 'Create Tables');
INSERT INTO public.test_departament (id, name) VALUES(4, 'Fix Bugs');
INSERT INTO public.test_task (id, customer_id, departament_id, category_id, description) VALUES(1, 1, 1, 1, 'Create New Form Design');
INSERT INTO public.test_task (id, customer_id, departament_id, category_id, description) VALUES(2, 1, 2, 4, 'Fix Bug on Customer Table');
INSERT INTO public.test_task (id, customer_id, departament_id, category_id, description) VALUES(2, 1, 3, 3, 'Create City Table');
INSERT INTO public.test_task (id, customer_id, departament_id, category_id, description) VALUES(3, 2, 1, 1, 'Create New Form Design');
INSERT INTO public.test_task (id, customer_id, departament_id, category_id, description) VALUES(4, 2, 3, 3, 'Create City Table');
INSERT INTO public.test_time_rule (id, customer_id, departament_id, category_id, time_deadline) VALUES(1, NULL, NULL, NULL, 20); --All Customers
INSERT INTO public.test_time_rule (id, customer_id, departament_id, category_id, time_deadline) VALUES(2, NULL, 1, NULL, 30); --All Customers + Departament
INSERT INTO public.test_time_rule (id, customer_id, departament_id, category_id, time_deadline) VALUES(3, NULL, NULL, 1, 40); --All Customers + Category
INSERT INTO public.test_time_rule (id, customer_id, departament_id, category_id, time_deadline) VALUES(4, NULL, 3, 3, 50); --All Customers + Departament + Category
INSERT INTO public.test_time_rule (id, customer_id, departament_id, category_id, time_deadline) VALUES(5, 1, NULL, NULL, 20); --Customers With Rule
INSERT INTO public.test_time_rule (id, customer_id, departament_id, category_id, time_deadline) VALUES(6, 1, 1, NULL, 30); --Customers + Departament
INSERT INTO public.test_time_rule (id, customer_id, departament_id, category_id, time_deadline) VALUES(7, 1, NULL, 1, 40); --Customers + Category
INSERT INTO public.test_time_rule (id, customer_id, departament_id, category_id, time_deadline) VALUES(8, 1, 3, 3, 50); --Customers + Departament + Category
Estou tentando criar um SELECT
que liste todas as tarefas e mostre qual regra se aplica a ele. Para isso estou utilizando os níveis de aplicação da regra, que são:
1 - Regra com cliente + departamento + categoria (ID de cliente definido)
2 - Regra com grupo de clientes + departamento + categoria (ID do grupo de clientes definido)
3 - Regra geral (todos os clientes) + departamento + categoria (sem cliente definido e ID do grupo de clientes)
4 - Regra com cliente + departamento
Eu resumi as regras, mas elas chegam a 16 regras, e criei uma view para cada regra com as tarefas que correspondem aos seus parâmetros.
Com base nessas regras criei um SELECT
com vários UNION ALL
(motivos de performance), mas como a linha pode ser repetida devido a um select anterior, acabei adicionando NOT EXISTS
condições para evitar esse problema.
Exemplo:
SELECT
1 as level,
ttr.id as rule_id,
ttr.customer_id as rule_customer_id,
tt.customer_id as task_customer_id,
ttr.departament_id as rule_departament_id,
tt.departament_id as task_departament_id,
ttr.category_id as rule_category_id,
tt.category_id as task_category_id,
tt.id as task_id,
tt.description as task_description,
ttr.time_deadline
FROM
test_time_rule ttr,
test_task tt
WHERE
ttr.customer_id IS NOT NULL
AND ttr.departament_id IS NOT NULL
AND ttr.category_id IS NOT NULL
AND tt.customer_id = ttr.customer_id
AND tt.departament_id = ttr.departament_id
AND tt.category_id = ttr.category_id
UNION
SELECT
2 as level,
ttr.id as rule_id,
ttr.customer_id as rule_customer_id,
tt.customer_id as task_customer_id,
ttr.departament_id as rule_departament_id,
tt.departament_id as task_departament_id,
ttr.category_id as rule_category_id,
tt.category_id as task_category_id,
tt.id as task_id,
tt.description as task_description,
ttr.time_deadline
FROM
test_time_rule ttr,
test_task tt
WHERE
ttr.customer_id IS NULL
AND ttr.departament_id IS NOT NULL
AND ttr.category_id IS NOT NULL
AND tt.departament_id = ttr.departament_id
AND tt.category_id = ttr.category_id
UNION
SELECT
3 as level,
ttr.id as rule_id,
ttr.customer_id as rule_customer_id,
tt.customer_id as task_customer_id,
ttr.departament_id as rule_departament_id,
tt.departament_id as task_departament_id,
ttr.category_id as rule_category_id,
tt.category_id as task_category_id,
tt.id as task_id,
tt.description as task_description,
ttr.time_deadline
FROM
test_time_rule ttr,
test_task tt
WHERE
ttr.customer_id IS NOT NULL
AND ttr.departament_id IS NOT NULL
AND ttr.category_id IS NULL
AND tt.customer_id = ttr.customer_id
AND tt.departament_id = ttr.departament_id
UNION
SELECT
4 as level,
ttr.id as rule_id,
ttr.customer_id as rule_customer_id,
tt.customer_id as task_customer_id,
ttr.departament_id as rule_departament_id,
tt.departament_id as task_departament_id,
ttr.category_id as rule_category_id,
tt.category_id as task_category_id,
tt.id as task_id,
tt.description as task_description,
ttr.time_deadline
FROM
test_time_rule ttr,
test_task tt
WHERE
ttr.customer_id IS NULL
AND ttr.departament_id IS NOT NULL
AND ttr.category_id IS NULL
AND tt.departament_id = ttr.departament_id
UNION
SELECT
5 as level,
ttr.id as rule_id,
ttr.customer_id as rule_customer_id,
tt.customer_id as task_customer_id,
ttr.departament_id as rule_departament_id,
tt.departament_id as task_departament_id,
ttr.category_id as rule_category_id,
tt.category_id as task_category_id,
tt.id as task_id,
tt.description as task_description,
ttr.time_deadline
FROM
test_time_rule ttr,
test_task tt
WHERE
ttr.customer_id IS NOT NULL
AND ttr.departament_id IS NULL
AND ttr.category_id IS NOT NULL
AND tt.customer_id = ttr.customer_id
AND tt.category_id = ttr.category_id
UNION
SELECT
6 as level,
ttr.id as rule_id,
ttr.customer_id as rule_customer_id,
tt.customer_id as task_customer_id,
ttr.departament_id as rule_departament_id,
tt.departament_id as task_departament_id,
ttr.category_id as rule_category_id,
tt.category_id as task_category_id,
tt.id as task_id,
tt.description as task_description,
ttr.time_deadline
FROM
test_time_rule ttr,
test_task tt
WHERE
ttr.customer_id IS NULL
AND ttr.departament_id IS NULL
AND ttr.category_id IS NOT NULL
AND tt.category_id = ttr.category_id
UNION
SELECT
7 as level,
ttr.id as rule_id,
ttr.customer_id as rule_customer_id,
tt.customer_id as task_customer_id,
ttr.departament_id as rule_departament_id,
tt.departament_id as task_departament_id,
ttr.category_id as rule_category_id,
tt.category_id as task_category_id,
tt.id as task_id,
tt.description as task_description,
ttr.time_deadline
FROM
test_time_rule ttr,
test_task tt
WHERE
ttr.customer_id IS NOT NULL
AND ttr.departament_id IS NULL
AND ttr.category_id IS NULL
AND tt.customer_id = ttr.customer_id
UNION
SELECT
8 as level,
ttr.id as rule_id,
ttr.customer_id as rule_customer_id,
tt.customer_id as task_customer_id,
ttr.departament_id as rule_departament_id,
tt.departament_id as task_departament_id,
ttr.category_id as rule_category_id,
tt.category_id as task_category_id,
tt.id as task_id,
tt.description as task_description,
ttr.time_deadline
FROM
test_time_rule ttr,
test_task tt
WHERE
ttr.customer_id IS NULL
AND ttr.departament_id IS NULL
AND ttr.category_id IS NULL
ORDER BY
level;
Como você deve ter notado a cada nível que ele entra, todas as visualizações anteriores são adicionadas, o que gera um SQL grande e lento.
O meu bom senso diz-me que esta não é a melhor forma de fazer esta consulta.
Considerei algo que ignora uma linha com base no ID da tarefa já exibido em uma união anterior. Existe uma maneira melhor de criar este SELECT?
Observação: não posso adicionar o ID da regra usada na tarefa, pois essa regra pode ser alterada, excluída ou até adicionada no futuro a uma regra que melhor corresponda à tarefa.
Se você quer
task_id
ser único, que tal algo como:Isso retornará a primeira linha para cada um
task_id
na ordenação especificada.