3

我有一个关于我正在尝试编写的 SQL 查询的问题。

我需要从数据库中查询数据。除其他外,该数据库具有以下 3 个字段:

Account_ID #, Date_Created, Time_Created

我需要编写一个查询,告诉我每小时开了多少个账户。

我已经编写了上述查询,但有时创建了 0 个帐户,因此这些“小时”不会填充到结果中。

例如:

日期__小时
435 12-Aug-12 03
213 12-Aug-12 04
125 12-Aug-12 06

如上例所示,第 5 小时没有开立任何账户。

有没有办法让结果可以填充小时但显示该小时打开的 0 个帐户?我希望我的结果看起来如何的示例:

日期_Hour
435 12-Aug-12 03
213 12-Aug-12 04
0 12-Aug-12 05
125 12-Aug-12 06

谢谢!

更新:这是我到目前为止

SELECT count(*) as num_apps, to_date(created_ts,'DD-Mon-RR') as app_date, to_char(created_ts,'HH24') as app_hour 
FROM accounts 
WHERE To_Date(created_ts,'DD-Mon-RR') >= To_Date('16-Aug-12','DD-Mon-RR') 
GROUP BY To_Date(created_ts,'DD-Mon-RR'), To_Char(created_ts,'HH24') 
ORDER BY app_date, app_hour
4

5 回答 5

2

要获得您想要的结果,您需要创建一个表(或使用查询来生成一个“临时”表),然后使用左连接到您的计算查询来获取每小时的行数 - 即使是那些体积为 0 的行。

例如,假设我有一个包含 app_date 和 app_hour 字段的表。还假设此表在您希望报告的每一天/小时都有一行。

查询将是:

SELECT NVL(c.num_apps,0) as num_apps, t.app_date, t.app_hour
    FROM time_table t
    LEFT OUTER JOIN 
    (
    SELECT count(*) as num_apps, to_date(created_ts,'DD-Mon-RR') as app_date, to_char(created_ts,'HH24') as app_hour 
    FROM accounts 
    WHERE To_Date(created_ts,'DD-Mon-RR') >= To_Date('16-Aug-12','DD-Mon-RR') 
    GROUP BY To_Date(created_ts,'DD-Mon-RR'), To_Char(created_ts,'HH24') 
    ORDER BY app_date, app_hour
    ) c ON (t.app_date = c.app_date AND t.app_hour = c.app_hour)
于 2012-10-02T19:26:23.867 回答
1

我相信最好的解决方案不是创建一些花哨的临时表,而只是使用这个结构:

select level
FROM Dual
CONNECT BY level <= 10
ORDER BY level;

这会给你(十行):1 2 3 4 5 6 7 8 9 10

对于小时间隔,只需稍作修改:

select 0 as num_apps, (To_Date('16-09-12','DD-MM-RR') + level / 24) as created_ts
FROM dual
CONNECT BY level <= (sysdate - To_Date('16-09-12','DD-MM-RR')) * 24 ;  

只是为了它为你添加解决方案的乐趣(我没有尝试语法,所以我很抱歉有任何错误,但这个想法很清楚):

 SELECT SUM(num_apps) as num_apps, to_date(created_ts,'DD-Mon-RR') as app_date, to_char(created_ts,'HH24') as app_hour 
FROM(
  SELECT count(*) as num_apps, created_ts
  FROM accounts 
  WHERE To_Date(created_ts,'DD-Mon-RR') >= To_Date('16-09-12','DD-MM-RR') 
UNION ALL
  select 0 as num_apps, (To_Date('16-09-12','DD-MM-RR') + level / 24) as created_ts
  FROM dual
  CONNECT BY level <= (sysdate - To_Date('16-09-12','DD-MM-RR')) * 24 ;  
)
GROUP BY To_Date(created_ts,'DD-Mon-RR'), To_Char(created_ts,'HH24') 
ORDER BY app_date, app_hour
;
于 2012-10-02T21:39:12.810 回答
0

您还可以在 SELECT 中使用 CASE 语句来强制使用您想要的值。

于 2012-10-02T19:29:09.483 回答
0

有一个“序列表”可能很有用,出于各种原因,看起来像这样:

create table dbo.sequence
(
  id int not null primary key clustered ,  
)

加载数百万行左右,涵盖正值和负值。

然后,给定一个看起来像这样的表

create table dbo.SomeTable
(
  account_id   int  not null primary key clustered ,
  date_created date not null ,
  time_created time not null ,
)

然后,您的查询就像(在 SQL Server 中)一样简单:

select year_created  = years.id  ,
       month_created = months.id ,
       day_created   = days.id   ,
       hour_created  = hours.id  ,
       volume        = t.volume
from       ( select * ,
                    is_leap_year = case
                                   when id % 400 = 0 then 1
                                   when id % 100 = 0 then 0
                                   when id %   4 = 0 then 1
                                   else                   0
                                   end
             from dbo.sequence
             where id between 1980 and year(current_timestamp)
           ) years
cross join ( select *
             from dbo.sequence
             where id between 1 and 12
           ) months
left  join ( select *
             from dbo.sequence
             where id between 1 and 31
           ) days on days.id <= case months.id
                                when  2 then 28 + years.is_leap_year
                                when  4 then 30
                                when  6 then 30
                                when  9 then 30
                                when 11 then 30
                                else         31
                                end
cross join ( select *
             from dbo.sequence
             where id between 0 and 23
           ) hours
left join ( select date_created ,
                   hour_created = datepart(hour,time_created ) ,
                   volume = count(*)
            from dbo.SomeTable
            group by date_created ,
                     datepart(hour,time_created)
          ) t on datepart( year  , t.date_created ) = years.id
             and datepart( month , t.date_created ) = months.id
             and datepart( day   , t.date_created ) = days.id
             and t.hour_created                     = hours.id
order by 1,2,3,4
于 2012-10-02T19:55:26.843 回答
0

我不清楚created_ts是日期时间还是 varchar。如果是日期时间,则不应使用to_date; 如果它是 varchar,则不应使用to_char.

假设这是一个日期时间,并借用@jakub.petr 的FROM Dual CONNECT BY level技巧,我建议:

SELECT count(*) as num_apps, to_char(created_ts,'DD-Mon-RR') as app_date, to_char(created_ts,'HH24') as app_hour 
FROM (select level-1 as hour FROM Dual CONNECT BY level <= 24) h 
  LEFT JOIN accounts a on h.hour = to_number(to_char(a.created_ts,'HH24'))
WHERE created_ts >= To_Date('16-Aug-12','DD-Mon-RR') 
GROUP BY trunc(created_ts), h.hour 
ORDER BY app_date, app_hour
于 2012-10-03T08:39:40.913 回答