这个会很快。我无法从row_number()
函数中得到我想要的内容。我得到了什么:
我需要row_number()
仅在具有不同patid
的不同dailyDosage
上增加。因此,屏幕上的第4-9行应该都是1.第13行应该是1(因为它是一个新的patid),第14行应该是2(因为每日剂量的变化。我得到的结果:
select distinct
ROW_NUMBER() over(partition by rx.patid,quantity/daysSup*cast(REPLACE(LEFT(strength,PATINDEX('%[^0-9]%',strength)),'m','') as int)
order by rx.patid,quantity/daysSup*cast(REPLACE(LEFT(strength,PATINDEX('%[^0-9]%',strength)),'m','') as int))
,rx.patid
,rx.drugName
,rx.strength
,rx.quantity
,rx.daysSup
,rx.fillDate
,quantity/daysSup*cast(REPLACE(LEFT(strength,PATINDEX('%[^0-9]%',strength)),'m','') as int) as dailyDosage
from rx
inner join (select distinct m.patid, m.sex, m.injurylevel from members as m) as m on m.PATID=rx.patid
where ISNUMERIC(REPLACE(LEFT(strength,PATINDEX('%[^0-9]%',strength)),'m',''))=1
and REPLACE(LEFT(strength,PATINDEX('%[^0-9]%',strength)),'m','') not like '%.%'
and drugname in ('baclofen')
and daysSup !=0 and quantity !=0
and daysSup > 1
order by rx.patid
SQL Server 2008 R2
答案 0 :(得分:5)
我认为您正在尝试对每日剂量进行排名。尝试使用dense_rank
而不是row_nubmer
。
Row_number枚举行。排名保持相同的值。 Dense_rank执行枚举,分配第一组1,下一组2,依此类推。 Rank在编号中留下空白。
答案 1 :(得分:4)
您应该更改ROW_NUMBER
并使用RANK
。