SQL Server 除外

发布于 2024-07-18 18:14:46 字数 721 浏览 7 评论 0原文

这是我所处的情况。我有一个包含人员信息的表。 其中有些是从其他系统导入的,有些是手动导入的。 我想做的是将所有人都拉进来,但如果有一条手动输入的记录和一条导入的记录,我只想选择导入的记录(因为它可能是最新的)。

这是我到目前为止所拥有的,但它似乎不起作用(它只返回手动输入的记录):

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

我有一种感觉这不起作用,因为内部查询也拉入了 instype 列,但我可以想不出更好的方法来做到这一点。 有什么建议吗?

Here's the situation I'm in. I have a table containing peoples' information. Some of them are imported from another system while some are imported manually. What I would like to do is pull everyone in, but if there is a record entered manually and a record that was imported, I want to select only the imported one (since it is likely more up to date).

Here is what I have so far, but it doesn't seem to work (it only returns the manually entered records):

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

I have a feeling this isn't working because of the inner query also pulling in the instype column, but I can't think of any better ways to do this. Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖上得阳光 2024-07-25 18:14:46
 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);
 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);
嘿哥们儿 2024-07-25 18:14:46

你可以尝试:

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

you could try:

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
凉栀 2024-07-25 18:14:46
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
)
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
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文