除非满足某些条件,否则请选择表中两个值中的较低者,然后选择较高的值

时间:2018-12-01 19:18:21

标签: sql sql-server

我有分析数据,得出两个稀释因子的结果。我想使用较低的稀释倍数,除非该结果的实验​​室限定符为E。在这些情况下,我需要使用较高的稀释倍数。下面的查询选择正确的记录,但这是我所能获得的。感谢您的帮助。

SELECT  
    s.task_code,  
    s.sys_loc_code,
    a.analyte_type, 
    a.chemical_name, 
    t.dilution_factor, 
    r.result_text, 
    r.lab_qualifiers
FROM 
    dt_sample s 
JOIN 
    dt_test t ON s.sample_id = t.sample_id AND s.facility_id = t.facility_id
JOIN 
    dt_result r ON t.test_id = r.test_id AND t.facility_id = r.facility_id  
JOIN 
    rt_analyte a ON r.cas_rn = a.cas_rn
WHERE 
    s.task_code= 'RFS01-10.1810009'
    AND s.sample_type_code IN ('F','D') 
    AND a.analyte_type IN ('Semivolatile','Volatile') 
    AND s.sys_loc_code IN ('15699', '45608', '90399', '91203', 'MOUND R1-0', '99305', 'MSETINF', '23296')
ORDER BY 
    s.sys_loc_code, a.chemical_name, t.dilution_factor;

这里是结果的示例。对于前两个记录,我想使用稀释系数= 1,但是在第二组记录中,我需要使用稀释系数= 10

task_code   sys_loc_code    analyte_type    chemical_name   dilution_factor result_text lab_qualifiers
RFS01-10.1810009    15699   Volatile    1,1,1,2-Tetrachloroethane   1   0.333   U
RFS01-10.1810009    15699   Volatile    1,1,1,2-Tetrachloroethane   10  3.33    U
RFS01-10.1810009    15699   Volatile    cis-1,2-Dichloroethene  1   324 E
RFS01-10.1810009    15699   Volatile    cis-1,2-Dichloroethene  10  282 

4 个答案:

答案 0 :(得分:0)

您应该在CASE WHEN ... THEN阶段使用SELECT运算符。看起来像

SELECT  
CASE 
WHEN r.lab_qualifiers = 'E' and t.dilution_factor_1 < t.dilution_factor_2 THEN t.dilution_factor_1 -- select lower dilution_factor value
WHEN r.lab_qualifiers = 'E' and t.dilution_factor_1 > t.dilution_factor_2 THEN t.dilution_factor_2 -- select lower dilution_factor value
WHEN r.lab_qualifiers <> 'E' and t.dilution_factor_1 < t.dilution_factor_2 THEN t.dilution_factor_2 -- select higher dilution_factor value
WHEN r.lab_qualifiers <> 'E' and t.dilution_factor_1 > t.dilution_factor_2 THEN t.dilution_factor_1 -- select higher dilution_factor value
END AS "dilution_factor"
FROM 
    dt_sample AS s 
    JOIN ...
    WHERE ...
    ORDER BY ...

因此,您需要更清楚地表示“较高”和“较低”值的含义,然后SELECT的一行仅返回一个值。您要从不同的行中选择稀释因子的值吗?

答案 1 :(得分:0)

这是一个优先级查询。如果我理解正确:

with t as (<your query here without the order by>)
select t.*
from (select t.*,
             sum(case when lab_qualifiers = 'E' then 1 else 0 end) over (partition by task_code, sys_loc_code, analyte_type, chemical_name) as numEs,
             row_number() over (partition by task_code, sys_loc_code,    analyte_type, chemical_name
                                order by dilution_factor) as seqnum,
             row_number() over (partition by task_code, sys_loc_code,    analyte_type, chemical_name
                                order by dilution_factor desc) as seqnum_desc
      from t
     ) t
where (numEs = 0 and seqnum = 1) or
      (numEs > 0 and seqnum_desc = 1);

答案 2 :(得分:0)

只需将那些具有luting_fact = E的记录作为较低的排名值(2),将其余的记录作为(1)进行排名,然后按以下的稀释因子进行排序

with data
  as ( 
SELECT  
    s.task_code,  
    s.sys_loc_code,
    a.analyte_type, 
    a.chemical_name, 
    t.dilution_factor, 
    r.result_text, 
    r.lab_qualifiers
FROM 
    dt_sample s ON f.facility_id = s.facility_id
JOIN 
    dt_test t ON s.sample_id = t.sample_id AND s.facility_id = t.facility_id
JOIN 
    dt_result r ON t.test_id = r.test_id AND t.facility_id = r.facility_id  
JOIN 
    rt_analyte a ON r.cas_rn = a.cas_rn
WHERE 
    s.task_code= 'RFS01-10.1810009'
    AND s.sample_type_code IN ('F','D') 
    AND a.analyte_type IN ('Semivolatile','Volatile') 
    AND s.sys_loc_code IN ('15699', '45608', '90399', '91203', 'MOUND R1-0', '99305', 'MSETINF', '23296')
  )
,ranked_results
  as (
select *
       ,row_number() over(partition by task_code
                                       ,sys_loc_code
                                       ,analyte_type
                                       ,chemical_name
                            order by case when lab_qualifiers<>'E' then 1 else 2
                                     end,dilution_factor) as rnk
  from data
)
select * 
  from ranked_results
  where rnk=1

答案 3 :(得分:0)

仅将SELECT中的t.dilution_factor更改为

(在r.lab_qualifiers ='E'然后是10否则为1的情况下)

SELECT  
    s.task_code,  
    s.sys_loc_code,
    a.analyte_type, 
    a.chemical_name, 
    (CASE WHEN r.lab_qualifiers='E' THEN 10 ELSE 1) dilution_factor, 
    r.result_text, 
    r.lab_qualifiers
FROM 
    dt_sample s 
JOIN 
    dt_test t ON s.sample_id = t.sample_id AND s.facility_id = t.facility_id
JOIN 
    dt_result r ON t.test_id = r.test_id AND t.facility_id = r.facility_id  
JOIN 
    rt_analyte a ON r.cas_rn = a.cas_rn
WHERE 
    s.task_code= 'RFS01-10.1810009'
    AND s.sample_type_code IN ('F','D') 
    AND a.analyte_type IN ('Semivolatile','Volatile') 
    AND s.sys_loc_code IN ('15699', '45608', '90399', '91203', 'MOUND R1-0', '99305', 'MSETINF', '23296')
ORDER BY 
    s.sys_loc_code, a.chemical_name, t.dilution_factor;