HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Where Clause With Full Outer Join

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
fullwithwherejoinclauseouter

Problem

I may just be setting up my query completely incorrect but my expected result set that I need returned is Nuestra B & Nosotros B since they fall into the date range respectively. I.E. Nuestra B feeduedate >= '20160301' AND fj.feeduedate = '20170601' AND fe.ViolationDate <= '20170606' they fall into place.

However when I run this query attempting to return the result I get 0 returned in my result set.

What is incorrect with my query? Below is sample data & DDL

```
CREATE TABLE [dbo].fei NOT NULL,
[violationNumber] varchar NOT NULL,
[violationDate] [date] NOT NULL
) ON [PRIMARY]

CREATE TABLE [dbo].FJI NOT NULL,
[VIN] varchar NOT NULL,
[vfees1] [float] NOT NULL,
[vfees2] [float] NOT NULL,
[vfees3] [float] NOT NULL,
[vfees4] [float] NOT NULL,
[vfees5] [float] NOT NULL,
[vfees6] [int] NULL,
[feeduedate] [date] NULL,
[totalvfees] [float] NOT NULL,
[totalvfeespaid] [float] NOT NULL,
[vfeesremaining] [float] NOT NULL,
[vfee7] [float] NOT NULL,
[vfee8] [float] NOT NULL,
[vfee9] [float] NOT NULL,
[vfee10] [float] NOT NULL,
[vfee11] [float] NOT NULL,
[vfee12] [float] NULL,
[vfee13] [float] NULL,
[vfee14] [float] NULL,
[vfee15] [float] NULL,
[vfee16] [float] NULL
) ON [PRIMARY]

INSERT [dbo].[fei] ([violatorsName], [violationNumber], [violationDate]) VALUES (N'Nostra A', N'3244', CAST(0xE63C0B00 AS Date))
INSERT [dbo].[fei] ([violatorsName], [violationNumber], [violationDate]) VALUES (N'Nuestra B', N'408', CAST(0xE53C0B00 AS Date))
INSERT [dbo].[FJI] ([violatorsName], [VIN], [vfees1], [vfees2], [vfees3], [vfees4], [vfees5], [vfees6], [feeduedate], [totalvfees], [totalvfeespaid], [vfeesremaining], [vfee7], [vfee8], [vfee9], [vfee10], [vfee11], [vfee12], [vfee13], [vfee14], vfee15, vfee16) VALUES (N'Nosotros A', N'41', 1917.71, 0, 898.3, 10.870000, 0, 1906, CAST(0x353B0B00 AS Date), 994

Solution

When you have an OUTER JOIN, any WHERE clause against any outer table turns that into an inner table. Meaning only rows where that predicate can be evaluated will make it through. Effectively a WHERE clause in an OUTER JOIN makes that an INNER JOIN.

I would try:

AND ((fe.ViolationDate >= '20170601' AND fe.ViolationDate = '20160301' AND fj.feeduedate <='20160330'))


Or:

WHERE (fe.ViolationDate >= '20170601' AND fe.ViolationDate = '20160301' AND fj.feeduedate <='20160330')

Code Snippets

AND ((fe.ViolationDate >= '20170601' AND fe.ViolationDate <= '20170606')
  OR  (fj.feeduedate >= '20160301' AND fj.feeduedate <='20160330'))
WHERE (fe.ViolationDate >= '20170601' AND fe.ViolationDate <= '20170606')
  OR  (fj.feeduedate >= '20160301' AND fj.feeduedate <='20160330')

Context

StackExchange Database Administrators Q#177022, answer score: 9

Revisions (0)

No revisions yet.