0
select whse,
    user_id,
    shift,
    tran_nbr,
    to_char(sched_start_date, 'HH24:MI:SS') as STRTUP,
    to_char(sched_start_date, 'MM/DD/YYYY') as DATE1,
    act_name,
    dept_code,
    num
from (
    select whse,
        user_id,
        shift,
        tran_nbr,
        sched_start_date,
        to_char(sched_start_date, 'MM/DD/YYYY'),
        actl_time,
        act_name,
        dept_Code,
        dense_RANK() over (
            partition by to_char(sched_start_date, 'MM/DD/YYYY'),
            user_id order by user_id,
                sched_start_date asc
            ) num
    from my_hdr
    ) X
where to_char(sched_start_date, 'MM/DD/YYYY HH24:MM:SS') between '02/09/2017 04:00:00' and '02/09/2017 10:00:00'
    and actl_time > '0'
    and ACT_NAME = 'JFSTRTUP'
order by whse,
    user_id,
    to_char(sched_start_date, 'HH24:MI:SS')

JFSTRTUP每当发现给它“1”的等级(数量)时,我都会尝试返回。现在我得到一些JFSTRTUP地方num <> 1

SQL 输出:

WHSE USER_ID Shift  TRAN NBR    STRTUP      DATE1       ACT_NAME    DEPT_CODE  NUM
008   AMANY   1B    900197641   05:28:00    02/09/2017  JFSTRTUP    ORDERFILL   1
008   BTORR   1C    900197629   05:58:00    02/09/2017  JFSTRTUP    ORDERFILL   3
008   CROM    1B    900197644   05:29:00    02/09/2017  JFSTRTUP    ORDERFILL   1

我想让第二行返回 1 而不是 3。

4

1 回答 1

0

在您的 ORDER BY 子句之前添加此行,在 JFSTRTUP 的过滤器之后:

AND num = 1

注意:您不需要在 dense_rank order by 子句中使用 user_id - 您按 user_id 进行分区,因此排名永远不会考虑 user_id。从来没有理由在窗口函数的 partition by 和 order by 子句中使用值。

于 2017-02-28T22:40:51.463 回答