简体   繁体   English

在db2数据库上的SQL中查询

[英]Query in sql on db2 database

I'm using this query: 我正在使用此查询:

select substr("Message_Time",6,2) || '/' || substr("Message_Time",4,2) || '/' || '20'|| substr("Message_Time",2,2) || substr("Message_Time",8,2) || ':' || substr("Message_Time",10,2) as "Date",
       count(*) as "Fault", "Message_Location", "Service_Name_U", "Operation_Name_U", "Port_Namespace_U", 
      "Error_Code_U", "Error_SubCode_U", "Fault_Code_U", "Fault_String_U",
       "Requester_Identity", "Application_ServerName_U"
from "Fault_Log_Table_610"
where "Message_Time" >= 1181016220000000 and "Message_Time" < 1181017220000000 and
      "Operation_Name_U" = 'getDomandeDisabile'
group by substr("Message_Time", 6, 2) || '/' || substr("Message_Time", 4, 2)  || '/' || '20'|| substr("Message_Time", 2, 2) || substr("Message_Time",8,2) || ':' || substr("Message_Time",10,2), 
       "Service_Name_U", "Operation_Name_U", "Error_Code_U", "Error_SubCode_U", "Message_Location", "Fault_Code_U", "Fault_String_U",
       "Port_Namespace_U", "Requester_Identity", "Application_ServerName_U"

I need to add 2 hours to Date field, in other words I need to add the number 2 to substr("Message_Time",8,2). 我需要在“日期”字段中添加2小时,换句话说,我需要在substr(“ Message_Time”,8,2)中添加数字2。

The date has the format 1181020164532000 where: 日期格式为1181020164532000,其中:

first number is century then following two numbers are the years then following two numbers are the months then following two numbers are the days then following two numbers are the hours then following two numbers are the minutes and last three numbers are milliseconds 第一个数字是世纪,然后是两个数字是年,然后是两个数字是月,然后是两个数字是天,然后是两个数字是小时,然后是两个数字是分钟,最后三个是毫秒

I would pre-compute the timestamp parsing and then add the 2 hours in a CTE (Common Table Expression). 我将预先计算时间戳解析,然后将2小时添加到CTE(公用表表达式)中。

Then I would use this ready-to-use data in the query you want. 然后,我将在您想要的查询中使用这些现成的数据。 Something like: 就像是:

with x as (
select
    timestampadd(8, 2, -- add 2 hours
      timestamp_format( -- parse the VARCHAR into a TIMESTAMP
                       '20' || substr("Message_Time",2,15) || '000',
                       'YYYYMMDDHHMISSNNNNNN')
    ) as "Date",
    "Message_Location", "Service_Name_U", 
    "Operation_Name_U", "Port_Namespace_U", 
    "Error_Code_U", "Error_SubCode_U", "Fault_Code_U", "Fault_String_U",
    "Requester_Identity", "Application_ServerName_U"
from "Fault_Log_Table_610"
where "Message_Time" >= 1181016220000000
  and "Message_Time" < 1181017220000000
  and "Operation_Name_U" = 'getDomandeDisabile'
)
select
    "Date",
    count(*) as "Fault",
    "Message_Location", "Service_Name_U", 
    "Operation_Name_U", "Port_Namespace_U", 
    "Error_Code_U", "Error_SubCode_U", "Fault_Code_U", "Fault_String_U",
    "Requester_Identity", "Application_ServerName_U"
from x
group by "Date", 
    "Service_Name_U", "Operation_Name_U", "Error_Code_U", "Error_SubCode_U",
    "Message_Location", "Fault_Code_U", "Fault_String_U",
    "Port_Namespace_U", "Requester_Identity", "Application_ServerName_U"

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

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