Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 6th, 2005, 05:01 AM
Registered User
 
Join Date: Nov 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to kim3er
Default Trouble with Subqueries, am I going mad.

The problem that faces me relates to the statement below:

select (isnull(c.FIRSTNAMES,'')
        + ' '
        + isnull(c.[NAME],'')) ClientName,
    c.REFER ClientRef,
    p.CONT_NUM PolicyRef,
    p.POL_NUM PolicyID,
    cs.[Desc] ,
    p.STATUS

from S_POLMAI p
inner join S_CLIENT c
    on p.CLIENT_NUM = c.CLIENT_NUM
inner join Hydra.dbo.def_PolicyStatus cs
    on p.STATUS = cs.[ID]

where p.PLAN_CODE = 'DGT'
      and p.POL_NUM not in (select [id] from #temp1) -- Line in dispute
    and p.POL_NUM not in (select distinct PolicyID
                from TrackItems
                where [Description] like '%blar blar blar%'
                    or [Description] like '%blar blar blar%')
    and p.STATUS in (19,20)
and p.POL_NUM = ***** -- Test Case

order by c.[NAME]

With the 'Line in dispute' commented out the statement returns a single row relating to the POL_NUM entered as a test case. The table #temp1 contains a list of POL_NUM's excluding the one I have used as my test case. POL_NUM and [id] (returned by #temp1) are of the same data type. Running the full statement returns no rows. Changing the 'Line in dispute' to 'in' rather than 'not in', also returns no rows. Replacing the select statement with a comma delimited list of numbers excluding the test case POL_NUM, works as expected.

 
Old April 6th, 2005, 08:06 AM
Registered User
 
Join Date: Nov 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to kim3er
Default

I've managed to reduce the problem to this statement.

select *
from S_POLMAI
where POL_NUM not in (select distinct [id] from #temp1)

There are over 2000 distinct POL_NUM's in S_POLMAI, exactly 180 distinct [id]'s in #Temp1

When I run the query above, 0 rows are returned. When take 'not' out of the where clause 179 rows are returned.

 
Old April 6th, 2005, 08:22 AM
Registered User
 
Join Date: Nov 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to kim3er
Default

Problem solved. There was a null column in #Temp1

 
Old April 10th, 2005, 02:13 PM
Authorized User
 
Join Date: Mar 2005
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

there is no need to use distinct in this query

select *
from S_POLMAI
where POL_NUM not in (select distinct [id] from #temp1)

atul






Similar Threads
Thread Thread Starter Forum Replies Last Post
which is faster Subqueries or Views? lakshminal SQL Language 0 May 1st, 2008 11:20 PM
SubQueries debbiecoates SQL Server 2000 5 October 22nd, 2007 02:04 PM
Multiple subqueries? thf1977 MySQL 1 October 24th, 2006 05:30 PM
Driving me mad - getting a div to display in IE elfranko HTML Code Clinic 0 December 21st, 2005 10:49 AM
subqueries mgdts SQL Server DTS 0 July 28th, 2003 01:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.