帮助处理 SQL 中的关联查询

发布于 2024-07-25 03:01:38 字数 1847 浏览 16 评论 0原文

请帮忙!! 我对 SQL 编程是全新的,并且正在自学一切。 我终于遇到了一个我似乎无法解决的问题。 希望有人可以提供帮助。 我有以下表格。 我需要做的是使用 FROISEDI 中的 RECDATE 并计算 WEI4CPP 中该日期之前(包括该日期)的所有不同日期 (I4C7DZ)。 我使用了下面的代码,它似乎一直有效,直到我有一条具有相同 CACLAIM 值的记录。 我还遇到过两个记录具有相同的 CACLAIM 和 DOI 的情况,因此我的代码也不起作用。 不知何故,我需要使用 FROISEDI 的 AGCLAIM,但我不知道如何将其放入我的计数表中。 哦,我使用的是 SAS,所以它不完全是 SQL,但非常接近。 谢谢你的帮助!


TABLE: FROISEDI
AGCLAIM    RECDATE   CACLAIM     DOI
09073589   1/29/09   09 41615    1/28/09  
09115390   3/01/09   00012HR09   2/23/09  
09234567   4/20/09   003140010   1/2/09  
09154826   5/01/09   003140010   4/28/09  

TABLE: WEI4CPP  
 I4C7DZ     I4X6TX      I4YWTX    I4YFTX  
1/28/09               1/28/09    09 41615  
1/29/09   09073589    1/28/09    09 41615  
1/30/09   09073589    1/28/09    09 41615  
2/24/09               2/23/09    00012HR09  
2/28/09               2/23/09    00012HR09    
3/01/09   09115390    2/23/09    00012HR09  
3/15/09   09115390    2/23/09    00012HR09  
1/15/09               1/02/09    003140010  
1/20/09               1/02/09    003140010  
2/08/09               1/02/09    003140010  
3/19/09               1/02/09    003140010  
4/20/09   09234567    1/02/09    003140010  
5/01/09   09154826    4/28/09    003140010  

TABLE I NEED TO PRODUCE: COUNTS  
AGCLAIM     CACLAIM   DOI      SUBMITS  
09073589    09 41615     1/28/09    2  
09115390    00012HR09    2/23/09    3  
09234567    003140010    1/02/09    5  
09154826    003140010    4/28/09    1  

我使用的代码:

PROC SQL;
CREATE TABLE COUNTS AS
SELECT I4YWTX AS DOI3,
I4YFTX AS CLMNUM2,
COUNT(DISTINCT I4C7DZ) AS SUBMITS
FROM WAREHOUS.WEI4CPP A
WHERE I4C7DZ<=(SELECT RECDATE
         FROM FROISEDI 
         WHERE FROISEDI.CACLAIM=A.I4YFTX
         AND FROISEDI.DOI=A.I4YWTX) 
GROUP BY WEI4CPP.I4YFTX, WEI4CPP.I4YWTX;
QUIT;

Please help!! I am brand new to SQL programming and am teaching myself everything as I go along. I’ve finally run into a problem I can’t seem to tackle. Hopefully someone can help. I have the following tables. What I need to do is use the RECDATE in FROISEDI and count all the distinct dates (I4C7DZ) prior to and including that date in WEI4CPP. I’ve used the code below and it seems to work until I have a record that has the same CACLAIM value. I also have instances where two records have the same CACLAIM and DOI so my code doesn’t work then either. Somehow, I need to use AGCLAIM from FROISEDI but I don't know how to pull it into my Counts table. Oh, and I’m using SAS so it’s not exactly SQL but really close. Thanks for any help!


TABLE: FROISEDI
AGCLAIM    RECDATE   CACLAIM     DOI
09073589   1/29/09   09 41615    1/28/09  
09115390   3/01/09   00012HR09   2/23/09  
09234567   4/20/09   003140010   1/2/09  
09154826   5/01/09   003140010   4/28/09  

TABLE: WEI4CPP  
 I4C7DZ     I4X6TX      I4YWTX    I4YFTX  
1/28/09               1/28/09    09 41615  
1/29/09   09073589    1/28/09    09 41615  
1/30/09   09073589    1/28/09    09 41615  
2/24/09               2/23/09    00012HR09  
2/28/09               2/23/09    00012HR09    
3/01/09   09115390    2/23/09    00012HR09  
3/15/09   09115390    2/23/09    00012HR09  
1/15/09               1/02/09    003140010  
1/20/09               1/02/09    003140010  
2/08/09               1/02/09    003140010  
3/19/09               1/02/09    003140010  
4/20/09   09234567    1/02/09    003140010  
5/01/09   09154826    4/28/09    003140010  

TABLE I NEED TO PRODUCE: COUNTS  
AGCLAIM     CACLAIM   DOI      SUBMITS  
09073589    09 41615     1/28/09    2  
09115390    00012HR09    2/23/09    3  
09234567    003140010    1/02/09    5  
09154826    003140010    4/28/09    1  

CODE I'VE USED:

PROC SQL;
CREATE TABLE COUNTS AS
SELECT I4YWTX AS DOI3,
I4YFTX AS CLMNUM2,
COUNT(DISTINCT I4C7DZ) AS SUBMITS
FROM WAREHOUS.WEI4CPP A
WHERE I4C7DZ<=(SELECT RECDATE
         FROM FROISEDI 
         WHERE FROISEDI.CACLAIM=A.I4YFTX
         AND FROISEDI.DOI=A.I4YWTX) 
GROUP BY WEI4CPP.I4YFTX, WEI4CPP.I4YWTX;
QUIT;

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

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

发布评论

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

评论(3

不气馁 2024-08-01 03:01:38

没有测试过。
尝试这个

SELECT 
    AGCLAIM, 
    CACLAIM,     
    DOI, T.cnt + 1 AS SUBMITS
FROM 
    FROISEDI 
    INNER JOIN 
    (
    SELECT 
        COUNT(*) cnt,
        I4YFTX ,
            I4YWTX     
    FROM 
        WEI4CPP   
    WHERE 
        ISNULL(I4X6TX,0) = 0 
    GROUP BY 
        I4YFTX, I4YWTX    
    ) T 
    ON FROISEDI.CACLAIM = T.I4YFTX

Didn't test it.
try this

SELECT 
    AGCLAIM, 
    CACLAIM,     
    DOI, T.cnt + 1 AS SUBMITS
FROM 
    FROISEDI 
    INNER JOIN 
    (
    SELECT 
        COUNT(*) cnt,
        I4YFTX ,
            I4YWTX     
    FROM 
        WEI4CPP   
    WHERE 
        ISNULL(I4X6TX,0) = 0 
    GROUP BY 
        I4YFTX, I4YWTX    
    ) T 
    ON FROISEDI.CACLAIM = T.I4YFTX
陌生 2024-08-01 03:01:38

我没有详细说明您的逻辑,但请确保两个日期都存储为数值。 我不止一次看到日期存储为字符,所以 GT 和 LT 并不像你想象的那样工作。

I haven't gone through your logic in detail but make sure that both dates are being stored as numeric values. I've seen more than once a date stored as a char so GT and LT then don't work the way you think.

小忆控 2024-08-01 03:01:38

看起来 WEI4CPP.I4YFTX 是 FROISEDI.CACLAIM 链接值,WEI4CPP.I4YWTX 是 FROISEDI.DOI。 难道就这么简单吗?

SELECT fr.AGCLAIM, fr.CACLAIM, fr.DOI, COUNT(we.I4C7DZ) as SUBMITS
FROM    FROISEDI fr
        INNER JOIN WEI4CPP we
            ON we.I4YFTX = fr.CACLAIM
            AND     we.I4YWTX = fr.DOI
            AND     ISNULL(we.I4X6TX, fr.AGCLAIM) = fr.AGCLAIM
WHERE   we.I4C7DZ <= fr.RECDATE
GROUP BY fr.AGCLAIM, fr.CACLAIM, fr.DOI

请注意,如果行具有相同的 DOI 和 CACLAIM,但 WEI4CPP.I4X6TX 列为空,则会出现问题。 在这种情况下,我不知道您关于解析它们属于哪个 AGCLAIM 的业务规则。 如果它们已填写且不匹配,我可以拒绝它们,但否则我需要某种与日期相关的信息或其他信息来匹配它们。

It looks like the WEI4CPP.I4YFTX is the FROISEDI.CACLAIM linking value, and WEI4CPP.I4YWTX is FROISEDI.DOI. Is it as simple as this?

SELECT fr.AGCLAIM, fr.CACLAIM, fr.DOI, COUNT(we.I4C7DZ) as SUBMITS
FROM    FROISEDI fr
        INNER JOIN WEI4CPP we
            ON we.I4YFTX = fr.CACLAIM
            AND     we.I4YWTX = fr.DOI
            AND     ISNULL(we.I4X6TX, fr.AGCLAIM) = fr.AGCLAIM
WHERE   we.I4C7DZ <= fr.RECDATE
GROUP BY fr.AGCLAIM, fr.CACLAIM, fr.DOI

Note there will be an issue with this if rows have the same DOI and CACLAIM, but the WEI4CPP.I4X6TX column is empty. In this case, I do not know your business rules about resolving which AGCLAIM they belong to. I can reject them if they are filled in and don't match, but I would need some kind of date-related or other information to match them otherwise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文