简体   繁体   English

从单列值的条件中选择具有多行的语句

[英]Select statement with multiple rows from condition on values in single column

I have the table below.Using salary as condition I want to get multiple rows. 我有下表。使用薪水作为条件,我想获得多行。 Below is current table call it employee. 下面是当前表,称为employee。

    empid     name         salary
-----------------------------------
1   A1        alex        20000
2   B2        ben         4500
3   C1        carl        14000

compare the salary to certain fixed values, and every time the salary is larger than the fixed value, show a record in output.My attempt condition case is close to this: 将薪水与某些固定值进行比较,每当薪水大于固定值时,在输出中显示一条记录。我的尝试条件情况与此相似:

   incometype= case When salary<6000 then 101 When salary Between 6000 And 18000 Then
   102 Else 103 End

Desired ouput would be: 期望的输出将是:

     empid     name   salary  incometype
------------------------------------------
1   A1        alex    20000    101
2   A1        alex    20000    102
3   A!        alex    20000    103
4   B2        ben     4500     101
5   C1        carl    14000    101
6   C1        carl    14000    102

I have tried using union but union will give me 3 rows for each record even when value meets 1st condition. 我曾尝试使用union,但是union会给我每条记录3行,即使值满足第一个条件。

Your question is unclear, because your logic implies that you should only have 3 output rows for 3 input rows. 您的问题尚不清楚,因为您的逻辑暗示您应该只对3个输入行有3个输出行。 Your output however implies that you want to compare the salary to certain fixed values, and every time the salary is larger than the fixed value, show a record in output. 但是,您的输出表示您希望将薪水与某些固定值进行比较,并且每当薪水大于固定值时,就在输出中显示一条记录。

If the former is the case, Minh's query is all you need. 如果是前一种情况,那么Minh的查询就是您所需要的。 In the latter case, you can do something like this: 在后一种情况下,您可以执行以下操作:

select e.*, m.incometype
from employee e
left join 
( 
 select 0 as threshold, 101 as incometype
 union
 select 5999 as threshold, 102 as incometype
 union
 select 17999 as threshold, 103 as incometype
) m
on e.salary > m.threshold
order by e.empid

If you want to add a calculate column ie one with values calculated using columns in this query, you can simply add it as a column in the select clause, like so: 如果要添加一个计算列,即在查询中使用该列计算值的列,则可以将其简单地添加为select子句中的列,如下所示:

select e.*, 
m.incometype, 
case 
when <first condition> then <business logic here>
....
else <handle default case>
end as yourcomputedcolumn
from
...

This returns 3 rows and enough for your need: 这将返回3行,足以满足您的需要:

  SELECT empid, name, salary, 
    case When salary<6000 then 101 
         When salary Between 6000 And 18000 Then 102 
         Else 103 End as incometype
    FROM employee;

Not very clear on the requirement, however the following worked for me: 要求不是很明确,但是以下对我有用:

Select 
    EmpId,Name,Sal,101 IncomeType
from Emp
Union all
Select 
    EmpId,Name,Sal,102
from Emp
Where Sal > 6000
union all
Select 
    EmpId,Name,Sal,103
from Emp
Where Sal > 18000;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM