在下面的SQL上,它在我的Excel工作表中找到“坏行”并将它们复制到另一个表中。 这很完美。但是,在下面的每个OR声明下,我想使列“rejectionreason”=一些错误文本。 因此,例如,如果eventID为= 0,则将行移至表中,同时将“rejectionreason”列更新为文本“errorID等于0的错误”。
我还需要对下面的所有其他OR语句做类似的事。
我该怎么做
SQL
REPLACE INTO InvalidBaseDataTable SELECT * FROM BaseDataTable where dateTime = '0000-00-00 00:00:00'
OR eventId = 0
OR ueType = 0
OR eventId NOT IN (SELECT DISTINCT(eventId) FROM EventCauseTable)
OR causeCode < (SELECT MIN(causeCode) FROM EventCauseTable)
OR causeCode > (SELECT MAX(causeCode) FROM EventCauseTable)
OR ueType NOT IN (SELECT DISTINCT(tac) FROM UeTable)
OR
(eventId NOT IN (SELECT DISTINCT(eventId) FROM EventCauseTable)
AND
causeCode NOT IN (SELECT DISTINCT(causeCode) FROM EventCauseTable))
答案 0 :(得分:3)
您可以尝试以下方式:
REPLACE INTO InvalidBaseDataTable
SELECT *, CASE
WHEN dateTime = '0000-00-00 00:00:00' THEN 'datetime equal to 0'
WHEN eventId = 0 THEN 'eventId equal to 0'
WHEN ueType = 0 THEN 'ueType equal to 0'
ELSE 'All good'
END AS rejectionreason
FROM BaseDataTable WHERE dateTime = '0000-00-00 00:00:00'
OR eventId = 0
OR ueType = 0
OR eventId NOT IN (SELECT DISTINCT(eventId) FROM EventCauseTable)
OR causeCode < (SELECT MIN(causeCode) FROM EventCauseTable)
OR causeCode > (SELECT MAX(causeCode) FROM EventCauseTable)
OR ueType NOT IN (SELECT DISTINCT(tac) FROM UeTable)
OR (eventId NOT IN (SELECT DISTINCT(eventId) FROM EventCauseTable)
AND
causeCode NOT IN (SELECT DISTINCT(causeCode) FROM EventCauseTable))
答案 1 :(得分:2)
我会做这样的事情。
特别是因为您只需要进行一次测试而不是一次就能找到问题,然后再次进行相同的测试以找到原因。
select *
from (select id,
case when dateTime = '0000-00-00 00:00:00' then 'dateTime = 00000-00-00 00:00:00' else
case when eventId = 0 or ueType = 0 then 'eventId or ueType = 0' else
case when eventId not in (SELECT DISTINCT(eventId) FROM EventCauseTable) then 'eventId not in EventCauseTable' else
case when testMe2 not in (select value from valueList) then 'Value in testMe2 is not in Values' else null end end end end end result
from testThis) x
where result is not null;