Return 记录最早日期
Return Record With Earliest Date
我正在使用 Aginity Workbench
和 Netezza
上的数据库,我正在尝试 return 最早日期的记录,其中包含 IS
代码三列(适用性)中的任意一列。一个 ICS_UID
有多个记录,但我只想 return 最早出现的具有 IS
代码的记录。
下面是我一直在尝试使用的代码,但它似乎是 returning 记录具有 IS
代码的所有实例,而不是 ICS_UID
的选择在 where 子句中。感谢任何帮助或建议。
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;
我正在使用 Aginity Workbench
和 Netezza
上的数据库,我正在尝试 return 最早日期的记录,其中包含 IS
代码三列(适用性)中的任意一列。一个 ICS_UID
有多个记录,但我只想 return 最早出现的具有 IS
代码的记录。
下面是我一直在尝试使用的代码,但它似乎是 returning 记录具有 IS
代码的所有实例,而不是 ICS_UID
的选择在 where 子句中。感谢任何帮助或建议。
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;