Question

Voici la situation dans laquelle je me trouve. J'ai un tableau contenant les informations des personnes. Certains d'entre eux sont importés d'un autre système alors que d'autres sont importés manuellement. J'aimerais attirer tout le monde, mais si un enregistrement est entré manuellement et qu'un enregistrement a été importé, je souhaite sélectionner uniquement l'enregistrement importé (car il est probablement plus à jour).

Voici ce que j'ai jusqu'à présent, mais cela ne semble pas fonctionner (il ne renvoie que les enregistrements saisis manuellement):

SELECT --fields go here
FROM
        (
                SELECT PERSON_UID, instype
                FROM AdmitsInterfaceTable
                WHERE instype='M' --Manually entered people
            EXCEPT
                SELECT PERSON_UID, instype
                FROM AdmitsInterfaceTable
                WHERE instype='I' --Imported people
        ) P,
        AdmitsInterfaceTable A
     WHERE 
        P.PERSON_UID=A.PERSON_UID
        AND P.instype=A.instype

J’ai le sentiment que cela ne fonctionne pas à cause de la requête interne qui figure également dans la colonne instype, mais je ne trouve pas de meilleure façon de le faire. Un conseil?

Était-ce utile?

La solution

 SELECT PERSON_UID, instype
    FROM AdmitsInterfaceTable a
    WHERE instype='I' --get all imported people            
 union -- plus all manuals who are not also imported
 SELECT PERSON_UID, instype
    FROM AdmitsInterfaceTable a
    WHERE instype='M' --Imported people
    and not exists (  -- without imported matches
     select * from FROM AdmitsInterfaceTable b
     WHERE instype='I' and b.person_uid = a.person_uid);

Autres conseils

vous pouvez essayer:

SELECT CASE WHEN b.person_UID is null THEN a.person_UID ELSE person_UID END, 
CASE WHEN b.person_UID is null THEN a.instype   ELSE instype   END
--and so forth through all the columns you want
FROM
(SELECT PERSON_UID, instype  -- add list of additional fields you need here
from AdmitsInterfaceTable  WHERE instype='M' ) A
LEFT JOIN
(SELECT PERSON_UID, instype  -- add list of additional fields you need here
from AdmitsInterfaceTable  WHERE instype='I' ) B on A.PERSON_UID = B.PERSON_UID
SELECT --fields go here
FROM AdmitsInterfaceTable A
 WHERE 
    P.PERSON_UID=A.PERSON_UID
    AND P.instype=A.instype
AND instype='M' --Manually entered people
AND person_uid not in (
SELECT PERSON_UID FROM AdmitsInterfaceTable
WHERE instype='I' --Imported people
)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top