I have two tables with a one-to-many relationship, like so:
CREATE TABLE samples (
id BIGSERIAL,
name character varying(250) NOT NULL,
create_date timestamp without time zone DEFAULT NOW(),
PRIMARY KEY (id),
);
CREATE TABLE sample_compounds (
id BIGSERIAL,
sample_id integer ,
compound_name character varying(250) NULL,
is_mixture_analysis boolean NULL,
score double precision NULL,
PRIMARY KEY (id),
CONSTRAINT sample_compounds_sample_id_fkey
FOREIGN KEY (sample_id)
REFERENCES samples(id)
);
I need to join the two tables and select one row from sample_compounds based on certain conditions. In pseudo SQL, I'd imagine it would be something like this, but the multiple conditions has me confused about where to start.
SELECT s.id, s.name, s.create_date, sc.compound_name
FROM samples s
LEFT JOIN LATERAL (
SELECT id, sample_id, compound_name, score, is_mixture_analysis
FROM sample_compounds
WHERE
condition1
OR condition2
OR condition3
LIMIT 1
) sc ON sc.sample_id=s.id
Here are the conditions I need to have...
Condition 1: If is_mixture_analysis has true values only, select the one with the highest score. For example, if the joining data from sample_compounds looked like this:
compound_name | score | is_mixture_analysis
----------------------------------------------------+-------+---------------------
Soluble Starch | 97 | t
Starch | 96 | t
Illite, Morris IL, | 95 | t
Acacia gum | 94 | t
Attapalgite, crude, Attapalgus, GA | 62 | t
The answer should be: Soluble Starch.
Condition 2: If is_mixture_analysis has false values only, select the one with the highest score. For example, if the joining data from sample_compounds looked like this:
compound_name | score | is_mixture_analysis
------------------------------------------+-------+---------------------
Nail Polish Remover (Methyl Acetate) | 96 | f
4-Styrenesulfonic acid | 95 | f
Dowex | 94 | f
1,6-bis-(p-Methyl phenyl)-1,6-hexanediol | 94 | f
The answer should be: Nail Polish Remover (Methyl Acetate).
Condition 3: If is_mixture_analysis has both true and false values, select the row with the highest score where is_mixture_analsis is false but only if it has a score >= 95, else select the row with the highest score where is_mixture_analysis is true. For example, if the joining data from sample_compounds looked like this:
compound_name | score | is_mixture_analysis
--------------------------------------+-------+---------------------
Adenosine triphosphate disodium salt | 98 | f
Stannous sulfate | 97 | f
Vanadyl sulfate | 96 | f
Cerium IV sulfate | 100 | t
The answer should be: Adenosine triphosphate disodium salt.
But, if the data looked like this:
compound_name | score | is_mixture_analysis
--------------------------------------+-------+---------------------
Adenosine triphosphate disodium salt | 94 | f
Stannous sulfate | 93 | f
Vanadyl sulfate | 92 | f
Cerium IV sulfate | 100 | t
Cerium sulfate | 99 | t
The answer should be: Cerium IV sulfate.
Any help on this would be greatly appreciated!