SQL - 过滤器中的CASE语句,可选用

时间:2015-12-16 23:37:30

标签: sql oracle case where

抱歉,我没有更好的词语来标题我的问题,但实际上我正在寻找过滤条件中的CASE / AND ... OR语句的实现,如果特定ID的值为NOT NULL且如果大于0,则只考虑该ID的条件,否则根本不需要使用该ID的条件。

示例1:

    select emp_id, cust_id, date_id
    from employee a
    join customer l
    on a.id = l.id
    join time_table ACT
    on a.join_date_id = ACT.date_id
    where a.emp_id = 1
    and l.cust_id in (2, 3)
    and ACT.date_id >= to_char((sysdate - EXTRACT(DAY FROM (SYSDATE))) - 90, 'DD-MON-YYYY')
    and ACT.date_id <= to_char(sysdate -  EXTRACT(DAY FROM (SYSDATE)), 'DD-MON-YYYY')
    and a.sub_emp_id = CASE WHEN @sub_emp_id IS NOT NULL AND @sub_emp_id > 0 THEN @sub_emp_id WHEN @sub_emp_id IS NULL THEN @sub_emp_id ELSE @sub_emp_id END -- where condition is, if sub_emp_id is not null and if sub_emp_id > 0 then use the value of sub_emp_id otherwise if sub_emp_id is NULL, then don't use the condition for sub_emp_id altogether.

示例2 -

    select emp_id, cust_id, date_id
    from employee a
    join customer l
    on a.id = l.id
    join time_table ACT
    on a.join_date_id = ACT.date_id
    where (a.emp_id = 1
    and l.cust_id in (2, 3)
    and ACT.date_id >= to_char((sysdate - EXTRACT(DAY FROM (SYSDATE))) - 90, 'DD-MON-YYYY')
    and ACT.date_id <= to_char(sysdate -  EXTRACT(DAY FROM (SYSDATE)), 'DD-MON-YYYY')
    and a.sub_emp_id > 0)
    OR
    (a.emp_id = 1
    and l.cust_id in (2, 3)
    and ACT.date_id >= to_char((sysdate - EXTRACT(DAY FROM (SYSDATE))) - 90, 'DD-MON-YYYY')
    and ACT.date_id <= to_char(sysdate -  EXTRACT(DAY FROM (SYSDATE)), 'DD-MON-YYYY')) -- where condition is, "if sub_emp_id is not null and if sub_emp_id > 0" then use the value of sub_emp_id otherwise "if sub_emp_id is NULL, then don't use the condition for sub_emp_id altogether".

3 个答案:

答案 0 :(得分:1)

如果我理解正确,您希望sub_emp_id上的过滤器看起来像这样:

...
and (@sub_emp_id is null or
     @sub_emp_id <= 0 or
     a.sub_temp_id = @sub_emp_id)

答案 1 :(得分:1)

通常你需要与&#34; all&#34;进行空匹配。对此的简单模式是:

str.count(substring)

答案 2 :(得分:1)

我认为你是为此拍摄的:

not (@sub_emp_id > 0 and a.sub_emp_id <> @sub_emp_id)
coalesce(@sub_emp_id, 0) <= 0 or a.sub_emp_id = @sub_emp_id

我认为这些是等效的选择:

public class AnimatedEditText extends EditText {
    private Paint paint;
    private Rect rect;
    private static final int ANIMATION_DURATION = 500;

    public AnimatedEditText(Context context) {
        super(context);
    }

    public AnimatedEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AnimatedEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public AnimatedEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        resetPaint(getResources().getColor(R.color.grey));
    }

    public void playAnimationInsideOut(int color) {
        getBackground().setColorFilter(null);
        resetPaint(getResources().getColor(color));

        rect.top = getMeasuredHeight() - getResources().getDimensionPixelSize(R.dimen.line_offset);
        rect.bottom = getMeasuredHeight();
        rect.left = getMeasuredWidth() / 2;
        rect.right = getMeasuredWidth() / 2;
        ValueAnimator animator = ValueAnimator.ofPropertyValuesHolder(PropertyValuesHolder.ofInt("left", rect.left, 0), PropertyValuesHolder.ofInt("right", rect.right, getMeasuredWidth()));
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                rect.left = (int) animation.getAnimatedValue("left");
                rect.right = (int) animation.getAnimatedValue("right");
            }
        });
        animator.setDuration(ANIMATION_DURATION);
        animator.start();
    }

    public void playAnimationOutsideIn() {
        getBackground().setColorFilter(getResources().getColor(R.color.green), PorterDuff.Mode.SRC_ATOP);

        rect.top = getMeasuredHeight() -     getResources().getDimensionPixelSize(R.dimen.line_offset);
        rect.bottom = getMeasuredHeight();
        rect.left = getMeasuredWidth() / 2;
        rect.right = getMeasuredWidth() / 2;
        ValueAnimator animator = ValueAnimator.ofPropertyValuesHolder(PropertyValuesHolder.ofInt("left", 0, rect.left), PropertyValuesHolder.ofInt("right",getMeasuredWidth(), rect.right ));

        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                rect.left = (int) animation.getAnimatedValue("left");
                rect.right = (int) animation.getAnimatedValue("right");

            }

        });

        animator.setDuration(ANIMATION_DURATION);
        animator.start();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(rect, paint);

        invalidate();
    }

    private void resetPaint(int color) {
        paint = new Paint();
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        paint.setColor(color);

        rect = new Rect();
        setLayerType(LAYER_TYPE_SOFTWARE, null);
    }

}