将秒转换为十进制

时间:2018-02-09 03:36:17

标签: sql-server oracle

我们将Atlassain Jira的数据库从SQL Server切换到Oracle SQL。所以现在一个较旧的书面脚本由于语法错误而无法正常工作,我试图纠正这个问题。我没有写原文,所以它有点乱。 原始剧本是:

 SELECT innerdata.author,CONVERT(VARCHAR(12),innerdata.workday, 106) as Work_Date,
         case
           when cast(right(CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)),3) as float) between .00 and .009 then CAST(sum(Timeworked)/3600 AS DECIMAL(10,2))
           when cast(right(CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)),3) as float) between .01 and .25  then CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)) + (.25-cast(right(CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)),3) as float)) --round to x.25
           when cast(right(CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)),3) as float) between .26 and .50  then CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)) + (.50-cast(right(CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)),3) as float)) --round to x.50
           when cast(right(CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)),3) as float) between .51 and .75  then CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)) + (.75-cast(right(CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)),3) as float)) --round to x.75
           when cast(right(CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)),3) as float) between .76 and 1.00 then CAST(sum(Timeworked)/3600 AS DECIMAL(10,0))                                                                             --round to next whole number
         End as total_time
 FROM
 (
 SELECT worklog.author,
          CASE
                WHEN ((SELECT DATEPART(DW, CAST(FLOOR(CAST(worklog.startdate AS FLOAT ))AS DATETIME))) IN(1) AND worklog.author = 'johnsmith') THEN CAST(FLOOR( CAST((worklog.startdate + 1) AS FLOAT ))AS DATETIME) 
                ELSE CAST(FLOOR( CAST(worklog.startdate AS FLOAT ))AS DATETIME) 
                END AS workday,
                             timeworked
       FROM ivrsjira_prod.dbo.worklog
       WHERE CAST(FLOOR( CAST(worklog.startdate AS FLOAT ))AS DATETIME) BETWEEN CONVERT(DATETIME,'2017-01-01',120) AND CONVERT(DATETIME,'2017-01-01',120)
       AND worklog.author IN ('johnsmith')
 ) innerdata
 GROUP BY innerdata.author,innerdata.workday
 ORDER BY innerdata.author, innerdata.workday DESC;

输出数据将出现

AUTHOR   | Work_Date  | total_time
johnsmith| 01-JAN-2017| 5.5
johnsmith| 03-JAN-2017| 2.75
johnsmith| 05-JAN-2017| 9.25

当您使用JIRA的前端时,最后一列total_time以秒格式记录到表中。编写脚本将数字(四舍五入)转换为四分之一小时的增量。用于报告目的。 这是表格的原始数据

ID |ISSUEID |AUTHOR    |GROUPLEVEL |ROLELEVEL |WORKLOGBODY |CREATED                 |UPDATEAUTHOR |UPDATED                 |STARTDATE               |TIMEWORKED
1  |3       |johnsmith |data       |data      |data        |01-JAN-2017 12:00:00 AM |johnsmith    |01-JAN-2017 12:00:00 AM |01-JAN-2017 12:00:00 AM |1200

所以这是我对脚本进行更新后的目前所有内容。

SELECT innerdata.author,TO_CHAR(innerdata.workday, 'YYYY-MM-DD') as Work_Date,
    case
      when cast(right(CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)),3) as float) between .00 and .009 then CAST(sum(Timeworked)/3600 AS DECIMAL(10,2))
      when cast(right(CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)),3) as float) between .01 and .25  then CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)) + (.25-cast(right(CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)),3) as float)) --round to x.25
      when cast(right(CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)),3) as float) between .26 and .50  then CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)) + (.50-cast(right(CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)),3) as float)) --round to x.5
      when cast(right(CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)),3) as float) between .51 and .75  then CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)) + (.75-cast(right(CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)),3) as float)) --round to x.75
      when cast(right(CAST(sum(Timeworked)/3600 AS DECIMAL(10,2)),3) as float) between .76 and 1.00 then CAST(sum(Timeworked)/3600 AS DECIMAL(10,0))                                                                             --round to next highest whole number
    End as total_time
FROM
(SELECT worklog.author,
         CASE
               WHEN ((SELECT DATEPART(DW, CAST(FLOOR( CAST(worklog.startdate AS FLOAT ))AS DATETIME))) IN(1) AND worklog.author = 'orourkad') THEN CAST(FLOOR( CAST((worklog.startdate + 1) AS FLOAT ))AS DATETIME) 
               ELSE CAST(FLOOR( CAST(worklog.startdate AS FLOAT ))AS DATETIME) 
               END AS workday,
                        timeworked
      FROM jira_ivrs.worklog
      WHERE CAST(FLOOR( CAST(worklog.startdate AS FLOAT ))AS DATETIME) BETWEEN TO_CHAR(DATETIME,'2017-01-01',120) AND TO_CHAR(DATETIME,'2017-01-01',120)
      AND worklog.author IN ('johnsmith')
) innerdata
GROUP BY innerdata.author,innerdata.workday
ORDER BY innerdata.author,innerdata.workday DESC;

但我现在得到错误

ORA-00936: missing expression
00936. 00000 -  "missing expression"
*Cause:    
*Action:
Error at Line: 12 Column: 102`

这直接指向IN(1)。 显然,由于语法上的差异,它给了我错误?

1 个答案:

答案 0 :(得分:0)

您似乎遇到CONVERT(VARCHAR(12),innerdata.workday, 106)问题 来自oracle docs:

  

CONVERT将字符串从一个字符集转换为另一个字符集。

     

char参数是要转换的值。它可以是任何一个   数据类型CHAR,VARCHAR2,NCHAR,NVARCHAR2,CLOB或NCLOB。

     

dest_char_set参数是要将字符集的名称   char被转换。

     

source_char_set参数是其中的字符集的名称   char存储在数据库中。默认值是数据库   字符集。

据我了解,您需要将字段从日期/时间转换为varchar。在这种情况下,您可以将convert替换为to_char(innerdata.workday,'DD MON YYYY')

3小时之后。

确定。看起来我明白你需要什么。像这样的东西用于内部选择(抱歉,我最近几年没有在我附近的oracle,所以我无法测试)

SELECT worklog.author,
        worklog.startdate+(CASE WHEN to_char(worklog.startdate,'DAY',
   'NLS_DATE_LANGUAGE=''numeric date language''')=1 THEN 1 ELSE 0 END) as workday,
timeworked
FROM jira_ivrs.worklog
WHERE worklog.startdate BETWEEN TO_DATE('&&Start_date','YYYY-MM-DD HH24:MI:SS') AND TO_DATE('&&End_date','YYYY-MM-DD HH24:MI:SS')
AND worklog.author IN ('username')

oracle日期算术中的JFYI并不复杂,您不需要将date转换为float。只需添加1到日期是1天的增加日期和1小时减去1/24的减少日期。 A pretty good article about date arithmetic

更新2。 我认为您的查询可能看起来像

    SELECT innerdata.author,trunc(workday),trunc(sum(timeworked)/3600)+trunc(mod(sum(timeworked),3600)/900)*0.25 total_time
FROM (
SELECT worklog.author,
        worklog.startdate+(CASE WHEN to_char(worklog.startdate,'DAY',
   'NLS_DATE_LANGUAGE=''numeric date language''')='7' THEN 1 ELSE 0 END) as workday,
timeworked
FROM worklog
WHERE -- worklog.startdate BETWEEN TO_DATE('&&Start_date','YYYY-MM-DD HH24:MI:SS') AND TO_DATE('&&End_date','YYYY-MM-DD HH24:MI:SS')
-- AND 
worklog.author IN ('username')) innerdata
group by innerdata.author,trunc(workday)
order by 1,2

小评论:

  1. 在星期日的oracle dow中这个表达式是7。
  2. trunc(workday)“将日期时间转换为日期