从同一列中减去Max和Min

时间:2017-10-02 17:25:17

标签: sql oracle date subtraction

class AttackComponent : GKComponent {

  //define behaviour


   override init() {

   }
}



class Player: GKEntity {
     overide init() {

        // add components here
       let attackComponent = AttackComponent()
       self.addComponent(attackComponent)

    }

如何从min(b.created)中减去max(b.created)?我坚持这一部分:

select 
      d.FK_SOCRD_ID,
      d.SUBJECT_NUMBER,
      c.PTLASTNAME || ', ' || c.PTFIRSTNAME as "Patient name",
      min(b.created) as Data Entry 1,
      max(b.created) as Data Entry 2,
      min(b.created) - max(b.created) 
        || ' days and '
        || TO_CHAR(to_date('01/01/2000', 'MM-DD-YYYY') 
                + (b.created - b.created), 'HH24:MI:SS' ) AS Diff
   FROM 
      CR_MDT a
         full outer join CR_MDT_VERIFY b
            on a.CR_MDT_ID = b.FK_CR_MDT_ID
         left join patient c
            on a.FK_SOCRD_ID = c.SOCRD_ID
            left join PT_STUDY d
               on c.SOCRD_ID = d.FK_SOCRD_ID
   where 
          a.CR_MDT_DT between TO_DATE('01/01/2017', 'mm/dd/yyyy') 
      and TO_DATE('12/31/2017', 'mm/dd/yyyy')
   group by 
      d.FK_SOCRD_ID,
      d.SUBJECT_NUMBER,
      c.PTLASTNAME,
      c.PTFIRSTNAME,
      b.created

我需要有关如何编写减法部分的帮助,因为b.created max和min来自同一列。

3 个答案:

答案 0 :(得分:0)

您可以将该查询放在子查询中并在外部进行减法:

SELECT *
    , max_created - min_created as date_difference
FROM
(
SELECT d.FK_SOCRD_ID
    , d.SUBJECT_NUMBER
    , c.PTLASTNAME || ', ' || c.PTFIRSTNAME AS "Patient name"
    , min(b.created) AS min_created
    , max(b.created) AS max_created
FROM CR_MDT a
FULL JOIN CR_MDT_VERIFY b
    ON a.CR_MDT_ID = b.FK_CR_MDT_ID
LEFT JOIN patient c
    ON c.SOCRD_ID = a.FK_SOCRD_ID
LEFT JOIN PT_STUDY d
    ON d.FK_SOCRD_ID = c.SOCRD_ID
WHERE a.CR_MDT_DT BETWEEN TO_DATE('01/01/2017', 'mm/dd/yyyy') AND TO_DATE('12/31/2017', 'mm/dd/yyyy')
GROUP BY d.FK_SOCRD_ID
    , d.SUBJECT_NUMBER
    , c.PTLASTNAME
    , c.PTFIRSTNAME
) subq

您可能需要根据您使用的dbms更改datediff函数。

编辑:我现在更新了日期减法部分,我知道你使用的是Oracle。

答案 1 :(得分:0)

您可以使用floor(),只留下差异的小数部分并将其转换为hh24:mi:ss,如下所示:

select id, mx, mn, floor(diff) ||' days and '|| 
       to_char(trunc(sysdate) + diff - floor(diff), 'hh24:mi:ss') diff
  from (select id, max(created) mx, min(created) mn, 
               max(created) - min(created) diff
          from test group by id)

测试数据:

create table test (id number(3), created date);
insert into test values (1, timestamp '2017-09-12 01:00:00');
insert into test values (1, timestamp '2017-11-05 11:24:17');
insert into test values (1, timestamp '2017-12-15 13:42:05');
insert into test values (2, timestamp '2017-01-05 11:00:00');
insert into test values (2, timestamp '2017-06-05 23:12:56');

结果:

  ID  MX                   MN                   DIFF
----  -------------------  -------------------  ---------------------
   1  2017-12-15 13:42:05  2017-09-12 01:00:00  94 days and 12:42:05
   2  2017-06-05 23:12:56  2017-01-05 11:00:00  151 days and 12:12:56

答案 2 :(得分:0)

这是给我预期结果的原因:

选择
d.FK_SOCRD_ID,
    d.SUBJECT_NUMBER,
        c.PTLASTNAME || ','|| c.PTFIRSTNAME AS“患者姓名”,
        min(b.created)AS min_created,
        max(b.created)AS max_created,
        trunc(max(b.created)) - trunc(min(b.created))|| '天'AS差异 来自CR_MDT a 完全加入CR_MDT_VERIFY b
    ON a.CR_MDT_ID = b.FK_CR_MDT_ID
LEFT JOIN患者c
    ON c.SOCRD_ID = a.FK_SOCRD_ID
LEFT JOIN PT_STUDY d
    ON d.FK_SOCRD_ID = c.SOCRD_ID
a.CR_MDT_DT在TO_DATE('01 / 01/2017','mm / dd / yyyy')和TO_DATE
('12 / 31/2017','mm / dd / yyyy')之间 GROUP BY d.FK_SOCRD_ID,
d.SUBJECT_NUMBER,
c.PTLASTNAME,
c.PTFIRSTNAME