如何根据条件对列名进行排序?

时间:2014-10-30 07:28:07

标签: database oracle10g oracle-sqldeveloper rdbms

我的表格包含以下列 -

  • 事件ID(varchar)
  • 创建日期(varchar- eg-' 01-OCT-2014')
  • 状态(varchar- eg-' open','关闭')

我需要根据从创建日期开始的日期和日期范围来计算事件。

例如

Date Range       || Open          || Closed ||
1-2 days         ||         5     ||       6 ||
2-5 days         ||         4     ||       8 ||
5-15 days        ||         10    ||       15 ||

任何帮助都将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:1)

SELECT CASE WHEN DATEDIFF(created_date, now()) BETWEEN 1 AND 2
                THEN '1-2 days'
            WHEN DATEDIFF(created_date, now()) BETWEEN 3 AND 5
                THEN '3-5 days'
            WHEN DATEDIFF(created_date, now()) BETWEEN 6 AND 15
                THEN '6-15 days'
            ELSE '> 15 days'
        END AS DateRange,
        SUM(CASE WHEN Status = 'open' THEN 1 ELSE 0 END) AS Open,
        SUM(CASE WHEN Status = 'closed' THEN 1 ELSE 0 END) AS Closed
FROM YourTable
GROUP BY DateRange