Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Table Join using unique values or where Null = all values Expand / Collapse
Author
Message
Posted Wednesday, March 20, 2013 7:35 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, April 11, 2014 2:46 AM
Points: 146, Visits: 791
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,





Post #1433202
Posted Wednesday, March 20, 2013 7:47 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, April 07, 2014 5:37 AM
Points: 205, Visits: 245
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
Post #1433217
Posted Wednesday, March 20, 2013 8:39 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, March 06, 2014 12:39 PM
Points: 376, Visits: 172
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
Post #1433259
Posted Wednesday, March 20, 2013 9:32 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 11, 2014 1:02 AM
Points: 11, Visits: 97
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 !
Post #1433311
Posted Wednesday, March 20, 2013 10:31 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, April 17, 2014 9:33 AM
Points: 6,754, Visits: 12,854
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
Post #1433335
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse