我有一个非常大的、未标准化的表,我正在修复它。从那个大表中,我正在对数据进行规范化。我使用了 SQL 语句
INSERT INTO smallTable(patientID, admissionDate, dischargeDate)
select distinct patientID, admissionDate, dischargeDate
FROM largeTable
所以我的 smallTable 填充了正确的行数。还有另一列,drgCode
我想添加到我的 smallTable 中。我尝试了以下查询来做到这一点
INSERT INTO smallTable(drgCode)
select drgCode from
(
SELECT DISTINCT patientID, admissionDate, dischargeDate, drgCode from largeTable) as t
我收到的错误是cannot insert the value NULL into patientID, column does not alloq nulls, insert fails
.
drgCode
正确选择的唯一方法select distinct
是使用查询的某些变体。当必须包含其他字段以缩小搜索范围时,如何仅插入一个字段。
我知道如果我清空我的 smallTable,我可以做到这一点,但我认为必须有办法解决它。