我有一个非常大的,未规范化的表,我正在修复它.从那张大表中我正在规范化数据.我使用了SQL语句
INSERT INTO smallTable(patientID, admissionDate, dischargeDate)
select distinct patientID, admissionDate, dischargeDate
FROM largeTable
Run Code Online (Sandbox Code Playgroud)
所以我的smallTable填充了正确的行数.还有另一个专栏,drgCode我想添加到我的smallTable中.我尝试了以下查询来做到这一点
INSERT INTO smallTable(drgCode)
select drgCode from
(
SELECT DISTINCT patientID, admissionDate, dischargeDate, drgCode from largeTable) as t
Run Code Online (Sandbox Code Playgroud)
我得到的错误是读cannot insert the value NULL into patientID, column does not alloq nulls, insert fails.
drgCode正确选择的唯一方法select distinct是使用查询的某些变体.如果必须包含其他字段以缩小搜索范围,如何只插入一个字段.
我知道如果我清空了我的smallTable,我可以做到这一点,但我认为必须要解决这个问题.
with drg as (SELECT DISTINCT patientID, admissionDate, dischargeDate, drgCode from largeTable)
update s
set s.drgCode = l.drgCode
from smallTable s join drg l on
s.patientId = l.patientId and
s.admissionDate = l.admissionDate and
s.dischargeDate = l.dischargeDate
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1125 次 |
| 最近记录: |