我已经获得了一个包含7列Reject1,Reject2,...等的数据集。列中的每个拒绝代码都有一个优先级,1是最高优先级,9999是最低优先级。不幸的是,这些数据没有规范化(我知道在面对面拍摄我),我的任务是从一个记录中的七列中找到最高优先级的拒绝代码,然后返回该拒绝代码。
我能够做的是将数据连接到具有拒绝代码层次结构的表,即拒绝代码79 = 1优先级。所以我现在有7个Reject Code列和7个Priority列对应。
我最初的想法是编写一堆更新查询,在比较两个优先级列后更新TopPriority
列。
UPDATE tblPriority
SET tblPriority.HighestPriority = tblPriority.Priority1
所以上面的代码工作正常。问题出在SQL的第二位:
UPDATE tblPriority
SET tblPriority.HighestPriority = tblPriority.Priority2
WHERE tblPriority.HighestPriority > tblPriority.Priority2
上面的代码给我一个数据类型不匹配,我假设它是因为Priority2列为空?如果是这样,如何解决这个问题。提前谢谢!
答案 0 :(得分:2)
您可以非常轻松地规范化数据
Select thingBeingRejected, Reject1 as RejectCode, 'Reject1' as SourceColumn
union all Select thingBeingRejected, Reject2 as RejectCode, 'Reject2' as SourceColumn
union all Select thingBeingRejected, Reject3 as RejectCode, 'Reject3' as SourceColumn
union all Select thingBeingRejected, Reject4 as RejectCode, 'Reject4' as SourceColumn
union all Select thingBeingRejected, Reject5 as RejectCode, 'Reject5' as SourceColumn
union all Select thingBeingRejected, Reject6 as RejectCode, 'Reject6' as SourceColumn
union all Select thingBeingRejected, Reject7 as RejectCode, 'Reject7' as SourceColumn
然后找到最正常的
Select max(p.priority), nrc.thingBeingRejected
from NormalizedRejectCodes nrc
inner join Priorities p on p.RejectCode = nrc.RejectCode
group by nrc.thingBeingRejected