从单个列中的值中选择具有多个条件的语句(Select statement with multiple rows from condition on values in single column)

编程入门 行业动态 更新时间:2024-10-28 12:31:31
从单个列中的值中选择具有多个条件的语句(Select statement with multiple rows from condition on values in single column)

我有下面的表格。以工资为条件,我想获得多行。 下面是当前的表格叫它雇员。

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

将工资与某些固定值进行比较,并且每次工资大于固定值时,都会在输出中显示一条记录。我尝试的条件案例接近于此:

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

期望的输出将是:

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

我尝试过使用union,但即使值符合1st条件,union也会为每条记录提供3行。

I have the table below.Using salary as condition I want to get multiple rows. Below is current table call it 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.

最满意答案

你的问题还不清楚,因为你的逻辑意味着你应该只有3个输出行用于3个输入行。 但是,您的输出意味着您要将薪水与某些固定值进行比较,并且每次薪水大于固定值时,都会在输出中显示一条记录。

如果前者是这种情况,Minh的查询就是你所需要的。 在后一种情况下,你可以这样做:

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

如果你想添加一个计算列,也就是一个使用这个查询中的列计算的值,你可以简单地将它作为一个列添加到select子句中,如下所示:

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

Your question is unclear, because your logic implies that you should only have 3 output rows for 3 input rows. 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. 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 i.e. one with values calculated using columns in this query, you can simply add it as a column in the select clause, like so:

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

更多推荐

本文发布于:2023-07-16 15:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1130444.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   语句   条件   statement   Select

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!