Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SQL Query conditional statement **RESOLVED**

Author  Topic 

acoggins75
Starting Member

2 Posts

Posted - 2005-01-05 : 15:19:29
Hey guys, my first post here.

I've got a query that I am dynamically building using VB.NET and has become pretty complicated as its developed. There is a portion in the WHERE clause were if 1 code is not present, I need to search for another. Here is the query with some of the unecessary stuff trimmed out:

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


(A.section_code = 'XW' AND B.line_item = '990')

This is the line I'm having troubles with. Under normal circumstances, ALL WC and XW records will have a 990 line_item. There will occaisionally be an instance however where an XW record will not. In those cases, I need to pull the 991 line_item. My problem here is that it's possible for both WC and XW records to have both a 990 and a 991. I need only the 990 in those cases where 1 exists and only the 991 if a 990 doesn't exist.

Does that make sense? Thanks in advance for any help on this.

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2005-01-05 : 15:58:46
You could use a NOT EXISTS predicate to detect where the '990' condition exists:

(A.section_code = 'XW' AND
(B.line_item = '990' OR
(B.line_item = '991' AND NOT EXISTS (
select *
from claim_header A1 INNER JOIN claim_detail B1 ON
A1.state_num = B1.state_num AND
A1.section_code = B1.section_code AND
A1.sequence_num = B1.sequence_num
where A1.report_date <= '12/31/2004' AND
A1.Claim_status <> 'C' AND
(A1.section_code = 'XW' AND B1.line_item = '990'))
)

HTH

=================================================================

Where it is a duty to worship the sun, it is pretty sure to be a crime to examine the laws of heat. -John Morley, statesman and writer (1838-1923)
Go to Top of Page

acoggins75
Starting Member

2 Posts

Posted - 2005-01-06 : 13:27:59
Thanks for that. I got it working with just a little bit of tweaking:

WHERE 

claimDetail.report_date <= '12/31/2004' AND
((claimHeader.claim_status <> 'C' AND ((claimHeader.section_code = 'WC' AND claimDetail.line_item = '990') OR (claimHeader.section_code = 'XW' AND (claimDetail.line_item = '990' OR
(claimDetail.line_item = '991' AND NOT EXISTS (
SELECT CD.line_item FROM
claim_header CH INNER JOIN claim_detail CD ON
CH.state_num = CD.state_num AND
CH.section_code = CD.section_code AND
CH.sequence_num = CD.sequence_num
WHERE
CD.state_num = claimHeader.state_num AND
CD.section_code = claimHeader.section_code AND
CD.sequence_num = claimHeader.sequence_num AND
CD.report_date <= '12/31/2004' AND
CH.Claim_status <> 'C' AND
(CH.section_code = 'XW' AND CD.line_item = '990')
)))))))
Go to Top of Page
   

- Advertisement -