1

I have built my query here thus far:

SELECT POLICYNUMBER, FPLAN, FEFFYY, FEFFMM, FEFFDD, FINSTP, CLIENTNUM, FIRSTNAME, 
MIDNAME, LASTNAME, BIRTHDATE FROM PFCASBENE 
INNER JOIN CMRELATN ON POLICYNUMBER = KEYFIELD1 
INNER JOIN CMPERSON ON CLIENTNUM = CLIENTID
WHERE FPSFLG='I' OR FPSFLG='P' ORDER BY CLIENTNUM ASC

The FINSTP field has a single character for an insurance type code.

EDIT: POLICYNUMBER, FPLAN, FEFFYY, FEFFMM, FEFFDD, FINSTP are fields in PFCASBENE CLIENTNUM is a field in CMRELATN and FIRSTNAME, MIDNAME, LASTNAME, BIRTHDATE are from CMPERSON. I should have said that before.

I want to return the results if the client only has policies that the FINSTP='F'. If they have other policies that have FINSTP = 'X', 'V', etc, then I don't want any of the clients records in the results.

this query returns multiple rows since a client can have multiple policies. I can get results if I put FINSTP='F' in the WHERE clause, but that's not what I want. That returns all that are 'F'. I'm not sure what else I need to add to the where clause to tune this query to what I need it to do.

This is for a DB2 on an AS/400 system.

Any help would be greatly appreciated!

  • Josh
4

3 回答 3

2
SELECT POLICYNUMBER, FPLAN, FEFFYY, FEFFMM, FEFFDD, FINSTP, CLIENTNUM, FIRSTNAME, 
MIDNAME, LASTNAME, BIRTHDATE FROM PFCASBENE 
INNER JOIN CMRELATN ON POLICYNUMBER = KEYFIELD1 
INNER JOIN CMPERSON ON CLIENTNUM = CLIENTID
WHERE (FPSFLG='I' OR FPSFLG='P') AND CLIENTNUM not in (
SELECT CLIENTNUM FROM PFCASBENE 
INNER JOIN CMRELATN ON POLICYNUMBER = KEYFIELD1 
INNER JOIN CMPERSON ON CLIENTNUM = CLIENTID
WHERE FINSTP <> 'F')
ORDER BY CLIENTNUM ASC

Sub select 说,返回所有 FINSTP 不等于 F 的客户端记录,然后排除该客户端结果,即使它们的 FINSTP 为 F(但由于其他 FINSTP 值,整个客户端都被删除。但如果他们只有 FINSTP F 那么该客户将不会被排除在外。

我基于 OP 声明:

如果客户端只有FINSTP='F' 的策略,我想返回结果。如果他们有其他具有 FINSTP = 'X'、'V' 等的策略,那么我不希望结果中出现任何客户端记录。

于 2013-05-23T12:41:06.003 回答
1

这应该这样做:

SELECT POLICYNUMBER, FPLAN, FEFFYY, FEFFMM, FEFFDD, FINSTP, CLIENTNUM, FIRSTNAME, 
MIDNAME, LASTNAME, BIRTHDATE
FROM PFCASBENE 
JOIN CMRELATN ON POLICYNUMBER = KEYFIELD1 
JOIN CMPERSON ON CLIENTNUM = CLIENTID
WHERE FPSFLG IN ('I', 'P')
AND CLIENTNUM NOT IN (
    SELECT CLIENTNUM
    FROM PFCASBENE 
    JOIN CMRELATN ON POLICYNUMBER = KEYFIELD1 
    JOIN CMPERSON ON CLIENTNUM = CLIENTID
    WHERE FINSTP != 'F'
)
ORDER BY CLIENTNUM ASC

我通过使用FPSFLG IN ('I', 'P')而不是改进了您的主要查询FPSFLG 'I' OR FPSFLG = 'P',它除了更整洁之外,更有可能使用索引(如果该列上存在索引)。

我会将其编码为左连接,但我不知道哪些列来自哪些表 - 太多的猜测工作,这个查询会工作。

于 2013-05-23T12:37:02.093 回答
0

假设您的键是POLICYNUMBER, 并且FINSTPPFCASBENE;

SELECT 
    POLICYNUMBER, FPLAN, FEFFYY, FEFFMM, FEFFDD, FINSTP, CLIENTNUM, FIRSTNAME, 
    MIDNAME, LASTNAME, BIRTHDATE 
FROM PFCASBENE 
INNER JOIN CMRELATN ON POLICYNUMBER = KEYFIELD1 
INNER JOIN CMPERSON ON CLIENTNUM = CLIENTID
WHERE 
    (FPSFLG='I' OR FPSFLG='P')
AND POLICYNUMBER NOT IN(--Policies other than F. You don't want them.
    SELECT POLICYNUMBER FROM PFCASBENE 
    WHERE FINSTP <> 'F'
)
ORDER BY CLIENTNUM ASC
于 2013-05-23T12:35:19.477 回答