具有多个条件的MySQL查询在同一列上

时间:2017-11-02 15:04:03

标签: mysql sql

`

Select peo.employee_number,
 bpf.name plan_name,
 typ.name plan_type,
 ben.effective_start_date,
 ben.effective_end_date,
 ben.enrt_cvg_strt_dt,
 ben.enrt_cvg_thru_dt
 from apps.ben_prtt_enrt_rslt_f   ben,
 apps.ben_pl_f               bpf,
 apps.ben_pl_typ_f           typ,
 apps.per_all_people_f       peo
 where (ben.enrt_cvg_thru_dt >= '01-JAN-2017' and typ.name ='Choice Health')
 and (ben.enrt_cvg_strt_dt >= ben.enrt_cvg_thru_dt and typ.name ='Waive 
 Health') 
 and ben.person_Id = peo.person_id
 and ben.pl_id = bpf.pl_id
 and typ.name ='choice Health' or 'waive health'
 and typ.pl_typ_id = ben.pl_typ_id`

我需要检索employees所有coverage_thru_date >= 01-JAN-2017并注册plan_name ='Choice Health'的所有人coverage_start_dt >= coverage_thru_dt以及plan_name='Waive Health'并注册require 'rubygems' require 'roo' class InputExcelReader $INPUTPATH = 'C:\test_input_excel.xlsx' excel_data_array = Array.new() workbook = Roo::Spreadsheet.open($INPUTPATH) worksheets = workbook.sheets puts worksheets puts "Found #{worksheets.count} worksheets" worksheets.each do |worksheet| puts "Reading: #{worksheet}" num_rows = 0 workbook.sheet(worksheet).each_row_streaming do |row| if(num_rows>0) puts "Reading the row no: #{num_rows}" row_cells = row.map { |cell| puts "Reading cells" cell.value } excel_data_array.push(row_cells) end num_rows += 1 end puts excel_data_array.to_s end end <的人/ p>

1 个答案:

答案 0 :(得分:1)

试试这个。

SELECT * 
FROM employees 
WHERE (coverage_thru_date >= TO_DATE('01-JAN-2017')
      AND plan_name ='Choice Health')
      OR
      (coverage_start_dt >= coverage_thru_dt
       AND plan_name='Waive Medical');

--------------REVISED BASED ON SUPPLIED QUERY--------------
SELECT peo.employee_number, 
       bpf.name plan_name, 
       typ.name plan_type, 
       ben.effective_start_date, 
       ben.effective_end_date, 
       ben.enrt_cvg_strt_dt, 
       ben.enrt_cvg_thru_dt 
FROM   apps.ben_prtt_enrt_rslt_f ben, 
       apps.ben_pl_f bpf, 
       apps.ben_pl_typ_f typ, 
       apps.per_all_people_f peo 
WHERE    ( ben.enrt_cvg_thru_dt >= '01-JAN-2017' 
          AND typ.name = 'Choice Health' ) 
       OR 
         ( ben.enrt_cvg_strt_dt >= ben.enrt_cvg_thru_dt 
          AND typ.name = 'Waive Health' ) 
       AND ben.person_id = peo.person_id 
       AND ben.pl_id = bpf.pl_id 
       AND typ.pl_typ_id = ben.pl_typ_id;