如果两列是 1 则创建新列
Posted
技术标签:
【中文标题】如果两列是 1 则创建新列【英文标题】:If two columns are 1 then make new column 【发布时间】:2014-08-08 05:39:08 【问题描述】:我需要一个我的 sql 语句来选择类似的东西
SELECT present, wholeday, attendance
present
和wholeday
是给定的,而出勤是由现在和全天的组合产生的
if present == 1 and wholeday == 1 then attendance = 1
if present == 1 and wholeday == 0 then attendance = .5
if present == 0 and wholeday == 0 then attendance = 0
【问题讨论】:
所以您希望我们帮助您而不需要您的任何努力? 您已经掌握了逻辑,是什么阻碍了您编写 SQL 查询? 既然没有 case for present=0, wholeday=1,(present + wholeday)/2.0
应该怎么做?
【参考方案1】:
您的查询将是:
SELECT PRESENT, WHOLEDAY,
CASE
WHEN (PRESENT = 1 AND WHOLEDAY = 1) THEN 1
WHEN (PRESENT = 1 AND WHOLEDAY = 0) THEN 0.5
ELSE 0
END as ATTENDANCE
FROM MY_TABLE
Case 语法是:
CASE
WHEN condition_1 THEN commands
WHEN condition_2 THEN commands
...
ELSE commands
END CASE;
【讨论】:
以上是关于如果两列是 1 则创建新列的主要内容,如果未能解决你的问题,请参考以下文章