行号分区不起作用

时间:2017-06-20 07:34:05

标签: sql sql-server-2008-r2

我有一个问题。不知道为什么。

select ROW_NUMBER() OVER (PARTITION BY a.rmid, c.nama ORDER BY a.tanggalrekam) rn,
       a.rmid, a.psid, c.nama, a.tanggalrekam
from _tRekamMedis a
inner join _tRekamMedisTindakan b on a.rmid= b.rmid
inner join _tPasien c on a.psid = c.psid
inner join _tSecurityUser d on b.dokterid = d.securityuserid
inner join _tTindakan e on b.tindakanid = e.tindakanid

结果:

    rn  rmid    psid    nama        tanggalrekam    dokterid
    1   1       1       Fauzan      2017-06-20      -2147483648
    2   1       1       Fauzan      2017-06-20      -2147483648
    3   1       1       Fauzan      2017-06-20      -2147483648
    1   2       2       Huri        2017-06-20      -2147483648

应该是:

    rn  rmid    psid    nama        tanggalrekam    dokterid
    1   1       1       Fauzan      2017-06-20      -2147483648
    1   1       1       Fauzan      2017-06-20      -2147483648
    1   1       1       Fauzan      2017-06-20      -2147483648
    2   2       2       Huri        2017-06-20      -2147483648

rownumber不起作用。它显示在rn字段上。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:3)

你显然不希望每个a.rmid,c.nama的数字,所以完全删除PARTITION BY条款。您只是在寻找没有任何分区的DENSE_RANK

SELECT DENSE_RANK() OVER (ORDER BY a.rmid, c.nama a.tanggalrekam) AS rn,
...

答案 1 :(得分:0)

包括分区的所有列

select ROW_NUMBER() OVER (PARTITION BY a.rmid, a.psid,c.nama,a.tanggalrekam ORDER BY a.tanggalrekam) rn,
       a.rmid, a.psid, c.nama, a.tanggalrekam
from _tRekamMedis a
inner join _tRekamMedisTindakan b on a.rmid= b.rmid
inner join _tPasien c on a.psid = c.psid
inner join _tSecurityUser d on b.dokterid = d.securityuserid
inner join _tTindakan e on b.tindakanid = e.tindakanid