最早的退货记录

时间:2017-04-25 12:23:40

标签: sql netezza aginity

我在Aginity Workbench上使用Netezza数据库,我正在尝试使用最早的日期返回记录,其中包含三个中任意一个的IS代码(可维护性) ) 列。一个ICS_UID有多个记录,但我只想返回最早出现IS代码的记录。

以下是我一直试图使用的代码,但它似乎返回了记录中IS代码所在的所有实例,而不是where子句中ICS_UID的选择。感谢任何帮助或建议。

SELECT 
ICS _UID, min(MOVEMENT_DATE) as MOVEMENT_DATE, CURRENT_A_SERVICABILITY_CODE, CURRENT_B_SERVICABILITY_CODE, 
CURRENT_C_SERVICABILITY_CODE 
FROM 
HUB_MOVEMENT
WHERE 
ICS_UID IN (317517607,317962513,etc,etc…)
AND CURRENT_A_SERVICABILITY_CODE = 'IS' OR CURRENT_B_SERVICABILITY_CODE = 'IS' OR CURRENT_C_SERVICABILITY_CODE = 'IS'
GROUP BY 
ICS_UID, CURRENT_A_SERVICABILITY_CODE,
CURRENT_B_SERVICABILITY_CODE,
CURRENT_C_SERVICABILITY_CODE;

1 个答案:

答案 0 :(得分:1)

请勿使用GROUP BY。如果你想要一条记录,那么:

SELECT m.*
FROM HUB_MOVEMENT m
WHERE ICS_UID IN (317517607,317962513,etc,etc…) AND
      'IS' IN (CURRENT_A_SERVICABILITY_CODE, CURRENT_B_SERVICABILITY_CODE , CURRENT_C_SERVICABILITY_CODE)
ORDER BY MOVEMENT_DATE
LIMIT 1;

如果您想要每ICS_UID行一行,则可以使用ROW_NUMBER()

SELECT m.*
FROM (SELECT m.*,
             ROW_NUMBER() OVER (PARTITION BY ICS_UID ORDER BY MOVEMENT_DATE) as seqnum
      FROM HUB_MOVEMENT m
      WHERE ICS_UID IN (317517607,317962513,etc,etc…) AND
            'IS' IN (CURRENT_A_SERVICABILITY_CODE, CURRENT_B_SERVICABILITY_CODE , CURRENT_C_SERVICABILITY_CODE)
     ) m
WHERE seqnum = 1;