SQL Join Query基于表中的值

时间:2017-10-31 14:51:01

标签: sql-server sql-server-2008 sql-server-2012

我有两个SQL表,即员工和条件

create table employees(
name varchar(100),
deptID int,
Salary int
);

insert into employees(Name, DeptID, Salary) Values 
('Raj', 1 , 27000),
('Usha', 2 , 25000),
('Vinoth', 2 , 22000),
('Ranjith', 1 , 40000),
('Jai', 1 , 45000);


create table conditions(
deptID int,
condition varchar(MAX)
);

insert into conditions(deptID, condition) Values 
(1, 'Salary > 25000; Salary < 35000'),
(2, 'Salary = 25000'),
(1, 'Salary > 35000; Salary <= 40000');

两个表基于deptID

连接在一起

以下是预期结果

Result

Name            Salary              Condition
__________________________________________________________________
Raj             27000           Salary > 25000; Salary < 35000
Usha            25000               Salary = 25000
Vinoth          22000               NULL
Ranjith         40000           Salary > 35000; Salary <= 40000
Jai             45000               NULL

我的问题是如何生成SQL查询以生成上述结果?

在上面的结果中,当薪水与条件表中的条件匹配时,会显示条件列值。

我写了这个查询

select e.name,
   c.deptid,
   replace(replace(c.condition, 'Salary', e.salary), ';', ' and') result
   from employees e left join conditions c on e.deptid = c.deptid
   order by name, condition;

,结果是

Name     DeptID             Condition
________________________________________________
Jai             1               45000 > 25000 and 45000 < 35000
Jai             1               45000 > 35000 and 45000 <= 40000
Raj             1               27000 > 25000 and 27000 < 35000
Raj             1               27000 > 35000 and 27000 <= 40000
Ranjith         1               40000 > 25000 and 40000 < 35000
Ranjith         1               40000 > 35000 and 40000 <= 40000
Usha            2               25000 = 25000
Vinoth          2               22000 = 25000

现在我不确定如何评估条件列以获得真或假结果。有人可以通过添加condtion来查询吗?

1 个答案:

答案 0 :(得分:0)

看起来查询是针对SQL服务器的,因为Oracle中没有values子句或varachar数据类型。

但是如果你想在Oracle中评估布尔表达式,你可以使用XML特性。

with employees(Name, DeptID, Salary) as 
(select 'Raj', 1, 27000 from dual
union all select 'Usha', 2 , 25000 from dual
union all select 'Vinoth', 2, 22000 from dual
union all select 'Ranjith', 1, 40000 from dual
union all select 'Jai', 1, 45000 from dual)
, conditions(deptID, condition) as 
(select 1, 'Salary > 25000; Salary < 35000' from dual
union all select 2, 'Salary = 25000' from dual
union all select 1, 'Salary > 35000; Salary <= 40000' from dual)
select e.*,
       c.*,
       xmlquery(replace(replace(condition, 'Salary', salary), ';', ' and') returning content) result
  from employees e left join conditions c on e.deptid = c.deptid
 order by name, condition;

NAME        DEPTID     SALARY   DEPTID_1 CONDITION                       RESULT   
------- ---------- ---------- ---------- ------------------------------- ---------
Jai              1      45000          1 Salary > 25000; Salary < 35000  false    
Jai              1      45000          1 Salary > 35000; Salary <= 40000 false    
Raj              1      27000          1 Salary > 25000; Salary < 35000  true     
Raj              1      27000          1 Salary > 35000; Salary <= 40000 false    
Ranjith          1      40000          1 Salary > 25000; Salary < 35000  false    
Ranjith          1      40000          1 Salary > 35000; Salary <= 40000 true     
Usha             2      25000          2 Salary = 25000                  true     
Vinoth           2      22000          2 Salary = 25000                  false    

8 rows selected

对于更复杂的逻辑,有(是)规则管理器。

Desupport of Database Rules Manager (RUL) and Expression Filter (EXF)