سؤال

On the SQL below it finds "bad rows" in my excel sheet and copies them into another table. This works perfect. However under each OR satement below i want to make the column "rejectionreason" = some error text. So for example if the eventID was = 0 then move the row to the table but also update the column "rejectionreason" to the text "Error the eventID was equals to 0".

I also need to do similar for all the other OR statements below.

How can i do this

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))
هل كانت مفيدة؟

المحلول

You could try something like:

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))

نصائح أخرى

I would do something like this.

Especially since you would want to only have to make the test once and not once to find the problem and then the same test again just to find the cause.

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;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top