Code: Select all
SELECT DISTINCT
A.state_num,
A.section_code,
A.sequence_num,
B.claim_status,
A.incurred_date,
A.policy_date,
Year(A.incurred_date) as incYear,
Year(A.policy_date) as polYear,
B.line_item,
left(B.line_item,2) as Coverage
FROM
claim_header A INNER JOIN claim_detail B ON
A.state_num = B.state_num AND
A.section_code = B.section_code AND
A.sequence_num = B.sequence_num
WHERE
report_date <= '12/31/2004' AND
((B.claim_status <> 'C' AND Left(B.line_item,2) IN ('11', '12', '19', '80', '41', '42', '71', '72', '73', '51', '52', '82')) OR
(A.Claim_status <> 'C' AND ((A.section_code = 'WC' AND B.line_item = '990') OR (A.section_code = 'XW' AND B.line_item = '990'))))
ORDER BY
A.state_num,
A.section_code,
Coverage, incYear,
A.sequence_num
This is the actual part I'm having trouble with. Some background. All WC and XW records should have a 990 line_item. Some WC and XW records will have a 991 line_item. I don't want the 991 at all. Here's the problem, some rare XW records will not have a 990 line_item. In these cases, I want the 991. I can't use an OR statement there because it will return both 990 and 991 for those XW records that have them.
I've looked at both the CASE and COALESCE statements but can't really make heads or tails of them. Anybody here have any ideas on what I can do here? Thanks.