Select
Max(isnull(UpdateDate, InsertDate)) as A
From yourTable
Where UpdateCode = 'ABC' or InsertCode = 'ABC'
Select
Max(isnull(UpdateDate, InsertDate)) as B
From yourTable
Where UpdateCode = 'ZYX' or InsertCode = 'ZYX'
我想在一个查询中进行这两个查询。 所以我就这样做了
Select
Max(isnull(UpdateDate, InsertDate)) as A,
Max(isnull(UpdateDate, InsertDate)) as B
From yourTable
Where (UpdateCode = 'ZYX' or InsertCode = 'ZYX')
or (UpdateCode = 'ABC' or InsertCode = 'ABC')
此查询会带来错误的数据。
答案 0 :(得分:1)
在您的查询中A
和B
是完全相同的字词,因此它们将具有完全相同的值。
处理此问题的一种方法是在case
调用中的max
表达式中添加逻辑:
SELECT MAX(CASE WHEN 'ABC' IN (InsertCode, UpdateCode)
THEN ISNULL(UpdateDate, InsertDate)
END) AS a
MAX(CASE WHEN 'XYZ' IN (InsertCode, UpdateCode)
THEN ISNULL(UpdateDate, InsertDate)
END) AS a
FROM YourTable
-- Where clause is an optimization, functionality would be correct even without it
WHERE 'ABC' IN (InsertCode, UpdateCode) OR
'XYZ' IN (InsertCode, UpdateCode)
答案 1 :(得分:0)
我相信您想要有条件地计算每个MAX
。
Select
Max(CASE WHEN UpdateCode = 'ZYX' or InsertCode = 'ZYX' THEN isnull(UpdateDate, InsertDate) END) as A,
Max(CASE WHEN UpdateCode = 'ABC' or InsertCode = 'ABC' THEN isnull(UpdateDate, InsertDate) END) as B
From
yourTable
Where
UpdateCode IN ('ZYX', 'ABC') or InsertCode IN ('ZYX', 'ABC')
答案 2 :(得分:0)
一种非常简单的方法是将两个语句与“联盟选择”结合起来。
否则你必须清楚地描述你对结果的期望。
答案 3 :(得分:0)
查询本身似乎有点奇怪,但我相信你有两个选择;
首先使用“IN”语句允许您的where条件能够满足多个条件
即
其次是使用JOIN来使两个字段匹配 - 我认为这更倾向于你的目标
private void registerOnTickReceiver() {
mMinuteTickReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent){
Intent timeTick=new Intent(LifeTimerClockWidget.ACTION_TICK);
// for Android 8 send an explicit broadcast
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
sendImplicitBroadcast(context, timeTick);
else
sendBroadcast(timeTick);
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(mMinuteTickReceiver, filter);
}