oracle:选择查询以通过将其格式从varchar更改为date来过滤日期列

v09wglhw  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(269)

我有一个表oracle db,它提供了不同客户在我们的服务中每天面临的各种故障情况的信息。
我想做的是计算每天所有客户报告的不同故障情况的总数,例如,手机脱机、宽带脱机、摄像头无响应等。
问题是,故障条件起始日期列的类型是varchar,因此筛选和分组不会给出所需的结果。
请提出一些解决方案,我不允许添加新列。我只被允许运行select命令。
这就是我尝试过的:

select trouble_condition_start_date, count(1) from trouble_tables
where trouble_condition_start_date >= '11/1/2019'
and trouble_condition = 'Camera - Not Responding'
group by trouble_condition_start_date
order by trouble_condition_start_date
hc8w905p

hc8w905p1#

正如您已经注意到的,将日期作为文本存储在数据库中是个坏主意。不管怎样,你可以随时用 TO_DATE . 例如。:

select
  to_date(trouble_condition_start_date, 'mm/dd/yyyy') as start_date, 
  count(*)
from trouble_tables
where to_date(trouble_condition_start_date, 'mm/dd/yyyy') >= date '2019-11-01'
  and trouble_condition = 'Camera - Not Responding'
group by to_date(trouble_condition_start_date, 'mm/dd/yyyy')
order by to_date(trouble_condition_start_date, 'mm/dd/yyyy');

同样的事情 WITH 提高可读性的条款:

with better_table as 
(
  select t.*, to_date(trouble_condition_start_date, 'mm/dd/yyyy') as start_date
  from trouble_tables t
)
select start_date, count(*)
from better_table
where start_date >= date '2019-11-01'
  and trouble_condition = 'Camera - Not Responding'
group by start_date
order by start_date;

相关问题