如何根据Oracle中的Case语句使用Group By? [英] How do I use Group By based on a Case statement in Oracle?

查看:350
本文介绍了如何根据Oracle中的Case语句使用Group By?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Oracle CASE 的SQL查询来比较日期列是小于还是大于当前日期。但是,如何在 GROUP BY -statement中使用 CASE -statement?我想在每种情况下计数记录。

I have an SQL-query where I use Oracle CASE to compare if a date column is less than or greater than current date. But how do I use that CASE-statement in a GROUP BY-statement? I would like to count the records in each case.

例如

E.g.

select
    (case
        when exp_date > sysdate then 1
        when exp_date <= sysdate then 2
        else 3
     end) expired, count(*)
from mytable
group by expired

但我在尝试此操作时遇到错误: ORA-00904 。任何建议?

But I get an error when trying this: ORA-00904. Any suggestions?

推荐答案

select
(case
    when exp_date > sysdate then 1
    when exp_date <= sysdate then 2
    else 3
 end) expired, count(*)
from mytable
group by (case
    when exp_date > sysdate then 1
    when exp_date <= sysdate then 2
    else 3
 end)

这篇关于如何根据Oracle中的Case语句使用Group By?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆