返回具有最早日期的记录



我正在Netezza数据库中使用Aginity Workbench,并且我正在尝试根据它返回具有最早日期的记录,该记录在三个(可维护性)列中的任何一个中具有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;

不要使用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;

相关内容

  • 没有找到相关文章

最新更新