简体   繁体   English

SQL如何计算案例

[英]SQL How to count case

I am trying to case out a select statement. 我试图说明一个选择声明。 However, there are multiple results for 7am 8am 9am etc. I'd like the results to show: 但是,早上7点8点9点等有多个结果。我希望结果显示:

Count Time 数时间
10 7am 上午10点半
6 8am 早上6点8分

etc. Instead, it is showing all the 7ams all the 8ams etc. 相反,它显示所有8ams所有8ams等。

select
case
when aq.datestarted between to_date( '22-MAY-2014 07:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 " 07:59:59', 'dd-mon-yyyy hh24:mi:ss' ) then '7am'
when aq.datestarted between to_date( '22-MAY-2014 08:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 " 08:59:59', 'dd-mon-yyyy hh24:mi:ss' ) then '8am'
when aq.datestarted between to_date( '22-MAY-2014 09:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 " 09:59:59', 'dd-mon-yyyy hh24:mi:ss' ) then '9am'
when aq.datestarted between to_date( '22-MAY-2014 10:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 " 10:59:59', 'dd-mon-yyyy hh24:mi:ss' ) then '10am'
when aq.datestarted between to_date( '22-MAY-2014 11:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 " 11:59:59', 'dd-mon-yyyy hh24:mi:ss' ) then '11am'
END as Time
from archivedqueue aq
where aq.datestarted between to_date( '22-MAY-2014 07:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 " 11:59:59', 'dd-mon-yyyy hh24:mi:ss' )
select COUNT(*), Time
FROM
(SELECT
    case
    when aq.datestarted between to_date( '22-MAY-2014 07:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 " 07:59:59', 'dd-mon-yyyy hh24:mi:ss' ) then '7am'
    when aq.datestarted between to_date( '22-MAY-2014 08:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 " 08:59:59', 'dd-mon-yyyy hh24:mi:ss' ) then '8am'
    when aq.datestarted between to_date( '22-MAY-2014 09:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 " 09:59:59', 'dd-mon-yyyy hh24:mi:ss' ) then '9am'
    when aq.datestarted between to_date( '22-MAY-2014 10:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 " 10:59:59', 'dd-mon-yyyy hh24:mi:ss' ) then '10am'
    when aq.datestarted between to_date( '22-MAY-2014 11:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 " 11:59:59', 'dd-mon-yyyy hh24:mi:ss' ) then '11am'
    END as Time
    from archivedqueue aq
    where aq.datestarted between to_date( '22-MAY-2014 07:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 " 11:59:59', 'dd-mon-yyyy hh24:mi:ss' )) tempTable
GROUP BY Time

Group by Time will combine all the 7ams,8ams, together. Group by Time将所有7个ams8ms组合在一起。 Count(*) counts the number of rows in each group. Count(*)计算每个组中的行数。

SELECT COUNT(*) AS Count, Time
  FROM (SELECT case
               when aq.datestarted between to_date( '22-MAY-2014 07:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 07:59:59', 'dd-mon-yyyy hh24:mi:ss' ) then '7am'
               when aq.datestarted between to_date( '22-MAY-2014 08:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 08:59:59', 'dd-mon-yyyy hh24:mi:ss' ) then '8am'
               when aq.datestarted between to_date( '22-MAY-2014 09:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 09:59:59', 'dd-mon-yyyy hh24:mi:ss' ) then '9am'
               when aq.datestarted between to_date( '22-MAY-2014 10:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 10:59:59', 'dd-mon-yyyy hh24:mi:ss' ) then '10am'
               when aq.datestarted between to_date( '22-MAY-2014 11:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 11:59:59', 'dd-mon-yyyy hh24:mi:ss' ) then '11am'
               END as Time
          FROM archivedqueue aq
         WHERE aq.datestarted between to_date( '22-MAY-2014 07:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 11:59:59', 'dd-mon-yyyy hh24:mi:ss' )
       ) name_required
 GROUP BY Time
 ORDER BY Time;

The sub-query is what you wrote (apart from a fixup in the second TO_DATE in the various BETWEEN conditions — it is easier to spot problems when you can see the code without horizontal scrolling!), marginally reformatted. 子查询是您编写的(除了在各种BETWEEN条件下的第二个TO_DATE中的修复 - 当您可以看到没有水平滚动的代码时更容易发现问题!),稍微重新格式化。 Note that the ORDER BY is imperfect because the sort is text-based (so the 10am and 11am entries will appear before the 7am to 9am entries). 请注意,ORDER BY不完美,因为排序是基于文本的(所以10am11am条目将出现在7am9am之前)。 If you don't like that, use a text-sortable format such as 07 or 0700 or 0700-07:59 instead of the 'am'-tagged times. 如果您不喜欢这样,请使用文本可排序的格式,例如0707000700-07:59而不是'am'标记的时间。

This is not extensible. 这不可扩展。 You need to work harder on what's in the CASE statement. 您需要更加努力地处理CASE语句中的内容。

Maybe: 也许:

TO_CHAR(aq.datestarted, 'yyyy-mm-dd hh24:00') AS time

The restriction condition in the WHERE clause then provides the correct limits on the search. 然后,WHERE子句中的限制条件为搜索提供了正确的限制。

SELECT COUNT(*) AS Count, TO_CHAR(aq.datestarted, 'yyyy-mm-dd hh:00') AS time
  FROM archivedqueue aq
 WHERE aq.datestarted BETWEEN to_date('22-MAY-2014 07:00:00', 'dd-mon-yyyy hh24:mi:ss')
                          AND to_date('22-MAY-2014 11:59:59', 'dd-mon-yyyy hh24:mi:ss')
 GROUP BY Time
 ORDER BY Time;

To get it working how you'd expect, use what's below... However, there's probably a better way than hardcoding dates and times. 为了让它按照您的预期运行,请使用以下内容...但是,可能有一种比硬编码日期和时间更好的方法。

SELECT COUNT(*) AS 'Count', 
        CASE 
            WHEN aq.datestarted BETWEEN to_date( '22-MAY-2014 07:00:00', 'dd-mon-yyyy hh24:mi:ss' ) AND to_date( '22-MAY-2014 " 07:59:59', 'dd-mon-yyyy hh24:mi:ss' ) THEN '7am'
            WHEN aq.datestarted BETWEEN to_date( '22-MAY-2014 08:00:00', 'dd-mon-yyyy hh24:mi:ss' ) AND to_date( '22-MAY-2014 " 08:59:59', 'dd-mon-yyyy hh24:mi:ss' ) THEN '8am'
            WHEN aq.datestarted BETWEEN to_date( '22-MAY-2014 09:00:00', 'dd-mon-yyyy hh24:mi:ss' ) AND to_date( '22-MAY-2014 " 09:59:59', 'dd-mon-yyyy hh24:mi:ss' ) THEN '9am'
            WHEN aq.datestarted BETWEEN to_date( '22-MAY-2014 10:00:00', 'dd-mon-yyyy hh24:mi:ss' ) AND to_date( '22-MAY-2014 " 10:59:59', 'dd-mon-yyyy hh24:mi:ss' ) THEN '10am'
            WHEN aq.datestarted BETWEEN to_date( '22-MAY-2014 11:00:00', 'dd-mon-yyyy hh24:mi:ss' ) AND to_date( '22-MAY-2014 " 11:59:59', 'dd-mon-yyyy hh24:mi:ss' ) THEN '11am'
        END AS 'Time'
FROM archivedqueue aq
WHERE aq.datestarted BETWEEN to_date( '22-MAY-2014 07:00:00', 'dd-mon-yyyy hh24:mi:ss' 
GROUP BY CASE 
            WHEN aq.datestarted BETWEEN to_date( '22-MAY-2014 07:00:00', 'dd-mon-yyyy hh24:mi:ss' ) AND to_date( '22-MAY-2014 " 07:59:59', 'dd-mon-yyyy hh24:mi:ss' ) THEN '7am'
            WHEN aq.datestarted BETWEEN to_date( '22-MAY-2014 08:00:00', 'dd-mon-yyyy hh24:mi:ss' ) AND to_date( '22-MAY-2014 " 08:59:59', 'dd-mon-yyyy hh24:mi:ss' ) THEN '8am'
            WHEN aq.datestarted BETWEEN to_date( '22-MAY-2014 09:00:00', 'dd-mon-yyyy hh24:mi:ss' ) AND to_date( '22-MAY-2014 " 09:59:59', 'dd-mon-yyyy hh24:mi:ss' ) THEN '9am'
            WHEN aq.datestarted BETWEEN to_date( '22-MAY-2014 10:00:00', 'dd-mon-yyyy hh24:mi:ss' ) AND to_date( '22-MAY-2014 " 10:59:59', 'dd-mon-yyyy hh24:mi:ss' ) THEN '10am'
            WHEN aq.datestarted BETWEEN to_date( '22-MAY-2014 11:00:00', 'dd-mon-yyyy hh24:mi:ss' ) AND to_date( '22-MAY-2014 " 11:59:59', 'dd-mon-yyyy hh24:mi:ss' ) THEN '11am'
        END

You just want this: 你只想要这个:

select count(*) count, hour(datestarted) time
from rchivedqueue
where datestarted between to_date( '22-MAY-2014 07:00:00', 'dd-mon-yyyy hh24:mi:ss' ) and to_date( '22-MAY-2014 " 11:59:59', 'dd-mon-yyyy hh24:mi:ss' )
group by hour(datestarted)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM