我是SQL的新手。我有一个包含44列的5000条记录表。我需要有一个该表的子集,其中包含10个列以及对特定列(NationalCode
)具有唯一值的所有行。
换句话说,此子集应显示所有唯一身份,并忽略具有重复NationalCode
的行。
我知道下面的查询是错误的但是正确的形式是什么?
insert into Patient(NationalID, P_FirstName, P_LastName, P_FatherName, Gender, P_Age, P_AgeGroup, P_MaritalStatus)
select
P_NationalCode, P_FirstName, P_LastName,
P_FatherName, FullDateAlternateKey, P_Age, P_AgeGroup,
P_MaritalStatus
from
OriginalData
where
P_NationalCode in (select distinct P_NationalCode
from OriginalData)
任何帮助都将不胜感激。
答案 0 :(得分:0)
试试这个
insert into Patient(NationalID, P_FirstName, P_LastName, P_FatherName, Gender, P_Age, P_AgeGroup, P_MaritalStatus)
select
P_NationalCode, P_FirstName, P_LastName,
P_FatherName, FullDateAlternateKey, P_Age, P_AgeGroup,
P_MaritalStatus
from
(select P_NationalCode, P_FirstName, P_LastName,
P_FatherName, FullDateAlternateKey, P_Age, P_AgeGroup,
P_MaritalStatus ,row_number() over(partition by P_NationalCode order by P_NationalCode) as rowNumber from OriginalData
) data
where
rowNumber=1