MAX不在SQL查询中工作

时间:2015-02-28 17:16:21

标签: sql oracle

我希望通过以下查询检索最新记录.... 但是max在以下查询中不起作用。将检索所有行而不是最新的行

 SELECT SV.SEGMENT1 TARGETED_INCENTIVE,
                         SIT.ANALYSIS_CRITERIA_ID,
                         SIT.OBJECT_VERSION_NUMBER OBJECT_VERSION_NUMBER,
                         ST.ID_FLEX_NUM,
                         SIT.DATE_FROM,
                         SIT.DATE_TO,
                         MAX (SIT.PERSON_ANALYSIS_ID)

                    FROM FND_ID_FLEX_STRUCTURES_TL STTL,
                         FND_ID_FLEX_STRUCTURES ST,
                         PER_PERSON_ANALYSES SIT,
                         PER_ANALYSIS_CRITERIA SV
                   WHERE     1 = 1
                         AND (STTL.ID_FLEX_STRUCTURE_NAME) LIKE
                                ('%%Tare%')
                         AND STTL.LANGUAGE = USERENV ('LANG')
                         AND ST.ID_FLEX_CODE = STTL.ID_FLEX_CODE
                         AND ST.ID_FLEX_NUM = STTL.ID_FLEX_NUM
                         AND ST.ID_FLEX_NUM = SIT.ID_FLEX_NUM
                         AND ST.ID_FLEX_NUM = SV.ID_FLEX_NUM
                         AND TO_DATE (SIT.DATE_TO) IS NULL
                         AND SIT.ANALYSIS_CRITERIA_ID = SV.ANALYSIS_CRITERIA_ID
                         AND SIT.PERSON_ID = (SELECT PERSON_ID
                                                FROM abc
                                               WHERE ID = :AIN)
                GROUP BY SV.SEGMENT1,
                         SIT.ANALYSIS_CRITERIA_ID,
                         STTL.ID_FLEX_STRUCTURE_NAME,
                         SIT.OBJECT_VERSION_NUMBER,
                         ST.ID_FLEX_NUM,
                         SIT.DATE_FROM,
                         SIT.DATE_TO;

有人可以指导吗?

1 个答案:

答案 0 :(得分:1)

我担心这不是MAX()所做的。 MAX()是一个聚合函数(尽管它可以用作窗口[解析]函数),因此当您获得按其他列分组的特定列的MAX()时,您将获得不同的值组合对于所有其他专栏。

我想你可能会想要这样的东西:

SELECT targeted_incentive, analysis_criteria_id
     , object_version_number, id_flex_num, date_from
     , date_to, person_analysis_id
  FROM (
    SELECT sv.segment1 AS targeted_incentive
         , sit.analysis_criteria_id
         , sit.object_version_number
         , st.id_flex_num
         , sit.date_from
         , sit.date_to
         , sit.person_analysis_id
         , RANK() OVER ( ORDER BY sit.person_analysis_id DESC ) rn
      FROM fnd_id_flex_structures_tl sttl
         , fnd_id_flex_structures st
         , per_person_analyses sit
         , per_analysis_criteria sv
     WHERE sttl.id_flex_structure_name LIKE '%Tare%'
       AND sttl.language = USERENV('LANG')
       AND st.id_flex_code = sttl.id_flex_code
       AND st.id_flex_num = sttl.id_flex_num
       AND st.id_flex_num = sit.id_flex_num
       AND st.id_flex_num = sv.id_flex_num
       AND sit.date_to IS NULL
       AND sit.analysis_criteria_id = sv.analysis_criteria_id
       AND sit.person_id = ( SELECT person_id FROM abc
                              WHERE id = :AIN )
) WHERE rn = 1;

RANK()窗口函数将按降序返回person_analysis_id值排序的每一行的等级。要获得最大值,只需过滤rank = 1.请注意,如果是tie,这将返回多行。如果您只想要一行,请使用ROW_NUMBER()代替RANK()

另请注意,我稍微清理了一下查询。例如,您当然不需要在%中连续使用两个LIKE通配符。你也绝对不需要WHERE 1=1条件。