根据药品类别创建药品序列号列

gojuced7  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(211)

我必须为药物列表创建一个sno列。请仔细检查以下样品以供参考。典型=d-糖尿病药物o-其他药物

ID  Date       Typ Drug
123 02/05/2020 D   aaa
123 02/05/2020 D   bbb
123 02/05/2020 D   nnn
123 02/05/2020 O   ccc
123 02/05/2020 O   fff
123 05/06/2020 D   asd
456 12/06/2020 D   wef
456 12/06/2020 D   fgf
456 12/06/2020 O   rty

预期产量

ID  Date       Typ Drug Sno
123 02/05/2020 D   aaa  1
123 02/05/2020 D   bbb  2
123 02/05/2020 D   nnn  3
123 02/05/2020 O   ccc  1
123 02/05/2020 O   fff  2
123 05/06/2020 D   asd  1
456 12/06/2020 D   wef  1
456 12/06/2020 D   fgf  2
456 12/06/2020 O   rty  1
4ioopgfo

4ioopgfo1#

哦,我想我明白规则了。
对于每种药品,在每一天,序列号应按药品的字母顺序递增
好 啊。

select    ID,
          [Date],
          Typ,
          Drug,
          Sno = row_number() over (partition by Date, Typ order by Drug asc)
from      YourTable

如果您还需要按id分区,请同时包括:

select    ID,
          [Date],
          Typ,
          Drug,
          Sno = row_number() over (partition by ID, Date, Typ order by Drug asc)
from      YourTable
7y4bm7vi

7y4bm7vi2#

你可以试试下面的-

select * row_number() over (partition by ID, Typ order by Drug) as Sno
from tablename

相关问题