dbTalk Databases Forums  

Select JOIN problem

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Select JOIN problem in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
shumaker@cs.fsu.edu
 
Posts: n/a

Default Select JOIN problem - 05-06-2005 , 10:28 AM






The query:
SELECT BTbl.PKey, BTbl.Result
FROM BTbl INNER JOIN
ATbl ON BTbl.PKey = ATbl.PKey
WHERE (ATbl.Status = 'DROPPED') AND (BTbl.Result <> 'RESOLVED')

Returns no rows.
If I do:
SELECT BTbl.PKey, BTbl.Result
FROM BTbl INNER JOIN
ATbl ON BTbl.PKey = ATbl.PKey
WHERE (ATbl.Status = 'DROPPED')

Returns:
PKey Result
125
127 RESOLVED

I want the first query to return the row with PKey: 125 because it's
result field does not equal 'RESOLVED'
Any ideas what I'm doing wrong?

My tables:

CREATE TABLE [dbo].[ATbl] (
[PKey] [int] NOT NULL ,
[Status] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[BTbl] (
[PKey] [int] NOT NULL ,
[Result] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO


Reply With Quote
  #2  
Old   
Simon Hayes
 
Posts: n/a

Default Re: Select JOIN problem - 05-06-2005 , 12:44 PM







<shumaker (AT) cs (DOT) fsu.edu> wrote

Quote:
The query:
SELECT BTbl.PKey, BTbl.Result
FROM BTbl INNER JOIN
ATbl ON BTbl.PKey = ATbl.PKey
WHERE (ATbl.Status = 'DROPPED') AND (BTbl.Result <> 'RESOLVED')

Returns no rows.
If I do:
SELECT BTbl.PKey, BTbl.Result
FROM BTbl INNER JOIN
ATbl ON BTbl.PKey = ATbl.PKey
WHERE (ATbl.Status = 'DROPPED')

Returns:
PKey Result
125
127 RESOLVED

I want the first query to return the row with PKey: 125 because it's
result field does not equal 'RESOLVED'
Any ideas what I'm doing wrong?

My tables:

CREATE TABLE [dbo].[ATbl] (
[PKey] [int] NOT NULL ,
[Status] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[BTbl] (
[PKey] [int] NOT NULL ,
[Result] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

What exactly is BTbl.Result where BTbl.PKey is 125? If it's a NULL, then
this behaviour would be correct; if it's an empty string, then your query
should work. If you do have NULLs in the column, you could try this query
instead:

SELECT BTbl.PKey, BTbl.Result
FROM BTbl INNER JOIN
ATbl ON BTbl.PKey = ATbl.PKey
WHERE (ATbl.Status = 'DROPPED') AND (coalesce(BTbl.Result, '') <>
'RESOLVED')

If this doesn't help, I suggest you post INSERTs to add some sample data to
your tables so that someone else can reproduce what you're seeing.

Simon




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.