views:

289

answers:

3

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;
+1  A: 

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
THEn
Will this work if I have instances like the last two in my example above where they have the same CACLAIM (aka I4YFTX) but different DOIs (I4YWTX)? And what about when they have the same CACLAIM and DOI and the only thing differentiating them is the AGCLAIM which is not on every record in WEI4CPP?
I think you cloud add condition. I will check and update it.
THEn
Question is when I4X6TX is empty how do you know which one it belongs to?
THEn
I4YFTX is supposed to be a unique number for each "group" of records, which is how you'd know that those records go together. I'm trying to get around some system errors that allowed the same number in that field mulitple times for what should be different "groups" by using other variables such as dates.
I updated the query. just add I4YWTX in group by. That should work.
THEn
A: 

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.

CTKeane
A: 

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.

breitak67