Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Table Join using unique values or where Null = all values


Table Join using unique values or where Null = all values

Author
Message
DerbyNeal
DerbyNeal
SSC-Enthusiastic
SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)

Group: General Forum Members
Points: 156 Visits: 909
Hi folks,

I have a complex query that involves many tables but I am struggling with one particular join (TableA and TableB). There is only one field in each table where a join can be made. TableA.Area contains values that can be matched with TableB. However TableA may also contain a Null value in the joining column. If there is a Null value, then TableA.Area needs to join with all values in TableB.ColId.


USE [DB1]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TableA](
[Id] [tinyint] IDENTITY(1,1) NOT NULL,
[Area] [nvarchar](10) NULL,
[Item] [nvarchar](10) NOT NULL,
[Description] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_TableA] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 85) ON [PRIMARY]
) ON [PRIMARY]
;

INSERT INTO dbo.TableA
(
Area, Item, [Description]
)
VALUES('A1', 'B1232', 'Thingymebobs')
;

INSERT INTO dbo.TableA
(
Area, Item, [Description]
)
VALUES('A4', 'B4352', 'Whatits')
;

INSERT INTO dbo.TableA
(
Area, Item, [Description]
)
VALUES(Null, 'B8769', 'Dunno')
;

USE [DB1]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TableB](
[ColId] [nvarchar](10) NOT NULL,
[DeliveryName] [nvarchar](60) NOT NULL,
CONSTRAINT [PK_TableB] PRIMARY KEY CLUSTERED
(
[ColId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 85) ON [PRIMARY]
) ON [PRIMARY]
;

INSERT INTO dbo.TableB
(
ColId, DeliveryName
)
VALUES('A1', 'Acme1')
;

INSERT INTO dbo.TableB
(
ColId, DeliveryName
)
VALUES('A4', 'Acme4')
;

INSERT INTO dbo.TableB
(
ColId, DeliveryName
)
VALUES('A8', 'Acme8')
;

INSERT INTO dbo.TableB
(
ColId, DeliveryName
)
VALUES('A9', 'Acme9')
;




The columns to be joined are TableA.Area AND TableB.ColId


SELECT b.DeliveryName, a.Item, a.[Description]
FROM
TableA a JOIN Tableb b ON --????
;



Any ideas please?

Thanks in advance,
Horatiu
Horatiu
SSC Veteran
SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)SSC Veteran (206 reputation)

Group: General Forum Members
Points: 206 Visits: 301
Something like this ?
SELECT b.DeliveryName, a.Item, a.[Description]
FROM
TableA a JOIN Tableb b ON (a.Area = b.ColId) or (a.Area is null)



There is always something new to learn.
My personal SQL Blog
raym85
raym85
Old Hand
Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)

Group: General Forum Members
Points: 376 Visits: 198
I think maybe you're looking for something like this?



SELECT DISTINCT *
FROM(
SELECT * FROM #TableA
where Area IS NULL )LL
FULL OUTER JOIN #TableB CB ON LL.Area = cb.ColId
LEFT OUTER JOIN (SELECT *
FROM #TableA a
JOIN #TableB b ON a.Area = b.ColId)PP
ON PP.Area = CB.ColId
0409SQL
0409SQL
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 109
Hi There

I think it would be better if you could provide us the format of output required.
Anyway try this:

SELECT A.Area, B.DeliveryName, A.Item, A.Description
FROM
TableA A inner join TableB B ON (A.Area = B.ColId) or (A.Area is NULL)
order by A.Area desc


Output:
---------


Area DeliveryName Item Description
A4 Acme4 B4352 Whatits
A1 Acme1 B1232 Thingymebobs
NULL Acme1 B8769 Dunno
NULL Acme4 B8769 Dunno
NULL Acme8 B8769 Dunno
NULL Acme9 B8769 Dunno




Not sure if this is what you need ! :-)
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9017 Visits: 19028
The spec isn't entirely clear; does this generate the results you're expecting to see?

SELECT b.*, a.*, x.* 
FROM TableB b
LEFT JOIN TableA a ON b.ColId = a.Area
LEFT JOIN TableA x ON x.Area IS NULL AND a.Area IS NULL



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search