TeraData查询从表中获取最小值和最大值

时间:2016-09-12 16:27:01

标签: database teradata

我有一个名为HMS_PGHL8_HQID_MAP_DEMO的表和名为UDMTOOL_STG的数据库。

以下查询会提取我所需的值。

SELECT udm_main.RECORD_ID AS  record_id ,udm_main.HMS_PGH_NODE_ID AS  hms_pgh_node_id, 
       udm_main.HMS_PGH_NODE_NAME AS hms_pgh_node_name ,udm_main.HQ_NODE_ID AS hq_node_id, udm_main.HQ_NODE_NAME AS hq_node_name,
       udm_main.NODE_TYPE_IND as node_type_ind,UPDATED_ON,UPDATED_BY,
       INSERT_TS  FROM UDMTOOL_STG.HMS_PGHL8_HQID_MAP_DEMO udm_main
WHERE op_code <> 'D' OR op_code IS NULL
ORDER BY RECORD_ID,HMS_PGH_NODE_ID,HQ_NODE_ID
GROUP BY 1,2,3,4,5,6,7,8,9;

现在我需要得到总行值:

  1. 记录ID为分钟
  2. 记录ID为最大
  3. 请注意,下面不起作用,

    SELECT MIN(udm_main.RECORD_ID AS)  record_id ,udm_main.HMS_PGH_NODE_ID AS  hms_pgh_node_id, 
               udm_main.HMS_PGH_NODE_NAME AS hms_pgh_node_name ,udm_main.HQ_NODE_ID AS hq_node_id, udm_main.HQ_NODE_NAME AS hq_node_name,
               udm_main.NODE_TYPE_IND as node_type_ind,UPDATED_ON,UPDATED_BY,
               INSERT_TS  FROM UDMTOOL_STG.HMS_PGHL8_HQID_MAP_DEMO udm_main
        WHERE op_code <> 'D' OR op_code IS NULL
        ORDER BY RECORD_ID,HMS_PGH_NODE_ID,HQ_NODE_ID
        GROUP BY 1,2,3,4,5,6,7,8,9;
    

    请求您的帮助以解决问题。

1 个答案:

答案 0 :(得分:3)

您可以使用QUALIFY过滤MIN / MAX:

SELECT udm_main.RECORD_ID,udm_main.HMS_PGH_NODE_ID AS  hms_pgh_node_id, 
       udm_main.HMS_PGH_NODE_NAME AS hms_pgh_node_name,udm_main.HQ_NODE_ID AS hq_node_id, udm_main.HQ_NODE_NAME AS hq_node_name,
       udm_main.NODE_TYPE_IND as node_type_ind,UPDATED_ON,UPDATED_BY,
       INSERT_TS
FROM UDMTOOL_STG.HMS_PGHL8_HQID_MAP_DEMO udm_main
WHERE op_code <> 'D' OR op_code IS NULL
GROUP BY 1,2,3,4,5,6,7,8,9
QUALIFY udm_main.RECORD_ID = MIN(udm_main.RECORD_ID) OVER ()
     OR udm_main.RECORD_ID = MAX(udm_main.RECORD_ID) OVER ()
ORDER BY RECORD_ID