|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, May 02, 2013 4:18 AM
Points: 144,
Visits: 755
|
|
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,
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: 2 days ago @ 3:14 AM
Points: 205,
Visits: 222
|
|
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
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 3:51 PM
Points: 376,
Visits: 165
|
|
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
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 9:13 AM
Points: 3,
Visits: 46
|
|
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 !
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: 2 days ago @ 9:27 AM
Points: 5,618,
Visits: 10,990
|
|
|
|
|