Click to See Complete Forum and Search --> : Need query writing help


SoonerTJ
June 20th, 2005, 12:57 PM
Hi.
I have a problem, hoping to get any ideas/help I can. Let
me know what you think. Thanks.

My problem is identifing non duplicate data, not based on a
specific row, but based on specific "sets" of rows (in this
case tbl_id being the same number constitutes a set). Tbl =
1 or 2 or 3, etc.

Here's some test data from tmp_qual table.

tbl id qual_tbl qual_value_type qual_value inc_excl
1 1 16 916-454 +
1 1 16 916-636 +
1 1 16 916-989 +
2 2 16 916-989 +
3 3 16 916-454 -
4 4 16 913-315 -
4 4 16 916-454 -
4 4 16 916-989 -
5 5 16 916-454 -
5 5 16 916-636 -
5 5 16 916-989 -
5 5 16 972-408 -

From this data I would like tbl_id = 5 to be returned:
tbl id qual_tbl qual_value_type qual_value inc_excl
5 5 16 916-454 -
5 5 16 916-636 -
5 5 16 916-989 -
5 5 16 972-408 -

I need this "Set" of data to be identified as the only non
duplicate set from the original data. Because the 972-408
value differentiates it from tbl_id #1.

I used this query:
SELECT tbl_id, qual_tbl, qual_value_type, qual_value,inc_excl
FROM #qual_tbl
WHERE tbl_id NOT IN ( SELECT tbl_id, qual_value
FROM #qual_tbl q1
WHERE ( SELECT count( * )
FROM #qual_tbl q2
WHERE q2.qual_value = q1.qual_value
AND q2.tbl_id != q2.tbl_id) = 0
)

And nothing returned, although I expected it too.

Any ideas/help??

Please let me know. Thanks!

erickwidya
June 20th, 2005, 10:11 PM
what is the criteria that makes 972-408 different from tbl_id #1?

if it is the first 3 character so it will return 972 and 913 which is having the minimum count
try this
SELECT Table1.*
FROM Table1
WHERE LEFT(table1.qual_value,3) = ANY
(SELECT que1.LeftVal
FROM
(SELECT COUNT(*) as TotalCount, Left(t2.qual_value,3) AS LeftVal
FROM table1 AS t2
GROUP BY Left(t2.qual_value,3)
HAVING COUNT(*) =1) as que1)

PS : don't forget to change to appropriate FieldName