选择记录的第一个实例 [英] Select the first instance of a record

查看:27
本文介绍了选择记录的第一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,myTable,其中有两个字段IDpatientID.相同的患者 ID 可以使用不同的 ID 多次出现在表中.如何确保我只获得每个 patientIDONE 实例?

I have a table, myTable that has two fields in it ID and patientID. The same patientID can be in the table more than once with a different ID. How can I make sure that I get only ONE instance of every patientID.?

我知道这不是完美的设计,但我需要从数据库和今天获取一些信息,然后再修复它.

I know this isn't perfect design, but I need to get some info out of the database and today and then fix it later.

推荐答案

您可以将 CTEROW_NUMBER 函数:

You could use a CTE with ROW_NUMBER function:

WITH CTE AS(
    SELECT myTable.*
    , RN = ROW_NUMBER()OVER(PARTITION BY patientID ORDER BY ID)
    FROM myTable 
)
SELECT * FROM CTE
WHERE RN = 1

这篇关于选择记录的第一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆