他正在尝试使用多个嵌套的案例查询来构建SQL查询。
我的表格包含以下列:ID,UserId,EventInTime,InTime,EventOutTime,OutTime。
以下是困扰我的列的伪代码:
(If EventInTime ==0 then IT = InTime else IT = EventInTime;
If EventOutTime ==0 then IT = OutTime else OT = EventOutTime;
If IT or OT ==0 then 0 else OT-IT) as Suma
继承我的代码:
SELECT
UserId,
StatId,
case when
case when EventInTime =0
then InTime
else EventInTime end=0
or
case when EventOutTime =0
then OutTime
else EventOutTime end
then 0
else
case when EventOutTime =0
then OutTime
else EventOutTime end -
case when EventInTime =0
then InTime
else EventInTime end
as suma
from Worktimes
我做错了什么?
答案 0 :(得分:1)
缺少第二个内部CASE值零的比较:
if(userName.equals("")||password.equals("")||confirmPassword.equals(""))
{
Toast.makeText(getApplicationContext(), "Field Vacant", Toast.LENGTH_LONG).show();
return;
}
// check if both password matches
if(!password.equals(confirmPassword))
{
Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
return;
}
else
{
// Save the Data in Database
loginDataBaseAdapter.insertEntryrestau(userName, password);
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
finish();
}
}
只有当两列都为零时,IT和OT才为零,因此前两个内部CASE可以简化:
SELECT UserId,
StatId,
CASE
WHEN CASE EventInTime
WHEN 0 THEN InTime
ELSE EventInTime
END = 0
OR
CASE EventOutTime
WHEN 0 THEN OutTime
ELSE EventOutTime
END = 0 -- !!!
THEN 0
ELSE CASE EventOutTime
WHEN 0 THEN OutTime
ELSE EventOutTime
END
-
CASE EventInTime
WHEN 0 THEN InTime
ELSE EventInTime
END
END AS Suma
FROM Worktimes;
使查询更简单的另一种方法是将SELECT UserId,
StatId,
CASE
WHEN (InTime = 0 AND EventInTime = 0) OR
(OutTime = 0 AND EventOutTime = 0)
THEN 0
ELSE CASE EventOutTime
WHEN 0 THEN OutTime
ELSE EventOutTime
END
-
CASE EventInTime
WHEN 0 THEN InTime
ELSE EventInTime
END
END AS Suma
FROM Worktimes;
和IT
作为虚拟列引入(这需要一个单独的子查询):
OT