在PostgreSQL中使用date-diff函数

时间:2014-11-05 16:04:36

标签: postgresql datediff netezza

我在sql server中使用了下面的查询,它工作正常,但现在我们开始使用Netezza并且查询停止工作。我怀疑日期功能在新西兰不起作用。我是Netezza的新手 - 请帮忙。这是完整的查询

SET NOCOUNT ON
GO

    DECLARE @DataSource TABLE
    (
         [PATIENT_ID] TINYINT
        ,[ADMIT_DATE] DATE
        ,[LOCATION] VARCHAR(3)
    )

    INSERT INTO @DataSource ([PATIENT_ID], [ADMIT_DATE], [LOCATION])
    VALUES (33, '1-10-2014', 'ER')
          ,(33, '1-11-2014', 'ER')
          ,(33, '1-15-2014', 'ER')
          ,(33, '1-17-2014', 'ER')
          ,(45, '2-15-2014', 'OBS')
          ,(45, '2-16-2014', 'OBS')
          ,(45, '2-20-2014', 'OBS')
          ,(45, '2-25-2014', 'OBS')
          ,(45, '2-27-2014', 'OBS')

;WITH TempDataSource ([PATIENT_ID], [ADMIT_DATE], [LOCATION], [Rank])  AS
(
    SELECT [PATIENT_ID]
          ,[ADMIT_DATE]
          ,[LOCATION]
          ,ROW_NUMBER() OVER (PARTITION BY [PATIENT_ID], [LOCATION] ORDER BY [ADMIT_DATE] ASC)
    FROM @DataSource    
)

,DataSource ([PATIENT_ID], [ADMIT_DATE], [LOCATION], [DIFF_IN_HOURS]) AS
(
SELECT DS1.[PATIENT_ID]
      ,DS1.[ADMIT_DATE]
      ,DS1.[LOCATION]
      ,DATEDIFF(HOUR, DS2.[ADMIT_DATE], DS1.[ADMIT_DATE])
FROM TempDataSource DS1
LEFT JOIN TempDataSource DS2
    ON DS1.[Rank] - 1 = DS2.[Rank]
    AND DS1.[PATIENT_ID] = DS2.[PATIENT_ID]
    AND DS1.[LOCATION] = DS2.[LOCATION]
)
SELECT [PATIENT_ID]
      ,[ADMIT_DATE]
      ,[LOCATION]
      ,ROW_NUMBER() OVER (PARTITION BY [PATIENT_ID], [LOCATION] ORDER BY [ADMIT_DATE] ASC)
FROM DataSource
WHERE [DIFF_IN_HOURS] >= 48

2 个答案:

答案 0 :(得分:1)

这就是我将SQL Server代码转换为在Netezza中工作的方式

    create temp table tmp_DataSource
    (
         PATIENT_ID smallint
        ,ADMIT_DATE timestamp
        ,LOCATION VARCHAR(3)
    );

    INSERT INTO tmp_DataSource(PATIENT_ID, ADMIT_DATE, LOCATION) VALUES (33, '1-10-2014 00:00:00', 'ER');
    INSERT INTO tmp_DataSource(PATIENT_ID, ADMIT_DATE, LOCATION) VALUES (33, '1-11-2014 00:01:00', 'ER');
    INSERT INTO tmp_DataSource(PATIENT_ID, ADMIT_DATE, LOCATION) VALUES (33, '1-15-2014', 'ER');
    INSERT INTO tmp_DataSource(PATIENT_ID, ADMIT_DATE, LOCATION) VALUES (33, '1-17-2014', 'ER');
    INSERT INTO tmp_DataSource(PATIENT_ID, ADMIT_DATE, LOCATION) VALUES (45, '2-15-2014', 'OBS');
    INSERT INTO tmp_DataSource(PATIENT_ID, ADMIT_DATE, LOCATION) VALUES (45, '2-16-2014', 'OBS');
    INSERT INTO tmp_DataSource(PATIENT_ID, ADMIT_DATE, LOCATION) VALUES (45, '2-20-2014', 'OBS');
    INSERT INTO tmp_DataSource(PATIENT_ID, ADMIT_DATE, LOCATION) VALUES (45, '2-25-2014', 'OBS');
    INSERT INTO tmp_DataSource(PATIENT_ID, ADMIT_DATE, LOCATION) VALUES (45, '2-27-2014', 'OBS');

create temp table TempDataSource as
    SELECT PATIENT_ID
          ,ADMIT_DATE
          ,LOCATION
          ,ROW_NUMBER() OVER (PARTITION BY PATIENT_ID, LOCATION ORDER BY ADMIT_DATE ASC) as rank
    FROM tmp_DataSource;

create temp table DataSource as
SELECT DS1.PATIENT_ID
      ,DS1.ADMIT_DATE
      ,DS1.LOCATION
      ,extract(epoch  from (DS1.ADMIT_DATE - DS2.ADMIT_DATE))/3600.0 as DIFF_IN_HOURS

FROM TempDataSource DS1
LEFT JOIN TempDataSource DS2
    ON DS1.Rank - 1 = DS2.Rank
    AND DS1.PATIENT_ID = DS2.PATIENT_ID
    AND DS1.LOCATION = DS2.LOCATION;

SELECT PATIENT_ID
      ,ADMIT_DATE
      ,LOCATION
      ,ROW_NUMBER() OVER (PARTITION BY PATIENT_ID, LOCATION ORDER BY ADMIT_DATE ASC)
FROM DataSource
WHERE DIFF_IN_HOURS >= 48;

答案 1 :(得分:0)

在PostgreSQL中没有datediff函数,但您可以在一个区间内使用date_part函数的各种表达式,例如:

DATE_PART('day', end - start) * 24 + DATE_PART('hour', end - start )

或以更合同的方式

DATE_PART('epoch', end - start) / 3600